Sharding: Picking a Key You Won't Regret in Two YearsMixed sources3 sources, drawn from a mix of official docs, project repositories and research.This rates where the sources come from. It doesn't prove every claim.
Hash vs range vs directory sharding, the shard-key checklist, consistent hashing in C#, dual-write resharding, and when managed Cosmos DB / SQL Hyperscale beats DIY.
By Amit UpadhyayPublished Updated 14 minArticle 5 of 6 in System Design
AI assistant · answers from this article
1 of 5
One database copes
Nothing to add yet
One server holds everything. Sharding is the most expensive fix on the list, so it comes after every cheaper one.
On this page
Sharding, in one sentence
Sharding splits one database’s data across several servers, so each server stores and writes only a part of it.
You reach for it when one database server can no longer handle the data or the write load, even after indexing, partitioning and replicas. Not before: it costs routing logic, cross-shard queries for the rare global case, and rebalancing work as tenants grow.
The cheat sheet
What to use when
You do not need all of this on day one. Each row is one signal and the piece that answers it. Stop at the row your system has reached, or pick one to see it in the picture above.
Where you areThe signalWhat to useWhat it costs
None, or it is slow but indexes, partitioning and replicas still help.Nothing. Use those firstNothing
The primary is at its write limit, and the cheaper fixes are used up.Split the data across servers by a shard keyRouting logic on every query path
An ordinary dashboard query has to ask every shard.Shard by tenant, so most queries stay on one shardStep 1 ↓Big tenants make the shards uneven
One shard is hot while the others sit idle.A directory that maps each tenant to a shard, so one can be movedStep 5 ↓The directory is on every request path
A reporting query fans out to every shard and slows them all.Serve reporting from a warehouseStep 3 ↓Reports are a little behind
Start With One Database
One database server takes you further than the headlines suggest. Indexes, partitioning, read replicas and a bigger machine each buy real room, and every one of them is cheaper than sharding, which changes how every query in the application is written. So sharding is the last thing to reach for, not the first.
The signal that you've arrived is specific: writes, not reads, have outgrown one machine, and the cheaper fixes are used up. From there the decisions come in a fixed order: the key, the routing, where related data lives, and how to move data later. The table above lists the signals. This article explains keys and placement, then answers each situation with a way to check it.
Before you shard
Your single DB is at 80% CPU and you're scared to deploy
You think sharding by user_id % N is fine until you have to add a shard
One of your shards is at 95% disk and the rest are at 30%
You can't do a cross-shard JOIN and didn't plan for that
This is the sharding playbook — when to actually shard, how to pick a key, hash vs range vs directory, and how to rebalance without a 3-day outage.
Pro deep dive14 min full read
Keep reading with Pro
This is one of our advanced production deep dives. Pro unlocks the rest of it, every other deep dive, and the See, Animate, Test and Build modes on every topic.
What it covers
Hash vs range vs directory sharding, the shard-key checklist, consistent hashing in C#, dual-write resharding, and when managed Cosmos DB / SQL Hyperscale beats DIY.