Ch. 12 · MongoDB

MongoDB Shard Keys and Workload Distribution

MongoDB Shard Keys and Workload Distribution. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readadvancedupdated Oct 3, 2026

A shard key influences distribution and query routing. Good cardinality alone does not guarantee balanced writes or targeted reads.

Before you start

You should understand documents, collections and indexes. Sketch representative documents and the reads and writes they must support. MongoDB-specific examples assume a collection with the shown fields; deployment topology, permissions and existing indexes can affect the operational behavior being discussed.

The practical goal is to reason through this situation: A monotonically increasing key can concentrate new writes in a narrow range. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: Map query routing

List common filters and whether they can target shards.

Step 2: Inspect write concentration

High cardinality does not prevent a monotonically growing hot range.

Step 3: Evaluate future growth

Balance distribution, locality and changing tenant workloads together.

Worked scenario

A monotonically increasing key can concentrate new writes in a narrow range.

A timestamp shard key spreads historical ranges but new inserts concentrate at the latest end. A more distributed key can reduce that hotspot while scattering tenant queries. Neither dimension alone decides the design; include write rates, tenant size distribution and operational growth expectations.

Common mistake

Random distribution can scatter common tenant queries across shards.

Verify the behavior

Measure routing and per-shard load for representative skewed workloads.

Interview exercise

Evaluate a shard key.

Answer and reasoning

Measure write concentration, common filters, growth and routing behavior together, including future workload changes.

Continue learning

Compare the scenario with the MongoDB interview questions and test your understanding with the MongoDB MCQs. For terminology and implementation details, consult the reference material.

More in MongoDB

read ✓MongoDB · mid

MongoDB Bulk Writes

Batch inserts and updates with bulkWrite, choose ordered or unordered, and handle partial failures correctly.

~2 min readread →
read ✓MongoDB · mid

MongoDB Capped Collections

Use capped collections for fixed-size, insertion-ordered logs, and know why documents cannot grow.

~2 min readread →
read ✓MongoDB · hard

MongoDB Change Streams and Resume Tokens

React to inserts and updates with change streams, persist resume tokens, and handle invalidate and failover correctly.

~2 min readread →
esc