Skip to content

The database is the bottleneck

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
  • 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.

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.

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
  1. Measure which queries cost the most, using the database’s own statistics, and fix the top ten.
  2. Pool connections, so application instances share a limited number of database connections.
  3. Separate workloads: send reporting to a warehouse fed by change data capture, and search to a search index.
  4. Send read-heavy, lag-tolerant queries to replicas, and cache data that is read far more than it changes.
  5. Split along domain boundaries when teams and load justify it, so each domain owns its own database.
  6. Shard only what still doesn’t fit, choosing a key that keeps most queries on one shard.
Taking load off the primary database1. Data that is read far more often than it changes comes from a cache. 2. Writes, and reads that must be current, go to the primary. 3. Reads that can tolerate a little lag go to replicas. 4. Text search uses a search index instead of database queries. 5. Change data capture keeps the search index and warehouse up to date, so reports never touch the primary. Applicationthrough a connection poolCachehot, rarely changing data Search indextext and filters Read replicasreads that can lag Primary databasewrites, current reads Change data capturestreams changes Data warehousereports, analytics12345
Taking load off the primary database Writes and must-be-current reads stay on the primary; everything else goes somewhere better suited.
  1. Data that is read far more often than it changes comes from a cache.
  2. Writes, and reads that must be current, go to the primary.
  3. Reads that can tolerate a little lag go to replicas.
  4. Text search uses a search index instead of database queries.
  5. Change data capture keeps the search index and warehouse up to date, so reports never touch the primary.
  • 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.

Primary database utilization at peak drops with headroom to spare, slow-query counts fall, and reports no longer affect customer-facing response times.

  • 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.

Written and edited by Abhinav Taduka, enterprise software architect.