Partition the data
One database holds more data or takes more writes than a single node can handle.
Say it like this
Writes outgrow one node, so I partition by user id with consistent hashing, keep every hot-path query on a single shard, and treat cross-shard queries as a batch concern.
Basic solution
Split rows across shards by a key (user id, tenant, hash of id). Each shard is a smaller database.
Routing a request to its shard
The router hashes the shard key and sends the request to one shard. A query without the key has to be scattered to every shard and gathered.
How it works
- Choose a shard key that appears in every hot-path query: user_id for a chat inbox, alias for a URL lookup. The key is the one value the router sees on every request, so a query that does not carry it cannot be sent to a single shard.
- Map the key to a shard. With hash sharding the router computes hash(key) mod N, which spreads keys evenly across the N shards; with range sharding a lookup table says "keys A–F live on shard 1", which keeps neighbouring keys together so a range scan stays on one shard.
- A router (a client library, a proxy such as Vitess, or the client itself) computes the shard for each request and opens a connection to that shard only. The other N−1 shards never see the request.
- Each shard is an ordinary database with its own primary, replicas and backups. Nothing inside a shard knows the others exist.
- Queries that need many shards (reports, search) are scattered to all shards and the replies gathered and merged by the router, or served from a separate derived store written for that query. Neither belongs on a user-facing request path.
What the shard key must do
- Spread load evenly: no single value should receive a large share of traffic, otherwise its shard becomes the hot one.
- Keep related rows together: a conversation and all its messages on one shard, so loading a chat is one query to one place.
- Be known at query time: the router can only route a query that includes the key, so every hot-path query must carry it.
Worked example
Chat app message store
- 50M DAU × 40 messages/day = 2B messages/day ≈ 23K writes/s average, ~70K at peak. One database node cannot absorb that write rate, which is the reason to shard at all.
- One Cassandra node comfortably takes ~10K writes/s, so about 8 nodes for peak plus headroom, times 3 for replication = 24 nodes. Across the cluster each key is stored three times, on three different nodes.
- Shard key: conversation_id. A conversation and its messages land on one partition, so "load the last 50 messages" is a single-partition range read that touches one node.
- Storage: 2B × 200 B ≈ 400 GB/day of messages, 1.2 TB/day once each is stored three times; 24 nodes × 2 TB = 48 TB holds ~40 days before tiering to cold storage. That is the retention window the cluster size buys you.
- The one query that breaks the model is "all messages by user X across chats", because user X is not the shard key. It is served from a separate user_messages table written alongside (denormalised) with user_id as its key, not by scanning every shard.
Variants and when each wins
Hash sharding
- The router hashes the key and takes the remainder modulo the shard count, so keys spread evenly by construction and the shard is cheap to compute. It wins when every query is a lookup by key; range scans across keys are impossible because neighbouring keys are scattered.
Range sharding
- Keys are sorted and cut into contiguous ranges, with each range on one shard, so a scan over a time window or a name prefix stays on one shard. It wins for time-series and alphabetical scans; the newest range becomes a hot shard unless keys are spread.
Directory (lookup table) sharding
- A small table maps each key or tenant to a shard, and the router consults it before every request. It wins when you need to move individual tenants or isolate a big one; the directory is another dependency to keep consistent and available.
Geographic sharding
- Data is partitioned by region so a request is served entirely inside the region it came from. It wins for latency and data-residency rules; users that travel or objects that move need a home-region rule.
Pick hash sharding with a stable key by default; range when time-ordered scans dominate; directory when you need to move individual tenants.
Failure modes
Hot shard
- One shard sits at 90% CPU while the rest are idle. It happens because the key is skewed: a celebrity, a popular tenant, or a time-ordered key that sends every new row to the same shard. Fix it by salting the key so one logical key spreads over several partitions, splitting the partition, or moving the hot entity to its own shard.
Cross-shard query
- A query that looks simple takes seconds, because the router had to send it to all N shards and merge the replies. It happens because the query does not contain the shard key, so the router cannot pick one shard. Fix it by denormalising the data into a second table keyed the other way, or by serving that query from a search or analytics store built for it.
Resharding downtime
- Adding shards needs a migration window during which writes pause. It happens because hash mod N remaps most keys when N changes, so almost every row has to move. Fix it with consistent hashing, which moves only about 1/N of the keys, or by pre-splitting into many logical shards (say 1,024) mapped to a few physical nodes, so growth means moving logical shards one at a time.
Cross-shard transaction
- A transfer between two users on different shards half-succeeds: one side is debited and the other never credited. It happens because no transaction spans two shards; each shard commits on its own. Fix it by keeping both sides on one shard by design, or by running a saga, a sequence of per-shard steps each with an undo step and an idempotency key so retries are safe.
Uneven growth
- One shard runs out of disk while the others are half empty. It happens with tenant-based keys when tenants have wildly different sizes, so one tenant fills its shard alone. Fix it by monitoring per-shard size and splitting or moving the largest tenants early, before the disk is full.
Interviewer follow-ups
Answer out loud before you open each one.
How do you pick the shard key?
Start from the top two or three queries on the hot path and pick the column they all filter on. Then check it spreads load: high cardinality, no single value with a large share of traffic. If the two conflict, favour the hot path and handle the skew with salting or a dedicated shard.
How would you add a shard without downtime?
Pre-split into many logical shards from day one and map them to physical nodes in a directory. Adding a node means moving a few logical shards: copy, dual-write during the move, flip the directory entry, then clean up. Consistent hashing gets a similar result for key-value stores.
What happens to a query that needs all users?
It becomes scatter-gather across every shard, which is fine for an offline report but not for a request path. For anything user-facing I maintain a derived table keyed the other way, or push the data to a search or analytics store built for that query.
How do joins work across shards?
They do not, in general. The usual answer is to co-locate the rows that are joined (same shard key), denormalise the joined fields into one row, or do the join in the application after two single-shard reads. Say which one applies to the specific query.
Connections
- In study paths
- Splitting the data
- Built on
- Consistent hash ring · Geohash and hierarchical cells (H3, S2)
- Pairs with
- Add nodes without remapping everything · Fix a hot shard · Spread reads across replicas · Precompute joins
- Pulls against
- Commit across two stores · Default to a relational database
- Used in
- URL Shortener (TinyURL / bit.ly) · Chat App (WhatsApp / Messenger) · Ride Matching (Uber / Lyft)
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon EC2 Auto Scaling | Service, Shard router | Compute Engine managed instance groups | Virtual Machine Scale Sets | Kubernetes Deployment with a HorizontalPodAutoscaler, Nomad |