Sharding
Splitting a database into smaller pieces (shards) distributed across multiple servers. Each shard holds a subset of the data.
What is Sharding?
In short
Sharding splits one large database into many smaller independent databases called shards, each holding a slice of the rows, so the data and the load spread across many machines instead of one. A shard key decides which shard each row lives on. It is how you scale writes and storage past what a single server can hold, at the cost of much harder cross-shard queries.
Why sharding exists
A single database server has limits: a fixed amount of disk, memory, CPU, and write throughput. You can buy a bigger machine for a while, which is vertical scaling, but eventually the largest available server is not enough, or it gets absurdly expensive. At that point you have to spread the data across many machines, which is horizontal scaling.
Sharding is horizontal scaling for a database. Instead of one server holding all hundred million users, you run several servers and each holds a portion, say twenty-five million each. Reads and writes for a given user go only to the shard that holds that user, so the total capacity is the sum of all the shards. This is the main way large systems scale writes, because unlike read replicas, sharding splits the write load too.
The shard key decides everything
Every sharded system needs a shard key: the column whose value decides which shard a row belongs to. A router computes the shard from the key, for example by hashing the user_id and taking it modulo the number of shards, or by assigning ranges of the key to shards. From then on, any operation that knows the key goes straight to the right shard.
Choosing the shard key well is the hardest and most important decision in sharding. A good key spreads both data and traffic evenly across shards. A bad key creates a hotspot: if you shard an e-commerce system by country and most of your customers are in one country, that one shard gets hammered while the rest sit idle. The infamous version is the celebrity problem, where sharding social data by user means one hugely popular account overwhelms its shard.
Sharding by key
A router sends each row to one shard based on its shard key. Each shard is a full database holding only its slice of the data.
Range vs hash sharding
Range sharding assigns contiguous ranges of the key to shards: A to H on shard one, I to P on shard two, and so on. It makes range scans easy, since nearby keys live together, but it is prone to hotspots, because real data is rarely spread evenly across ranges and new sequential keys all land on the last shard.
Hash sharding runs the key through a hash function first and shards on the result. This scatters even sequential or skewed keys evenly across shards, which is great for balancing load, but it destroys locality, so a query for a range of keys now has to hit every shard. Many systems use consistent hashing here so that adding or removing a shard moves only a small fraction of the data rather than reshuffling everything.
In a real system
An application queries through a shard router that maps each shard key to one of several independent databases. No single machine holds all the data.

What gets harder once you shard
Sharding buys scale but charges a real tax. A query that does not include the shard key cannot be routed to one shard, so it becomes a scatter-gather that hits every shard and merges the results, which is slow and scales badly. Joins across shards are painful or impossible and often have to be done in the application. Transactions that span shards need distributed-transaction machinery that most teams try hard to avoid.
Resharding, changing the number of shards as you grow, is operationally heavy because it means moving large volumes of live data while the system keeps serving. Because of all this, the usual advice is to delay sharding as long as a single well-tuned database with read replicas can carry the load, and when you do shard, pick a key that keeps the queries you care about on a single shard.
Where it is used in production
Vitess (YouTube / PlanetScale)
A sharding layer on top of MySQL, built to scale YouTube's database and now the engine behind PlanetScale, hiding the routing from the application.
MongoDB
Has built-in sharding: you pick a shard key and a config server tracks which ranges live on which shards, with automatic balancing.
Citus (PostgreSQL)
An extension that turns PostgreSQL into a distributed, sharded database while keeping standard SQL.
Amazon DynamoDB
Transparently partitions a table by the partition key across many nodes, applying sharding automatically as the table grows.
Frequently asked questions
- What is database sharding?
- Sharding splits one database into several smaller independent databases (shards), each holding a subset of the rows, spread across multiple servers. A shard key decides which shard each row lives on. It scales storage and write throughput beyond what a single machine can handle.
- What is a shard key and why does it matter?
- A shard key is the column whose value determines which shard a row belongs to. It matters because it controls how evenly data and traffic spread. A good key balances load across shards; a bad key creates a hotspot where one shard gets most of the traffic while others sit idle.
- What is the difference between range and hash sharding?
- Range sharding assigns contiguous key ranges to shards, which keeps related keys together for easy range scans but is prone to hotspots. Hash sharding shards on the hash of the key, which spreads load evenly but loses locality, so range queries must hit every shard.
- What is the difference between sharding and replication?
- Replication copies the same data to multiple servers, mainly to scale reads and survive failures. Sharding splits different data across servers, so each holds only a slice, mainly to scale writes and storage. Large systems usually do both: shard the data, then replicate each shard.
- When should you shard a database?
- As late as possible. Shard only when a single well-tuned database with read replicas can no longer handle the write volume or data size, because sharding makes cross-shard queries, joins, and transactions much harder. When you do shard, choose a key that keeps your important queries on one shard.
Learn Sharding hands-on
This page explains the idea. The full lesson lets you step through the ring as servers join and leave, read the implementation, and check yourself with a quiz. It is one of 760+ lessons in the System Design Masterclass, from your first API call to distributed consensus. Eleven Foundation lessons are free, no signup. Lifetime access is ₹499 in India or $7.99 worldwide, one payment, no subscription.
Related lessons
Lessons that touch on Sharding as part of a larger topic.
Distributed SQL
How SQL databases scale across multiple nodes, sharding, query routing, distributed joins, and the coordination challenges
intermediate · database types storage
Distributed Cache
Spread your cache across multiple nodes with consistent hashing, when one Redis server isn't enough
foundation · caching strategies
Data Partitioning
Split massive datasets across multiple nodes so no single machine drowns
intermediate · data replication distribution
The RAG Retrieval Cliff: Engineering Recall Back at Scale
The corpus-growth cliff is one equation, lambda equals N times p. Learn the fixes that hold recall at ten million chunks: filtering, partitioning, coarse-to-fine and hierarchical retrieval, ANN tuning, and what each costs.
ml-intermediate · retrieval rag
See also
Related glossary terms you might want to look up next.
Replication
Keeping copies of the same data on multiple servers. Improves read performance and provides fault tolerance if one server goes down.
Consistent Hashing
A hashing technique where adding or removing servers only moves a small fraction of keys. Used by Amazon DynamoDB and Cassandra for data distribution.
Horizontal Scaling
Adding more machines to handle increased load (scaling out). Like opening more checkout lanes instead of making one cashier faster.
Database
An organized collection of data that can be easily accessed, managed, and updated. The backbone of almost every application.
SQL
Structured Query Language for managing relational databases. Tables, rows, columns, and powerful joins to query related data.
NoSQL
Databases that don't use traditional table-based relational models. Includes document stores, key-value, graph, and column-family databases.