Interview's MateSystem Design
Interview's Mate

System Design

Architecture & trade-offs

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
System Design topics
Lesson 10/ 31
Storage, Caching & Search

Replication & Partitioning (Sharding)

Scale reads with replicas, survive node loss, and scale writes/storage across shards.

IntermediateReplicas · Sharding · Hot Keys3 quiz questions0/2 subtopics done
Practice in AI HLD
In plain wordsDiagramTheorySubtopics (2)Step by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice
Deep-dive subtopics (2) - 0/2 completed
Leader-Follower, Multi-Leader & Leaderless Replication

Compare single-leader, multi-leader, and Dynamo-style leaderless replication.

Intermediate
Sharding Strategies & Celebrity Hot Spots

Hash vs. range sharding, cross-shard JOINs, and handling viral hot partitions.

Intermediate

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

Loading interactive diagram
Loading interactive exercise

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.

Key takeaway: Replication copies the same data for reliability & reads; Sharding splits data for storage & write scale.

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 4
Leader-Follower Replication

1. 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.

Watch out for replication lag on read-your-writes paths
Key numbers at this step
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_id or org_id as shard key when most queries are scoped to a user/tenant

When to avoid or be careful

  • Sharding by created_at on a high-write table (every new write hits the newest shard)
  • Running cross-shard JOINs or foreign keys on every request

Replication & Sharding Topologies

Single-Leader Replication
Best for: Most RDBMS apps with strong ordering and read scaling

Strengths: No write conflicts; simple reasoning; replicas scale read QPS.

Watch out: Writes capped by one leader; async replicas can serve stale reads.

Multi-Leader / Leaderless (Quorum)
Best for: Multi-region local writes, Dynamo/Cassandra high availability

Strengths: Writes stay available locally even if a link or node fails.

Watch out: Concurrent write conflicts require LWW, vector clocks, or CRDTs.

Hash vs. Range Sharding
Best for: Point lookups (Hash) vs. time/alphabetical ranges (Range)

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.
Interview tip: Always discuss replication lag and read-your-writes consistency right after proposing read replicas.

Knowledge check

Loading quiz

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.

Open AI HLD Practice →SD Interview Q&A
Previous topic
Indexing & Storage Engines
Next topic
Caching Strategies