Skip to main content
We picked user_id as Chirp’s shard key. But what if the shard key is not numeric — for example an email or username? You can’t meaningfully divide chris@gmail.com by 4 and take the remainder. Instead you need a deterministic rule that maps any shard key (string or number) to an integer you can use to pick a shard. The rule must satisfy two properties:
  • Deterministic: the same input always produces the same output.
  • Well-distributed (avalanche effect): nearby inputs map to seemingly unrelated outputs so data spreads evenly across shards.
A function with these properties is called a hash function; its numeric output is a hash. Most languages provide hashing utilities — for deterministic, cross-process sharding prefer stable hashes from standard libraries or well-known hashing libraries. How sharding with hashing typically works
  1. Compute a deterministic hash from the shard key (string or number).
  2. Convert the hash to an integer.
  3. Compute hash % num_shards and use the remainder as the shard index.
Example: convert a user email to a shard (4 shards)
Important properties of good hashes for sharding
  • Determinism: the same input maps to the same shard so every machine can independently calculate where a key lives.
  • Avalanche effect: a small change in input produces a very different hash; this spreads nearby keys across shards and reduces clustering.
Conceptual example: one character change produces a very different hash
Avoid using Python’s built-in hash() for cross-process sharding Python’s built-in hash() includes a per-process random seed by default, so the same string can produce different values across interpreter restarts (a security measure). For deterministic, cross-machine sharding use hashlib or a stable non-cryptographic library such as MurmurHash, xxHash, or CityHash. See the Python docs for details.
Python’s built-in hash() is randomized between interpreter processes by default. For cross-process / cross-machine sharding use deterministic hashes from hashlib or a stable non-cryptographic hash library.
Production examples and guarantees
  • Many production systems use hashes that are stable across restarts and machines. For example, MongoDB supports hashed sharding where the shard key is hashed deterministically and those hash values determine placement: https://www.mongodb.com/docs/manual/tutorial/hashed-sharding/
  • Two useful guarantees from deterministic hashing:
    1. Same input → same output (so every node can compute shard locally).
    2. Nearby inputs map to very different outputs (helps spread keys evenly).
Chirp’s hashing flow (concrete)
  • Compute hash(user_id).
  • Calculate hash % num_shards.
  • Use the remainder as the shard index.
Example (conceptual):
  • user 41 → hash → large number → modulo 4 → remainder 3 → shard 3
  • user 18 → hash → large number → modulo 4 → remainder 0 → shard 0
Because this rule depends only on the shard key and the number of shards, any machine can compute it locally without coordination. Range sharding Range sharding assigns contiguous ranges of shard key values to each shard. It’s ideal when range queries are the common access pattern (e.g., date ranges). For example, if most queries request “all transactions for March” and you shard by month, that query touches only one shard.
A technical diagram illustrating range-based sharding of transaction data by month/quarter, showing a query for "March" routed into one of four shards labeled Jan–Mar, Apr–Jun, Jul–Sep, and Oct–Dec. Below each shard are database icons and sample tables listing transaction dates and amounts.
Trade-offs:
  • Pros: efficient for range queries; predictable placement for related keys.
  • Cons: hot shards when new writes concentrate in the current range (e.g., “today’s” data).
Directory sharding (lookup table) A directory maps each logical entity (customer, tenant, etc.) to a physical shard, allowing manual control. This is useful when tenants are skewed in size and you want to place a very large tenant on a dedicated shard.
A diagram of a directory-based database sharding scheme where an app looks up a directory table that maps customers (Acme, Birch, Cobalt, Globex) to shard numbers 0–3 and routes queries to the corresponding shard databases. The shards show record counts (Acme 50, Birch 50, Cobalt 50, Globex 50,000).
Trade-offs:
  • Pros: precise placement and easy to move a tenant to another shard.
  • Cons: every request must consult the directory (it must be highly available and low-latency); adds an operational dependency.
Geographic sharding Geographic sharding places data near users or where regulations require it (e.g., store EU user data in EU datacenters). Because country-level keys are often uneven, geographic sharding is commonly used in two layers: first split by region, then hash within the region.
A dark infographic of Europe showing doodle user avatars with ID badges (23, 18, 28) inside a dashed region, aligned to stacked database/server icons. Labels on the side note geographic sharding steps like "Region → Machine Group" and "User ID → Shard."
Trade-offs:
  • Pros: meets latency and legal requirements; keeps data local.
  • Cons: uneven keys per region; needs a second-level distributor (often hashing) inside each region.
Comparison: four sharding strategies
A dark-themed infographic comparing four data indexing options — Hash, Range, Directory, and Geographic — each shown in its own panel with a simple diagram. Captions under the panels explain typical uses like looking up profiles, querying ranges (dates), grouping by hand, and geographic queries.
Which method for Chirp? Chirp’s common pattern is opening a user’s profile and showing that user’s posts, and there is no strong region-binding requirement. Hashing the user ID satisfies Chirp’s needs: it spreads profiles evenly across shards and makes lookups simple and fast. Routing: who computes the shard? Routing the request to the correct shard can happen in several places:
  • Application servers compute the hash and connect to the chosen shard.
  • A proxy or router layer computes the shard and forwards requests.
  • A client library encapsulates the hashing and routing logic.
Whatever component runs the routing must implement exactly the same deterministic hashing rule so every request reaches the correct shard.
A diagram titled "Chirp stays with hashing" that shows user requests being hashed (user_id → hash → ÷4 → remainder) and routed to one of four database shards (0–3). It illustrates how the remainder of the hash determines which shard stores a user’s data.
Links and references

Watch Video