Databases · Question 27 of 50

What is the difference between replication and sharding?

What they're checking

Scaling reads against scaling writes.

The answer

Replication keeps copies of the same data on several machines: a primary takes the writes and replicas copy them, so reads can be spread out and a replica can take over if the primary fails. Replicas usually lag a little, so a user may not see their own change if you read from a replica right after it; send those reads to the primary.

Sharding splits the data across machines by a key (user id, region), so each holds a part. It scales writes and storage, but queries and transactions across shards get hard, and a poor key makes hot shards. Usually you replicate first, and shard when one primary can no longer take the writes or hold the data.

The picture

replication:  [primary] ── copies ──▶ [replica] [replica]   same data, more reads
sharding:     users 1–1M  ─▶ [shard A]
              users 1M–2M ─▶ [shard B]                     different data, more writes
The follow-up they'll ask

How do you pick a shard key?

Show the answer

Something most queries filter by, with many values spread evenly, usually the user id. Not the creation date: every new write would land on one shard.

Your progress stays on this device.