Database Sharding Explained: Range, Hash and Directory Sharding

Blog / Database Sharding Explained: Range, Hash and Directory Sharding
Database sharding simply explained

What actually is database sharding? And why do large systems split one database across many servers?

The Single-Server Problem

A single database server will eventually reach physical limits on storage, CPU and write throughput.

Buying bigger hardware is called vertical scaling. It works for a while, but eventually hits a cost and physical hardware ceiling.

Read replicas copy the exact same data to multiple servers. They help reads, but they do nothing for writes.

So how can we scale our write capacity? This is where sharding comes in.

Splitting Rows Across Servers

Sharding is horizontal partitioning across separate, independent servers. It splits a table's rows across several independent database servers called shards.

Every shard has the exact same schema but holds a completely different subset of rows.

The shard key is the specific column that decides which shard a given row goes to, for example user_id.

A router sends each query to the right shard. It can live in a few places:

  • In the application

  • In a proxy, like Vitess for MySQL

  • In an extension, like Citus for PostgreSQL

  • In a dedicated router, like mongos for MongoDB

An app sends a query to a router, which sends it to the one shard that holds the row

Remember that replication copies the exact same data, but sharding splits it.

So how do we map a shard key to a specific shard? Let's look at range sharding.

Range Sharding

In range sharding, each shard owns a contiguous block of the shard key.

For example, user_id 1 to 1 million lives on shard 1, and the next million lives on shard 2.

A range query asking for users 100 to 200 is highly efficient, because it goes directly to one single shard.

But range sharding can create a severe performance bottleneck.

The Hot Shard Problem

If you use a shard key that only goes up, like created_at, every new write lands exclusively on the newest shard. The older shards sit completely idle.

That one overloaded node is called a hot shard, and it defeats the purpose of splitting the write load.

Every new write lands on the newest shard while the older shards sit idle

So how can we spread writes evenly? This is where hash sharding comes in.

Hash Sharding

Hash sharding passes the shard key through a hash function and divides by the total number of shards. This uses a tiny expression like hash(user_id) % 4

This scrambles the inputs and spreads rows evenly, so no single shard gets all the new writes.

The cost is that neighbouring keys end up on completely different shards, so a range query has to ask every single shard.

Hash sharding spreads users 1 to 4 across four shards, so a range query hits every shard

Adding a Shard

When you use modulo math, adding a single shard changes the denominator.

Almost every key will now map to a different shard, and nearly all your data has to move across the network.

Consistent hashing fixes this problem, so only a small slice of data moves. We have a whole video on it.

Choosing a Shard Key

A good shard key has three properties:

  • It has many distinct values

  • It spreads rows evenly

  • It matches your most common queries, so each query hits just one shard

Compare user_id with created_at. A user ID is good because it isolates all of one user's data on a single shard, whereas a timestamp is bad because it creates a hot shard.

A single celebrity user can still overload one shard, even with perfect hashing.

Directory sharding keeps a lookup table that maps each key to a shard. It is flexible, but adds an extra lookup query.

The Cost: Cross-Shard Queries

Any query that does not include the shard key has to be sent to every shard, and the results merged.

This is called a scatter-gather query, and it is notoriously slow.

Because the data is physically separated, SQL joins across shards have to be executed manually in your application layer.

A database transaction spanning multiple shards requires a distributed protocol like two-phase commit, which is extremely slow and complex.

With all these costs, you might wonder if you should use sharding at all.

When Not to Shard

It is a common interview trap for candidates to reach for sharding far too early. Sharding introduces immense operational complexity.

You should always try better indexes, caching and read replicas first.

You should only shard when a single server physically cannot handle the write throughput or the total data size.

Recap

Feature

Range sharding

Hash sharding

Directory sharding

How a row finds its shard

Contiguous block

Modulo math on hashed key

Lookup table

Range queries

Fast, hits one shard

Slow, hits all shards

Slow, hits all shards

Even spread of writes

Poor, creates hot shards

Excellent, spreads evenly

Excellent, explicit control

Adding a shard

Easy to split a block

Hard, moves massive data

Easy, update the table

Choosing the right strategy depends entirely on your specific traffic patterns and query needs. Knowing these trade-offs will help you design highly scalable systems.

About TechPrep

Never walk into a technical interview unprepared again. TechPrep empowers software engineers to stop guessing and start getting offers. We provide the exact questions asked by tech companies across Data Structures & Algorithms, System Design, Low-Level Design & AI coding rounds. Don't leave your career up to chance. Join thousands of engineers who have successfully navigated the tech hiring maze and landed roles at top tech companies.