Scaling Databases

A practical playbook — query optimisation, connection pooling, vertical scaling, read replicas, caching, CQRS, partitioning and sharding, and choosing the right database for each job.

Intermediate 3 min readLesson 8 of 20

The database is usually the hardest part of a system to scale, because it holds state. App servers can be cloned freely; data must stay consistent. This lesson is a playbook, in roughly the order you should reach for each tool.

  1. Optimise queries and indexes. Most "we need to scale" problems are slow queries (Indexing, Query processing). Fix N+1 queries, add the right composite indexes, avoid large OFFSETs.
  2. Pool connections. Each database connection costs memory (PostgreSQL forks a process per connection). Hundreds of app instances × 20 connections each can exhaust the database. Use a pooler (PgBouncer, RDS Proxy, HikariCP on the app side) and keep pools small.
  3. Scale vertically. A bigger instance with more RAM (to hold the working set) and faster storage goes a long way — often to tens of thousands of queries per second.
  4. Cache reads in Redis or in-process (Caching).
  5. Add read replicas for read-heavy traffic.
  6. Move work off the request path: queues for writes that can be asynchronous, batch writes.
  7. Partition tables and archive old data.
  8. Split by function (separate databases per service or domain).
  9. Shard when the write volume or data size exceeds one primary.

Read replicas

writesreadsApp serversPrimarywrites + critical readsReplica 1Replica 2
  • Route reads to replicas and writes to the primary (in the app, a driver, or a proxy).
  • Replicas are usually asynchronous, so they lag. Read your own writes from the primary (Replication).
  • Replicas also serve analytics and backups without loading the primary, and can be promoted on failure.

CQRS: separate read and write models

Command Query Responsibility Segregation uses different models (often different stores) for writes and reads:

  • Write side: a normalised, transactional database that validates commands.
  • Read side: denormalised projections optimised for each query — a search index, a precomputed feed, a reporting table — updated asynchronously from change events.

Great when reads and writes have very different shapes or scale. The cost: eventual consistency between the sides, and more moving parts.

Partitioning and sharding

When one primary can't handle the write volume or the data size, split the data horizontally (Sharding & partitioning). Choose a shard key that keeps most queries on a single shard, and expect cross-shard queries and transactions to become harder. Alternatives that shard for you: managed distributed SQL (CockroachDB, Spanner, Aurora Limitless, Citus, Vitess, PlanetScale) or NoSQL stores built for scale-out.

Choosing the right store for the job

Polyglot persistence — each workload in the store that suits it:

NeedStore
Transactions, relationships, flexible queriesPostgreSQL / MySQL
Caching, counters, sessions, leaderboardsRedis
Full-text search, log searchElasticsearch / OpenSearch
Huge write volume, simple access patternsCassandra / DynamoDB
Analytics over billions of rowsColumnar warehouse (BigQuery, Snowflake, ClickHouse)
Files, images, videoObject storage (S3)
Events and streamsKafka
EmbeddingsVector database / pgvector

Keep them in sync with change data capture (stream the primary database's log into Kafka, then into search indexes and warehouses) rather than dual writes from the application, which drift apart when one write fails.

Operational must-haves

  • Backups and tested restores, plus point-in-time recovery from the WAL.
  • Monitoring: query latency, slow query logs, replication lag, connections, cache hit ratio, disk and IOPS.
  • Safe migrations: backwards-compatible schema changes (expand → migrate → contract), online index builds.
  • Capacity planning: watch growth trends before you hit limits.
Key takeaways
  • Scale in order: optimise → pool → bigger box → cache → replicas → async → partition → split → shard.
  • Read replicas scale reads but lag; route read-your-writes to the primary.
  • CQRS separates write and read models, updated asynchronously.
  • Use polyglot persistence, kept in sync via change data capture.
  • Backups, monitoring and safe migrations matter as much as scaling.