Database Sharding & Partitioning
You'll learn to
- -Explain what a shard is, and use rough numbers to judge whether you need to shard yet
- -Compare hash-based, range-based and directory-based sharding, and the weakness of each
- -Choose a shard key, and say what makes one good
- -Name the costs of sharding: cross-shard queries, resharding and hot shards
Consistent hashing, in the previous chapter, is a general technique for dividing keys among machines. Sharding is what you call it when you apply that idea to a database: the data is split into slices, each slice lives on its own independent database instance, and each instance is responsible for only its slice. It is the main way to get past the limit of what one machine can store and write.
An Analogy: A Library Too Big for One Building
A city library has outgrown its building. Rather than build an impossibly large one, it opens several branches, and every book has to live in exactly one of them. The question is the rule for deciding where: by the first letter of the author's name, by a number worked out from the book's ID, or by looking each book up in a master catalog that says which branch holds it. Each rule works, each has awkward cases, and moving books between branches later is expensive.
- -The branches are the shards, each holding its own slice of the books.
- -Sorting by the author's first letter is range-based sharding.
- -A number worked out from the book's ID is hash-based sharding.
- -The master catalog is directory-based sharding.
- -The desk that points a reader to the right branch is the shard router.
Do You Actually Need to Shard Yet?
Sharding is usually the last resort, not the first move. Try the cheaper fixes first, and shard only when the numbers say a single primary database cannot cope. A rough gut check:
Sharding is usually the last resort, not the first move: it's a one-way door that adds real complexity (cross-shard queries, rebalancing, no more simple JOINs). Vertical scaling and read replicas solve a large fraction of real-world scale problems without it.
Three Ways to Decide Where a Row Goes
- -Hash-based: shard = hash(key) mod number of shards. With four shards and user ids 1001 to 1008 using id mod 4, the ids land on shards 1, 2, 3, 0, 1, 2, 3, 0, an even spread. Its weakness is range queries (neighboring ids are scattered) and changing the number of shards: as the previous chapter showed, going from 3 shards to 4 with a plain modulo moves about three keys in four.
- -Range-based: each shard owns a contiguous range, such as users A to F on shard 1. Range queries are efficient, since neighboring keys sit together. The weakness is hotspots: if the key is a timestamp or an ever-increasing id, all new writes go to the last shard while the rest sit idle.
- -Directory-based: a lookup table records which shard holds each key. It is the most flexible, because you can move any key by editing the table, but the directory is itself a critical component that every request depends on, so it must be fast, replicated and cached.
In practice, systems that need to add shards without moving most of their data combine hashing with the ring technique from the previous chapter, which keeps most keys where they are when a shard is added.
Choosing the Shard Key
The shard key, the field the placement rule is applied to, is the most important decision, because it is very hard to change later. A good key has many distinct values, spreads load evenly, and matches the way you query. Take an orders table. If you shard by user id, all of one user's orders sit on one shard, so the common query all orders for user 42 touches a single shard. If you shard by order id, the data spreads very evenly, but that same query must ask every shard and merge the answers, which is called scatter-gather and gets slower as shards are added. A celebrity account can still make one shard a hotspot even with a good key, and the usual remedy is to split that one key further.
The diagram shows the shape: a router sits in front, works out which shard owns the key, and sends the request there.
A shard router directs each request to the database instance that owns that piece of data.
What Sharding Costs
- -Cross-shard queries: a join between data on two shards is no longer a simple database join, and often has to be done in the application.
- -Cross-shard transactions: keeping a change that touches two shards all-or-nothing is much harder than inside one database, as a later module on distributed transactions explains.
- -Resharding: moving data between shards while the system is live is slow and delicate.
- -Operational weight: many databases to back up, monitor, upgrade and keep balanced.
The Wiki card below goes further into partitioning strategies and what to watch out for:
Split your database into multiple independent chunks (shards), each owning a subset of the data, deployed on separate hardware.
A library with 10 million books splits into 26 branches - one per letter of the alphabet. Books A-D go to Branch 1, E-H to Branch 2, and so on. Any librarian can immediately tell you which branch has your book. Each branch can be upgraded independently.
When a single database server cannot handle your data volume or write throughput, you partition (shard) the data across multiple database instances. Each shard is an independent database that owns a non-overlapping slice of the full dataset.
Key-based (Hash) Sharding: shard = hash(user_id) % num_shards. Simple and even distribution. Problem: resharding is painful - adding a new shard requires remapping and moving a huge portion of data. Consistent hashing solves this but adds complexity.
Range-based Sharding: Shard by value ranges (user_ids 0-1M on Shard 1, 1M-2M on Shard 2). Simple to understand and enables range queries within a shard. Problem: hotspots - if all new users are being created right now, they all land on the same shard.
Directory-based Sharding: A lookup table maps each key to a specific shard. Maximum flexibility (any key can be moved to any shard). Problem: the lookup table becomes a bottleneck and single point of failure.
The hardest problems with sharding: cross-shard joins (expensive, often impossible), distributed transactions (require complex 2-phase commit), and rebalancing (moving data when shard sizes skew). Many teams avoid sharding and instead choose Vitess, CockroachDB, or Spanner which handle sharding transparently.
- -Sharding enables horizontal scaling of writes (not just reads).
- -Shard key choice is critical and nearly irreversible after launch.
- -Cross-shard joins and transactions are extremely complex - avoid designing for them.
- -Consider Vitess (YouTube's MySQL sharding layer), CockroachDB, or Aurora for managed sharding.
- -MongoDB, Cassandra, and DynamoDB shard automatically and transparently.
In one line: sharding splits one database into independent slices chosen by a shard key. Hash spreads evenly, range keeps neighbors together but creates hotspots, a directory is flexible but is a dependency. Choose the key to match your queries, and do it only when vertical scaling and read replicas are not enough.
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!