Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced18 min

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:

  1. 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.
  2. 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.
  3. 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

Single-Instance vs Sharded Architecture
CapabilitySingle Instance (Replicated)Sharded Cluster
Write ScalingLimited by the capacity of the single leaderMultiplied horizontally across N shard masters
Joins & ConstraintsFull relational joins and foreign keys enforcedCross-shard joins and foreign keys are impossible or very slow
TransactionsFast, single-node ACID transactionsRequires slow distributed two-phase commit (2PC)
Operational BurdenLow: standard backup, migration, and monitoringHigh: rebalancing, split partitions, shard key skew

Sharding by key

users · all records

user 101
user 102
user 103
user 104
user 105
user 106
Router · shard = user_id % 3

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.

    01What is a 'scatter-gather' query in a sharded database architecture?
    02Why are cross-shard transactions considered a major architectural headache?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson