Scaling a Database
The database is usually the first thing to buckle under load. Here's the ordered playbook for scaling it.
Scaling a database in 30 seconds
- The database is usually the first bottleneck because it holds state while the web tier stays stateless.
- The ordered playbook: optimize and tune, scale up (vertical), add a cache, read replicas, then shard.
- Cheap-and-simple first, powerful-and-complex last — each step costs more complexity than the one before.
- Reads scale easily with caching and replicas; writes are the hard part.
- Only sharding truly scales writes, because each shard writes its own slice independently.
- Don't skip to sharding — it's the most common premature optimization; exhaust the cheaper steps first.
Why the kitchen jams before the dining room
Picture a restaurant getting popular. Adding tables and waiters is easy — you just hire more and put out more chairs, and they don't step on each other because a waiter doesn't remember anything between trips. But there's one kitchen, and every order funnels through it. No matter how many waiters you add, the kitchen is where the line backs up, because it's the one place that holds the state everyone depends on.
A web system works the same way. The web servers are the waiters — stateless, so you add more at will. The database is the kitchen — it holds the state, so everything eventually waits on it, and it's almost always the first thing to buckle. Scaling a database is the art of relieving that one kitchen without burning it down, and there's a well-worn order to the moves.
The problem it solves
You can scale the web tier easily by adding stateless servers, but they all talk to the database — which holds state and quickly becomes the bottleneck. Scaling a database is harder precisely because of that state. The good news is there's a well-worn, ordered playbook: cheap-and-simple moves first, powerful-and-complex moves only when you've earned the need.
The ordered playbook
The moves go in a deliberate order — cheapest and simplest first, most powerful and most complex last. The discipline is to climb only as far as your actual load forces you, because every step up costs real complexity you can't easily undo.
Optimize first
Add the right indexes, fix slow queries, tune the schema and connection pool. The cheapest 'scaling' is often making the existing database do less work.
Scale up (vertical)
Give the database more CPU, RAM, and faster disks. Simple and effective — until you hit the ceiling and the price curve.
Add a cache
Put a cache in front of hot reads so most requests never touch the database. Huge relief for read-heavy workloads.
Read replicas
Copy data to followers and send reads there. Scales reads well; mind replication lag and read-your-writes.
Shard (partition)
Split the data across nodes so writes spread out. The most powerful move — and the most complex, so it comes last.
Step 1-2: optimize, then scale up
The first two steps need no new architecture, which is exactly why they come first. Optimizing means making the database do less work for the same result: the right index turns a full-table scan into a jump, a rewritten query stops re-reading the same rows, a tuned connection pool stops threads from queuing. It's astonishing how often a single missing index is the 'scaling problem.' Scaling up (vertical) is just buying a bigger machine — more CPU, more RAM, faster disks. Both are cheap in complexity; you change nothing about how the system is shaped.
The catch with scaling up is that it has a hard ceiling and an ugly price curve — the biggest machine money can buy is finite, the cost per unit of power climbs steeply, and one giant machine is still a single point of failure. So it buys time, not a permanent answer.
Step 3-4: cache, then read replicas
When a bigger box isn't enough, attack the read load, because most systems read far more than they write. A cache in front of the database answers hot reads from memory so the request never reaches the database at all — the single biggest relief for read-heavy traffic. When the cache isn't enough, read replicas copy the data to follower nodes and you send reads there, spreading read load across many machines.
def route(query):
if query.is_write():
return primary # one leader absorbs all writes
# Spread reads across cheap followers
return random.choice(replicas)
# Caveat: a follower may lag the primary by milliseconds.
# For read-your-writes, pin a user's reads to the primary
# briefly after they write, or route them to a caught-up replica.The gap caches and replicas can't fill — writes
Here's the wall every system eventually hits: caching and replicas only scale reads. A cache holds read results; it doesn't absorb writes. A read replica is a copy of the data, and every copy still has to apply every write — so adding replicas actually adds write work, it doesn't reduce it. All writes still funnel through the single leader. If your bottleneck is write throughput, you can add a thousand replicas and the leader is still the choke point.
This is the key insight. Read scaling is relatively easy — caching and read replicas handle enormous read volume because copies are cheap. Write scaling is the hard one, because every copy must eventually agree. Replicas don't help write throughput; only sharding truly scales writes, by giving each shard its own slice to write independently. So if your bottleneck is reads, you have easy options; if it's writes, you're heading toward sharding.
Step 5: shard — the only real write-scaling move
Sharding is the last step because it's the only one that scales writes, and the most complex. Instead of one leader taking all writes, you split the data across many shards, each with its own leader taking only its slice of the writes. Five shards, five leaders, a fifth of the write load each. That's genuine horizontal write scale — but it comes with cross-shard queries, resharding, and hot keys, which is why you don't reach for it until the cheaper steps are exhausted.
SHARDS = [shard0, shard1, shard2, shard3]
def shard_for(user_id):
return SHARDS[hash(user_id) % len(SHARDS)]
def write_order(order):
# Each shard writes its own slice independently -> writes scale out
shard_for(order.user_id).insert(order)
# The cost: a query spanning many users must scatter-gather
# across shards, and cross-shard transactions get hard.| Technique | Scales | Complexity | Main caveat |
|---|---|---|---|
| Indexing / tuning | Both (does less work) | Low | Diminishing returns eventually |
| Vertical scaling | Both (to a ceiling) | Low | Hard limit; single point of failure |
| Caching | Reads | Medium | Staleness, invalidation, stampedes |
| Read replicas | Reads | Medium | Replication lag; writes still centralized |
| Sharding | Writes (and storage) | High | Cross-shard queries, resharding, hot keys |
Reading the bottleneck before you act
The playbook only works if you know which resource is actually saturated — pulling the wrong lever wastes effort and money. Before adding anything, look at the numbers: is CPU pegged, is the disk thrashing, are reads or writes dominating, is one slow query eating the box? The answer tells you which step you're actually on.
| Symptom | Likely bottleneck | Right move |
|---|---|---|
| One query slow, rest fine | Missing index / bad plan | Optimize (step 1) |
| CPU & RAM maxed, load broad | Machine too small | Scale up (step 2) |
| Same hot reads over and over | Repeated read work | Add a cache (step 3) |
| Read volume high, writes modest | Read capacity | Read replicas (step 4) |
| Write throughput maxes the leader | Single write node | Shard (step 5) |
| Dataset won't fit one machine | Storage ceiling | Shard (step 5) |
Scaling a database is less about any one technique and more about a disciplined order of moves — knowing which cheap lever to pull before reaching for the expensive, irreversible one.
Why it matters
- The database is where most systems hit their first real wall, so the playbook is one every engineer needs.
- Pulling the levers in order saves enormous cost and complexity — the cheapest fix often buys years.
- Understanding why reads and writes scale differently is what keeps you from reaching for the wrong tool.
If ignored
- Jumping straight to sharding brings cross-shard joins, resharding pain, and a migration you can't easily undo.
- Assuming read replicas help write throughput sends you down a path that never relieves the real bottleneck.
- Ignoring replication lag and read-your-writes ships a database that quietly shows users stale data.
Where it fits
- Sits at the data tier, the usual limiting factor once the stateless web tier scales out.
- Sequences the other data-layer tools — indexing, caching, replication, sharding — into one path.
- A capacity-planning discipline as much as an architecture, applied step by step as load grows.
Related topics
- Replication: the read-scaling step, with lag and failover to account for
- Sharding: the final, write-scaling step — powerful and the most complex
- Caching: the big relief for read-heavy load before you touch the database itself
- Indexing: step one — making the existing database do less work
Interview lens
Questions it unlocks
- Your database is the bottleneck — walk me through scaling it.
- How do you scale reads versus writes?
- When is it finally time to shard?
60-second answer
I follow an ordered playbook: index and tune first, scale the box up while it's cheap, add a cache for hot reads, then read replicas, and only then shard. The key framing is that reads and writes scale differently — caching and replicas handle reads cheaply, but only sharding truly scales writes because each shard writes independently. So I shard last and only when write throughput or data size genuinely exceeds a well-tuned single primary, because sharding brings cross-shard queries and resharding complexity I don't want prematurely.
Common traps
- Jumping straight to sharding before cheaper options are exhausted.
- Assuming read replicas help write throughput — they don't.
- Ignoring replication lag and read-your-writes when adding replicas.