Sharding: Shard Keys, Cross-Shard Queries, and Reality
Understand the idea
Why sharding is the ultimate last resort of database scaling, not an easy first move.
The idea
When your data or write volume becomes too massive even for the biggest server available, databases turn to . Sharding means splitting rows of a table across multiple completely separate database servers (shards), where each server holds only a portion of the total data.
The key to sharding is the . Every row has a designated key (such as user_id or account_id). A router runs a simple formula (like user_id % number_of_shards) to send queries straight to the specific server holding that user's records.
Sharding solves massive write traffic, but it brings tricky challenges:
- Single-Shard Queries: Fast and simple! If a query includes the shard key (like finding orders for user 101), the router sends it straight to the one server with that data.
- Cross-Shard Queries: Slow and complex! If you search for something without the shard key (like searching all users in a specific city), the database has to do a : sending the query to every single shard, waiting for all of them to respond over the network, and stitching the results together.
- Cross-Shard Transactions: Trying to update data on two different shards at the same time requires complex two-phase coordination that can slow down your app.
What changes
| Capability | Single Instance (Replicated) | Sharded Cluster |
|---|---|---|
| Write Scaling | Limited by the capacity of the single leader | Multiplied horizontally across N shard masters |
| Joins & Constraints | Full relational joins and foreign keys enforced | Cross-shard joins and foreign keys are impossible or very slow |
| Transactions | Fast, single-node ACID transactions | Requires slow distributed two-phase commit (2PC) |
| Operational Burden | Low: standard backup, migration, and monitoring | High: rebalancing, split partitions, shard key skew |
Sharding by key
users · all records
Shard 0
Empty
Shard 1
Empty
Shard 2
Empty
Start with one collection of six users.
01 / 05Sharding by key
The command
Working demonstration of single-shard routing vs scatter-gather
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
The mistake
Check the result
Run the example to see how a lookup by shard key hits exactly 1 shard, while querying by city requires scattering the query across all shards.
Guided challenge
Detect scatter-gather queries
Update the function so it returns true when queryHasShardKey is false (scatter-gather needed) and false when true.
Detect scatter-gather queries
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
EXPECTED RESULT
false true
Hints 0/1
Use all hints or make 3 failed checks (0/3).
Free exploration
Change this example and run it again. Each run is a new process. Nothing is saved.
Your variation
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.