Overview
Curated: · Written: · Reviewed:
Database Scaling (Sharding & Replication)
In one line
Find the saturated resource first, then apply the cheapest remedy that relieves it: query and index fixes, pooling, and caching before replicas. Replicas buy read throughput and availability. Sharding is the only rung that adds write throughput, and it is used only when one primary cannot take the write rate or the dataset.
Key definitions
- Async replica: a copy fed from the primary's WAL. Reads from it can trail the primary by the replication lag.
- Read-your-writes: a user sees their own committed write on the next read.
- Partitioning: splitting one table by a key, usually time, inside one database on one primary.
- Sharding: splitting data across independent databases by a shard key, so each shard owns writes for its keys.
- Shard key: the column that decides which shard owns a row. It must spread writes and appear in most queries.
- Fencing: a token that stops a deposed leader from accepting writes after failover.
Formulas and complexity
Rank statements by total time, not mean. In the MCQ 12 snapshot, Q1 is 2,000,000 × 0.4 ms = 800 s and Q4 is 300 × 900 ms = 270 s, so Q1 is tuned first.
How it works
- Every write commits on the primary and is shipped to every replica, so replicas add work to the primary instead of removing it.
- A read routed to a replica can miss a write made moments earlier, because the replica has not applied it yet.
- A query that filters on the partition key touches only the matching partitions. Other queries touch all of them.
- A shard router maps the shard key to a database. A query that omits the shard key goes to every shard.
When to use and when not to
| Lever | Use when | Avoid when |
|---|---|---|
| Query and index fix | a few statements dominate total time | the plan already reads a small slice |
| Connection pooler | connections queue, or active backends exceed cores | sessions rely on SET, prepared statements or advisory locks in transaction mode |
| Caching | the data is read far more than it changes | staleness is unacceptable and invalidation is not owned |
| Vertical scaling | the working set fits a bigger machine and queries are efficient | the largest instance is near, or a restart is not acceptable |
| Read replicas | reads dominate and can tolerate lag | writes saturate the primary, or users re-read their own writes |
| Partitioning | retention by time, or table and index size | the bottleneck is write throughput |
| Sharding | one primary cannot take the write rate or the dataset | a well-indexed single node still has headroom |
Traps interviewers probe
- Adding replicas to raise write throughput. Writes stay on the primary, which now also ships WAL to each replica. Fix: fix the top queries, measure write TPS, then shard by a key present in writes.
- Reading your own write from a replica. Fix: return the written row from the write response, or route that user to the primary for a window, or wait for a known replication position.
- Raising the pool to clear a queue. Active backends beyond the core count slow everything down. Fix: size the pool near the core count and let the pooler queue requests.
- Sharding by time. The newest shard takes every current write. Fix: hash or directory routing on a key that is present in most queries.
- Partitioning without the partition key in unique constraints, or without future partitions. Uniqueness is not enforced across partitions, and a missing partition makes inserts fail at the boundary. Attaching a partition while a DEFAULT partition holds matching rows forces a scan of that partition under a lock on the parent. Fix: include the partition key in unique constraints, automate partition creation, and keep the DEFAULT partition empty.
Interview tips
Opening: "Which resource is saturated: CPU, I/O, memory, connections or lock contention? I'd check the top statements by total time before picking a remedy."
Follow-ups to expect:
- What breaks when you add replicas? Read-your-writes and monotonic reads.
- How do you choose a shard key? Even spread, presence in most queries, co-located related rows.
- What happens on failover? Consensus picks one leader, fencing stops the old one, and asynchronous replication can lose acknowledged commits.
- Synchronous or asynchronous? Durability against commit latency, chosen per workload.
remote_applyis needed if a standby read must see the write. - Why not shard now? It is the last rung, it is hard to reverse, and it brings cross-shard joins, cross-shard transactions and operations multiplied by N.
30-second outline: Name the saturated resource and the evidence. Fix the top queries and indexes first, because they are free and reversible. Add pooling if connections queue. Use replicas for read throughput, and state lag and read-your-writes as the cost. Shard only when writes or the dataset outgrow one primary, using a key that spreads writes and appears in most queries, and accept the cross-shard costs.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.