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.
- Compute a deterministic hash from the shard key (string or number).
- Convert the hash to an integer.
- Compute
hash % num_shardsand use the remainder as the shard index.
- 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.
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.- 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:
- Same input → same output (so every node can compute shard locally).
- Nearby inputs map to very different outputs (helps spread keys evenly).
- Compute
hash(user_id). - Calculate
hash % num_shards. - Use the remainder as the shard index.
- user 41 → hash → large number → modulo 4 → remainder 3 → shard 3
- user 18 → hash → large number → modulo 4 → remainder 0 → shard 0

- 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).

- 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.

- Pros: meets latency and legal requirements; keeps data local.
- Cons: uneven keys per region; needs a second-level distributor (often hashing) inside each region.

- 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.

- Python hashlib docs: https://docs.python.org/3/library/hashlib.html
- Python
hash()docs: https://docs.python.org/3/library/functions.html#hash - MD5 background: https://en.wikipedia.org/wiki/MD5
- MurmurHash: https://en.wikipedia.org/wiki/MurmurHash
- xxHash: https://github.com/Cyan4973/xxHash
- CityHash: https://github.com/google/cityhash
- MongoDB: hashed sharding overview: https://www.mongodb.com/docs/manual/tutorial/hashed-sharding/