Sharding and consistent hashing
We divide ownership only after understanding a working database. More machines add routing and migration decisions alongside their capacity. We will trace partition placement, a safe ownership change, and the complete score command with a reconciled storage budget.
#Section 31Partitioning and sharding solve different placement problems
Heron’s old event history makes maintenance cumbersome even when recent requests are small. Partitioning divides one logical data set into subsets under a rule, such as match or time. Sharding assigns subsets to separate database owners, commonly on separate machines. A partition can remain local; dividing a table does not by itself distribute its writes.
Time partitions can make retention efficient: remove an expired subset instead of deleting individual old rows. They can also let a planner avoid irrelevant subsets. That partition pruning requires predicates and metadata that identify the relevant partition; an unbounded query can still touch them all.
A sharded match lookup needs routing from match identity to owner. A transaction confined to one owner can retain a local boundary. A cross-owner operation needs another protocol, an explicit weaker invariant, or a redesigned boundary. The network work is part of the design rather than an implementation detail to hide.
Start with the measured resource ceiling: storage, write CPU, lock contention, or another bottleneck. Read replicas may help a read bottleneck without adding independent ownership. Sharding can divide suitable writes but brings routing, backups, cross-shard queries, and ownership transitions that the single database did not need.
#Section 32Partition keys determine the request’s working set
A viewer supplies a match identifier, so routing all of that match’s events to one owner keeps its recent-history query local. A partition key is the value used to select the data subset or owner. Its job is to make important operations bounded while spreading enough independent demand.
Hashing match identities can distribute matches that have unrelated identifier patterns. A range partition groups ordered intervals instead, making corresponding range scans local. Hashing improves spread under suitable inputs; ranges preserve useful locality but can concentrate newly growing keys. Neither changes the popularity of one particular match.
Suppose the illustrative shard request rates are 100, 100, and 800 per second. They total 1,000, but the mean of 333.3333, rounded to four decimals, hides the owner receiving 800. Equal record counts do not imply equal demand. Measure placement by bytes, requests, write contention, and the largest indivisible item.
A key should also fit constraint scope. A uniqueness rule within a match can be enforced locally when all its events share an owner. A global user-name uniqueness rule cannot be inferred from independent per-shard constraints. Route it through a suitable authority or choose a protocol that explicitly enforces it.
#Section 33Resharding is a transfer of authority
A shard grows too large and a new owner is ready. Resharding moves data or changes partition ownership. Copying bytes is only the first task: writes arriving during the copy must reach the destination, and the cutover must establish which owner may accept the next write.
An illustrative snapshot contains 1,000,000 records and copies at 10,000 records per second, taking 100 s if that rate stays constant. A change stream carries writes after the snapshot boundary. If arrivals are 20 changes per second but catch-up applies only 15 for 60 s, backlog grows by changes.
When catch-up can apply 40 per second while arrivals remain 20, spare rate is 20 and those 300 changes drain in 15 s. Catch-up must be faster than the continuing change rate to close the gap. An owner that copies successfully but never catches up is not ready to serve current truth.
At cutover, coordinate a named history boundary, prevent old ownership from accepting conflicting writes, and publish routing. A fencing epoch is an increasing ownership value checked at the write resource. Install the new epoch atomically at the protected resource before relying on it to reject the old owner's requests. Issuing an epoch elsewhere does not update storage by itself.
Keep old data until verified recovery and cleanup conditions hold. Retried commands carry their original identity across the migration. A route cache can be stale, so the old endpoint must reject or redirect safely instead of quietly continuing independent ownership.
#Section 34Hot partitions preserve their heat after hashing
A final attracts most viewers to one match. A hot partition is a data subset whose demand overwhelms its owner. Hashing its identifier produces another owner label, not more independent capacity for that one ordered stream. Adding idle owners elsewhere does not split the hot request's contract.
Read copies and caches can spread permitted reads when freshness requirements allow them. Batching can reduce repeated per-event overhead. If an illustrative transaction groups 10 ordered events, it can share fixed transaction costs, but it also delays acknowledgement and keeps the group within its chosen atomic boundary.
Splitting one match across buckets changes the read path. Recent events may come from one bucket, while a history scan merges several. Splitting concurrent writes can also change sequence ownership and invariants. First identify whether the pressure is replaceable reads, append work, one contested row, or ordered authority.
A scatter-gather query sends work to several owners and combines results. Its latency and availability now depend on the required replies, and its global ordering may need merging. A small final page can hide a large candidate scan. Bound per-owner work and state what an unavailable shard means to the completeness of the result.
#Section 35Consistent hashing changes a bounded part of ownership
A simple hash modulo the number of machines can remap many keys when that machine count changes. Consistent hashing puts owners and keys on an ordered ring and maps a key to a nearby owner under a stated rule. Adding an owner takes a neighboring interval rather than redefining every modulo result.
Use an illustrative ring numbered from 0 through 99, choosing the first owner clockwise at or after each key position. Owners A, B, C, and D sit at 0, 25, 50, and 75. Sample keys [10,30,55,58,70,90] map to [B,C,D,D,D,A], wrapping 90 to A.
Insert E at 60. Keys 55 and 58 now map to E; the others retain their owners. Exactly two of these six sample keys move, a sample fraction of one third. The interval length and this deliberately chosen sample do not represent an equal-share production ring.
Under an ideal uniform model with five equal owners, the new owner receives an expected one fifth of keys. That is an expectation, not the measured fraction for every ring or finite sample. Virtual nodes place several tokens for each physical owner to improve distribution and support weighted capacity, but do not eliminate workload skew or migration work.
#Section 36The complete command and its storage budget
The scorer submits command k7 with a payload fingerprint. Route it to the current match owner, verify the ownership epoch at the protected write boundary, validate permission, and begin a transaction. Atomically claim the command identity, update the score and sequence, store the event and result, and record publication intent.
An outbox is a database record of an external action to perform after the local transaction commits. Its publisher can retry without needing a cross-service atomic commit. The receiver still needs a stable event identity and duplicate handling. The outbox closes the crash gap between committing truth and remembering to publish, not the possibility of repeated delivery.
| Step | Owned state | Failure response |
|---|---|---|
| Route and fence | Current match authority | Reject or redirect stale owner |
| Claim command | Unique key and fingerprint | Return saved result or reject mismatch |
| Change and commit | Score, event, result, intent | Roll back group or reconcile lost reply |
| Replica application | Comparable committed position | Wait or route a fenced read |
| Publish outbox | Stable event identifier | Retry and deduplicate delivery |
For the separate illustrative storage workload, every daily API request creates a 500-byte record. Daily payload is bytes. Retaining it for 30 days gives 129,600,000,000 logical bytes, and three copies give 388,800,000,000 bytes. Reads do not secretly create rows; this is the specified write-sizing scenario.
Indexes, retained versions, WAL, outbox retention, backups, and free space are extra. Count them separately rather than claiming the logical total is a disk requirement. The next unit compares alternative data models; it does not remove the need to name identity, visibility, durability, and ownership for every stored fact.