Database Scaling & Replication

From one database server to replicas, partitions and shards — in the order you should reach for them.

Intermediate10 min read3 interview questions

The database is usually the first real bottleneck, because it holds state and cannot simply be cloned like a web server. Scaling it is a sequence of steps, each adding capacity and complexity.

Start with the cheap wins — indexes, query tuning, caching, a bigger machine — before reaching for replicas and shards.

Diagram

writesreadsreplicatereplicatemissApplicationPrimaryall writesReplica 1readsReplica 2readsCachehot reads
A primary takes writes and streams changes to read replicas; a cache absorbs the hottest reads.

How it works

  1. Optimise: add the right indexes, fix slow queries, use connection pooling.
  2. Cache: put hot, read-mostly data in Redis or an in-process cache.
  3. Scale up: move to a larger instance — often the cheapest step.
  4. Read replicas: send read-only queries to copies of the primary.
  5. Partition: split very large tables by date or key inside one database.
  6. Shard: split data across multiple databases when one primary can’t take the writes.

Key concepts

Primary–replica
One node accepts writes and ships its change log to replicas that serve reads.
Replication lag
Replicas can be milliseconds to seconds behind, so a user may not see their own write.
Synchronous replication
The write waits until a replica confirms. Safer, but slower.
Multi-primary
Several nodes accept writes. Needs conflict resolution; use only when required.
Failover
Promoting a replica to primary when the primary fails, manually or automatically.

Trade-offs

Benefits

  • Replicas multiply read capacity
  • Replicas in other zones give high availability
  • Each step can be taken incrementally

Costs

  • Replication lag causes read-your-own-write surprises
  • Replicas do not help write throughput
  • Sharding changes the application and complicates queries

Use it when

  • Read-heavy workloads (most web apps read far more than they write)
  • Reporting queries that would slow down the primary
  • High-availability requirements

Common mistakes

  • Reading from a replica right after a write and showing stale data
  • Jumping to sharding before indexing and caching
  • Running heavy analytics on the primary

Interview questions

Q1A user updates their profile and the page still shows the old value. Why?
The write went to the primary but the read came from a lagging replica. Read from the primary for a short time after a user’s own writes, or route that user’s reads to the primary.
Q2Do read replicas help with a write-heavy workload?
No. Every replica must still apply every write. Write scaling needs batching, queuing, a faster primary or sharding.
Q3What is the difference between partitioning and sharding?
Partitioning splits a table into pieces inside one database server. Sharding spreads data across separate servers, each holding a subset.