Skip to main content
QuantLab Logo
Glossary · Infrastructure

What is Database Sharding?

Database sharding is the practice of splitting one enormous database into many smaller pieces called shards, each holding a different slice of the rows and running on its own server, so that no single machine has to store all the data or absorb all the traffic. It is the heaviest tool in the scaling toolbox — and the one you reach for last.

The wall it gets you past

A relational database on a single server can be scaled vertically — more CPU, more RAM, faster disks — for a long way. But there is a ceiling: eventually the dataset is too large to fit, or the write volume is too high for one machine to commit, and you cannot buy a bigger box. Replication helps with reads but not with this problem, because every replica still holds the entire dataset and every write still goes through one primary. Sharding breaks that ceiling by spreading the data itself horizontally across many machines, so each shard holds and serves only its portion.

Sharding vs. replication

These two are constantly confused, so it is worth being precise. Replication copies the same data to multiple servers — useful for scaling reads and for redundancy if a node dies. Sharding splits different data onto different servers — useful for scaling writes and total storage. They are orthogonal and most large systems use both: the data is divided into shards, and each shard is itself replicated for durability and read capacity. Reaching for replication when you actually need sharding (or vice versa) is a common and expensive misdiagnosis.

The shard key is everything

The single most consequential decision in sharding is the shard key — the column that determines which shard a given row lives on. A good key, like customer ID in a multi-tenant SaaS, keeps each customer's data together on one shard and distributes load evenly. A bad key creates two failure modes. Hot shards: if you shard by something skewed, one shard ends up with the celebrity account and all the traffic while others sit idle. Scatter-gather queries: if a common query does not include the shard key, the system must ask every shard and merge the results, which is slow and scales badly. Choosing the key well, up front, is most of the work.

Strategies: range, hash, and directory

There are a few ways to map keys to shards. Range-based sharding assigns contiguous ranges (customers A–F on shard one) — simple, but prone to hotspots if activity clusters. Hash-based sharding runs the key through a hash function to scatter rows evenly, at the cost of making range scans impractical. Directory-based sharding keeps an explicit lookup table mapping keys to shards, which is flexible and makes rebalancing easier but adds a lookup and a potential single point of failure. Consistent hashing — the same idea behind Redis Cluster — minimizes how much data must move when you add or remove a shard.

The costs you inherit

Sharding is powerful but it is not free, and the bill comes due in complexity. Joins and transactions that span shards become hard or impossible; foreign keys across shards do not work; queries that do not hit the shard key get slow; and rebalancing — moving data when a shard fills up — is genuinely painful in production. The application also has to become shard-aware, or sit behind a routing layer that is. This is why the standard advice is to exhaust the cheaper options first: a bigger server, read replicas, caching, and query tuning. Modern distributed databases such as CockroachDB, Vitess, and Citus automate much of the sharding machinery, which is often a better path than hand-rolling it.

At QUANT LAB

Our default advice on the systems we build under data engineering and SaaS platform development is: do not shard until you genuinely have to. Most "we need to shard" conversations are actually solved by an index, a cache, a read replica, or a query rewrite — changes that load testing usually reveals before any partitioning is needed. When sharding is truly warranted, we lean on managed distributed databases over bespoke logic, choose the shard key with the access patterns in mind, and keep the routing out of scattered application code.

Hitting a database scaling wall?

We diagnose the real bottleneck before reaching for sharding, then scale your data layer the right way. Book a 30-minute call.

Data engineering