Knowledge Guide
HomeSystem DesignScalable Systems (Advanced Topics)

hard Sharding Strategies: range vs hash vs directory vs geo

One placement function, one central tension

Sharding maps every row to one of N shards through a placement function shard = f(key). The shape of that function decides one tension that governs everything else: range-query locality vs even load distribution. A function that keeps neighbouring keys together makes ordered scans cheap but concentrates sequential writes; a function that scatters neighbouring keys spreads load evenly but destroys ordered scans. You cannot have both from a single function — picking where you sit on that axis is the core decision, and directory and geo sharding are the two ways to escape the axis by placing data by policy instead of by key shape.

An axis with RANGE (ordered intervals, great scans, hotspot risk) on one end and HASH (scrambled, even load, no scans) on the other, plus DIRECTORY and GEO as orthogonal policy-based choices
An axis with RANGE (ordered intervals, great scans, hotspot risk) on one end and HASH (scrambled, even load, no scans) on the other, plus DIRECTORY and GEO as orthogonal policy-based choices

The four strategies, mechanism-first

The same six keys placed under range sharding (contiguous, a 20-45 query hits two adjacent shards) and hash sharding (scattered, the same query must fan out to all shards)
The same six keys placed under range sharding (contiguous, a 20-45 query hits two adjacent shards) and hash sharding (scattered, the same query must fan out to all shards)

Traced example: the same six keys under each strategy

Take six user rows with keys 10, 25, 42, 57, 73, 88 in a key space of 0–99, and N = 3 shards. Watch where each strategy puts them, and what the range query "keys 20–45" costs.

StrategyPlacement of 10 25 42 57 73 88Query "keys 20–45"
Range [0,33) [33,66) [66,99]S1: 10, 25 · S2: 42, 57 · S3: 73, 88touches S1 + S2 only (contiguous) — no scatter
Hash h(key) mod 3S0: 25, 73 · S1: 42, 88 · S2: 10, 57 (even 2/2/2){25,42} live in S0 and S1, but you can't know which — scatter to all 3
Directory tablewhatever the table says — e.g. pin heavy user 88 alone on S3range not localized unless the inner scheme is range; extra hop per lookup
Geo by regionEU: 10, 42 · US: 25, 57 · APAC: 73, 88 (by user location)irrelevant unless the query is region-scoped; cross-region = scatter

The contrast is the whole lesson: under range the query reads two adjacent shards and stops; under hash the same query cannot be localized at all, because hashing deliberately destroyed the ordering the query relies on. Range trades even load for locality; hash trades locality for even load.

Now grow the cluster N=3 → N=4

Under naive hash mod N the divisor changes, so the residue of almost every key changes: roughly (N−1)/N of all rows must move — a near-total migration while serving traffic. Under consistent hashing the new node captures only its ring arc, so only ~K/N keys relocate. This is why elastic hash-sharded stores (Dynamo, Cassandra) use a ring with vnodes, never raw mod N.

Pitfalls

Selection & trade-offs — how a senior engineer decides

Start from the access pattern, not the technique. Two questions settle most of it: do my hot queries scan ranges of the key or look up single keys? and does the cluster grow and shrink often? The first question is the range-vs-hash axis; the second is why naive hash-mod is almost always wrong.

StrategyChoose whenAvoid whenReshard costRange scans
Rangeordered scans dominate (time-series, dashboards, "last 7 days"); key is non-monotonickey is monotonic → hotspotLow — split/merge the affected intervalExcellent
Hash (consistent + vnodes)point lookups on an elastic KV store (user-by-id)you need ordered scansMinimal — ~K/N keys moveNone
Directoryper-tenant / arbitrary placement, whales, uneven shards (multi-tenant SaaS)extra hop or SPOF unacceptableLow — remap moved bucketsDepends on inner scheme
Geodata-residency law, latency locality (compliance)access is heavily cross-regionMigrate a region's dataWithin region

Crisp rules of thumb. Time-series → range (you always scan by time; just avoid a raw monotonic write key). User-by-id / session store → hash (point lookups, elastic membership; you forfeit scans you never needed). Multi-tenant SaaS → directory (you must place tenants by hand and isolate whales; pay for a highly-available lookup layer). Compliance / regional latency → geo (residency is a hard constraint that overrides the axis; accept cross-region scatter). When one axis cannot satisfy residency, elasticity, and locality together, layer them (geo on the outside, consistent hashing inside) and accept the doubled operational surface.

Takeaways


Re-authored for this guide; both diagrams hand-authored as SVG. Sources: Martin Kleppmann, Designing Data-Intensive Applications, ch. 6 (Partitioning); DeCandia et al., Dynamo (SOSP 2007); Karger et al., Consistent Hashing (STOC 1997); Apache Cassandra docs (virtual nodes). See also: Data Sharding Techniques, Online Resharding & Cross-Shard Operations, Consistent Hashing, Shard Key: Hot vs Skew.

🤖 Don't fully get this? Learn it with Claude

Stuck on Sharding Strategies: range vs hash vs directory vs geo? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Sharding Strategies: range vs hash vs directory vs geo** (System Design) and want to truly understand it. Explain Sharding Strategies: range vs hash vs directory vs geo from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Sharding Strategies: range vs hash vs directory vs geo** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Sharding Strategies: range vs hash vs directory vs geo** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Sharding Strategies: range vs hash vs directory vs geo** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes