System DesignIntermediate18 min

Replication & sharding

Copy data for availability and read scale, split data for write scale, and accept the new failure modes.

See it on the map

Replication keeps copies of the same data on several nodes. A leader takes writes and followers serve reads and take over if the leader fails. Followers lag behind the leader, so a read right after a write can return old data unless you route it carefully.

Sharding splits data across nodes so each holds a slice. The shard key decides everything: it must spread load evenly and keep the data a query needs together. A bad key creates a hot shard, which is a single-node bottleneck wearing a distributed costume.

Resharding later is painful, so choose a scheme that moves little data when nodes change, such as consistent hashing or a directory of fixed logical shards mapped to physical nodes.

Key points

  • Replication is for availability and read scale. Sharding is for write and storage scale.
  • Replica lag means read-your-writes needs deliberate routing.
  • A good shard key spreads load and keeps related data together.
  • Cross-shard queries and transactions are expensive. Design to avoid them.

Trade-offs

ChoiceYou gainYou pay
Hash shardingEven distributionRange scans touch many shards
Range shardingEfficient range queriesHot ranges, such as the newest time slice

Answer these out loud

  1. 1You shard orders by customer id. What happens for a customer who is 1,000x bigger than the median?
  2. 2Describe how a user sees stale data after a write with async replicas, and two ways to prevent it.

Use this from an agent

Any MCP client, including Grok, can read this lesson. See how to connect.

REST
curl https://engineeros-delta.vercel.app/api/v1/topics/replication-and-sharding

This unlocks