Replication & Partitioning (Sharding)
Scale reads with replicas, survive node loss, and scale writes/storage across shards.
In plain words
Replication keeps copies of the same data on several machines so you can survive failures and serve more reads. Sharding splits the data into pieces so each machine holds only part of it.
Think of it like…
Replication is making photocopies of a textbook for multiple classrooms; sharding is splitting an encyclopedia A–G, H–P, Q–Z onto separate shelves.
Where you’ve seen it
Instagram splits users across many database shards by user ID, and each shard has replicas so a single server failure doesn’t lose anyone’s photos.
Words you’ll hear
- Replica
- A copy of the database kept in sync with the primary.
- Primary / leader
- The copy that accepts writes.
- Shard
- One slice of the data, stored on its own machine(s).
- Shard key
- The field used to decide which shard a record belongs to.
Visual diagram
Theory & deep dive
Replication vs. Sharding - don’t confuse them
Replication copies the *same* data to multiple servers. Why? Two reasons: (1) if the primary machine dies, a replica takes over; (2) reads can spread across replicas.
Sharding (Partitioning) splits *different* subsets of data across multiple servers (e.g., Shard 1 holds users 1–10M, Shard 2 holds 10M–20M). You shard when data size or write volume outgrows what any single machine can hold.
Choosing the right Shard Key
A bad shard key destroys everything. Sharding tweets by country overloads the US shard; sharding orders by date puts 100% of today’s writes onto one hot shard!
Good shard keys have high cardinality (millions of distinct values) and match your main query pattern - e.g., user_id, tenant_id, or hash(order_id).
How it works, step by step
Step 1 of 41. Add read replicas before sharding
Stream WAL/binlog changes from one primary to N read replicas. Keep writes on the leader and route read queries to replicas.
- Async Lag
- 10–200 ms
- Sync Lag
- 0 ms (higher write RTT)
When to use it & trade-offs
What it is
Replication copies the same data across multiple nodes for availability and read scale. Partitioning (sharding) splits a dataset across independent nodes so no single machine holds all writes or storage.
Why it matters: Once a dataset exceeds a single node’s disk/IOPS or write QPS saturates one primary, sharding and replication are the only path forward.
When to reach for it
- Read replicas when read QPS grows 10× faster than writes
- Sharding when write QPS or dataset size (>2–10 TB) outgrows one primary node
- Using
user_idororg_idas shard key when most queries are scoped to a user/tenant
When to avoid or be careful
- Sharding by
created_aton a high-write table (every new write hits the newest shard) - Running cross-shard JOINs or foreign keys on every request
Replication & Sharding Topologies
Strengths: No write conflicts; simple reasoning; replicas scale read QPS.
Watch out: Writes capped by one leader; async replicas can serve stale reads.
Strengths: Writes stay available locally even if a link or node fails.
Watch out: Concurrent write conflicts require LWW, vector clocks, or CRDTs.
Strengths: Hash prevents monotonic write hot spots; Range supports efficient BETWEEN queries.
Watch out: Hash breaks range scans; Range creates hot spots on "today’s" partition.
Interview signals & numbers to know
When this topic comes up
- terabytes of data
- write bottleneck
- single primary saturated
- read-your-writes
- hot shard
Benchmarks & rules of thumb
- Typical replication factor
- 3 copies (across AZs)
- Async replica lag
- 10–200 ms
- Shard size sweet spot
- 100 GB – 2 TB per shard
- Logical vs physical shards
- 10–50× logical buckets
Common mistakes & follow-ups
Common interview mistakes
- Routing a user’s profile page to an async read replica right after they clicked “Save” (read-your-own-writes bug)
- Picking a low-cardinality shard key like
country(US shard melts while Antarctica sits idle) - Ignoring how you will split shards later when data doubles
Likely interviewer follow-ups
- How do you fix read-your-writes inconsistency when using async read replicas?
- If you shard a social app by
user_id, how do you efficiently support global search or hashtag feeds? - Explain split-brain during leader failover and how fencing prevents data corruption.
Knowledge check
Put it into practice
Design full architectures with AI feedback
Apply Replication & Partitioning (Sharding) inside real High-Level Design prompts or review Q&A flashcards.