The database is the bottleneck
At a glance
Section titled “At a glance”| Aspect | Aspect |
|---|---|
| Typical organization | Any successful application a few years old, running on one large relational database |
| Symptoms | High CPU or I/O on the database; slow queries at busy times; the largest instance size is close |
| Root cause | One database serving every workload: transactions, reports, searches and background jobs |
| Usual fix | Tune first, then separate reads, cache, split by domain, and shard only when nothing else is enough |
| Patterns | Read replicas, caching, CQRS, partitioning, sharding |
Sound familiar?
Section titled “Sound familiar?”- The database is already on the biggest instance the provider offers, or close to it.
- A monthly report slows down checkout.
- Every team’s service reads and writes the same tables.
- Schema changes need a maintenance window and a lot of nerve.
- Connection limits are hit before CPU is.
Why it happens
Section titled “Why it happens”A single relational database is the right choice for a young product: simple, consistent and well understood. Success adds users, features and teams, and each adds load and new kinds of queries. Eventually transactional traffic, analytics, search and batch jobs all compete for the same machine. Most databases hit trouble from mixed workloads and poor queries long before they hit raw size limits.
Your options
Section titled “Your options”Try these roughly in order; each is cheaper and less risky than the next.
| Option | When it fits | Cost and risk |
|---|---|---|
| Tune: indexes, query fixes, connection pooling | Always first | Low. Often gives the biggest win |
| Move reporting and search off to a warehouse or search index | Analytics or text search are on the main database | Low to medium |
| Read replicas | Reads dominate and can be slightly stale | Low to medium. Replica lag |
| Cache hot data | The same data is read again and again | Medium. Invalidation is hard |
| Split by domain: a database per service or module | Different parts of the system have different load and owners | Medium to high. Cross-domain joins disappear |
| Partition or shard a large table | One dataset is too big or too busy for one machine | High. Every query must know its shard key |
A proven approach
Section titled “A proven approach”- Measure which queries cost the most, using the database’s own statistics, and fix the top ten.
- Pool connections, so application instances share a limited number of database connections.
- Separate workloads: send reporting to a warehouse fed by change data capture, and search to a search index.
- Send read-heavy, lag-tolerant queries to replicas, and cache data that is read far more than it changes.
- Split along domain boundaries when teams and load justify it, so each domain owns its own database.
- Shard only what still doesn’t fit, choosing a key that keeps most queries on one shard.
- Data that is read far more often than it changes comes from a cache.
- Writes, and reads that must be current, go to the primary.
- Reads that can tolerate a little lag go to replicas.
- Text search uses a search index instead of database queries.
- Change data capture keeps the search index and warehouse up to date, so reports never touch the primary.
Trade-offs and what to watch
Section titled “Trade-offs and what to watch”- Replica lag can show users stale data right after they change it; read your own writes from the primary.
- Every copy adds something to keep consistent and operate.
- Sharding is hard to undo. Choose the shard key carefully, and only shard what you must.
How you’ll know it worked
Section titled “How you’ll know it worked”Primary database utilization at peak drops with headroom to spare, slow-query counts fall, and reports no longer affect customer-facing response times.
Seen in the real world
Section titled “Seen in the real world”- Figma sharded Postgres horizontally while keeping it Postgres.
- Slack moved to Vitess to shard MySQL by keys other than workspace.
- Discord put a data-service layer in front of its database to combine identical requests.
Related on ArchitectureCompass
Section titled “Related on ArchitectureCompass”Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.