Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

SD
System Design
0/13 completed
Beginner
What is system design?How the internet worksBack-of-the-envelope estimation
Intermediate
Scaling basicsLoad balancersCachingDatabases at scale
Advanced
Sharding and consistent hashingMessage queues and event streamingConsistency, CAP and distributed transactionsReliability and observability
Pro
Designing a URL shortenerDesigning a chat app
Tracks / System Design / Advanced

Sharding and consistent hashing

When one database can't hold all the data or absorb all the writes, you shard it: split the rows across many independent databases (shards), each holding a slice. Sharding is how Instagram, Discord and Uber store billions of rows. The key decisions are how to split and how to move data when you add shards, which is where consistent hashing comes in.

Choosing a shard key

The shard key decides which shard a row lives on. A good key:

  • Spreads data and traffic evenly (no "hot" shard).
  • Matches the main access pattern, so most queries touch one shard: shard orders by customer_id if you mostly load a customer's orders.
  • Doesn't change over time.
Strategy How Good Bad
Range A–M on shard 1, N–Z on shard 2; or by date Range queries stay on one shard Hot spots (everyone writes today's dates)
Hash hash(key) % N Even spread Range queries hit every shard; adding shards moves most data
Directory / lookup A table maps key → shard Full flexibility, easy rebalancing The directory must be fast and highly available
Geographic Users in India → Mumbai region Low latency, data residency laws Uneven region sizes

The problem with hash % N

Hashing spreads keys evenly, until you add a shard. With hash(key) % N, changing N changes the shard of almost every key, so almost all data must move:

Python · editable

80% of the data would have to be copied between machines while the system is running. Ideally, adding a 5th shard should move only about 1/5.

Consistent hashing

Place the shards on a ring of hash values. Each key belongs to the first shard found clockwise from the key's hash. Adding a shard only takes over the keys between it and its neighbour; everything else stays put. Giving each shard many virtual nodes spreads the ring evenly:

Python · editable

Consistent hashing is used by DynamoDB, Cassandra, Riak, Discord's message storage, CDNs choosing which server caches a file, and distributed caches.

Hot keys and celebrity problems

Even with perfect hashing, one key can be too popular: a celebrity with 100 million followers, a viral product, a huge customer. Mitigations:

  • Split the hot key: append a suffix (post:42#0 … post:42#9) to spread it over shards, and combine results when reading.
  • Cache hot items aggressively in front of the database.
  • Give huge tenants their own shard (a directory-based exception).

What sharding costs

Sharding solves scale, and adds lasting complexity:

  • Cross-shard queries ("all orders above ₹10,000 this week") must ask every shard and merge the results (scatter-gather).
  • Cross-shard transactions need distributed protocols or careful design (see the consistency lesson); most designs keep each transaction within one shard.
  • Joins across shards are impractical; denormalise, or keep related data on the same shard (shard orders and order items both by customer_id).
  • Unique IDs can't come from one auto-increment counter (the URL shortener lesson shows alternatives).
  • Rebalancing data while serving traffic is delicate.

That's why the advice is: scale up, add indexes, caches and replicas first; shard when you must. When you do, managed systems such as Vitess (MySQL), Citus (PostgreSQL), CockroachDB, YugabyteDB, Spanner, DynamoDB and MongoDB handle much of this for you.

Practice

  1. Change the number of virtual nodes to 5 and then 500. How does the spread across shards change?
  2. In the ring, what fraction of keys move when you go from 10 to 11 shards? Compare with % N.
  3. Pick shard keys for: a chat app's messages, an e-commerce order table, a multi-tenant SaaS product. Explain the main query each key keeps on one shard.
✎ Check your understanding

1.Why does hash(key) % N cause problems when adding a shard?

2.What makes a good shard key?

3.Why use virtual nodes in a consistent hash ring?

← Databases at scale