Skip to content

Partition data without hiding hot keys

12 min reading

Choose partition keys and reason about skew, movement and rebalancing

Page details

System Design · Store and find data

12 min reading · 25 min with practice

Sources

Published · Last edited:

Prerequisite: Replicate data and observe lag. For basics, use the independent System Design foundations.

More
20px

Hypothetical scenario. Northwind proposes dividing products among storage owners. Dev shows an even distribution of identifiers. Maya places the promoted product on the board and traces most incoming requests to the one owner holding it. The record counts look balanced; the work does not.

Dev: “Every owner has a similar number of products.”

Priya: “Which owner receives the requests for this promotion?”

Choose a partition key against the existing access sheet, not a generic scale target. Distinguish record placement, request concentration and the responsibility that moves during rebalancing. This remains a design exercise; the selected catalogue rate alone has not justified distributed storage.

Which resource or operation needs to be divided?

Northwind’s selected workload has fifteen catalogue requests per second during the busy interval. Nothing in that number alone establishes a need for distributed storage. Partitioning is introduced here as a design exercise and a possible future response to a measured resource boundary, not an emergency inferred from an arbitrary request count.

A store might outgrow its memory, storage capacity, write processing or serving throughput. Different boundaries call for different changes. Replicas can help selected reads without dividing authoritative write ownership. An index can reduce work per query without adding nodes. Partitioning should address the constrained resource after those distinctions are understood.

Retain the earlier access sheet: product detail selects identity and market/currency; category and promotion queries select bounded ordered groups. A key that makes detail lookup local may spread one category across many owners. A key that groups a seller’s records can make that seller’s listing local while concentrating a large seller’s workload.

Choose placement and location together

Range partitioning assigns intervals of a key space to owners. It can preserve useful locality for range queries, while a heavily accessed interval can become hot. Hash partitioning transforms a key into a placement space. It can distribute many distinct keys, but it does not divide the work of one key simply because the hash function is well behaved.

A routing map connects the application’s key to the responsible partition. It may be consulted through metadata, cached by clients or implemented inside a database’s coordinator. Whichever mechanism is selected, the system needs a policy for stale routing information and for discovering a changed owner.

Consistent hashing organizes placement so that adding or removing capacity can move a subset of keys rather than changing a naive modulo result for nearly every key. Virtual nodes can distribute ownership units across physical nodes. Neither technique guarantees equal traffic when keys have unequal demand.

Even records can produce uneven trafficA routing map directs product keys to three owners. Each owner holds an illustrative equal record count, but a promoted hot key sends most of the selected requests to owner A. More key ranges do not split one indivisible key.Key → routing mapPlacement ≠ demandOwner AHot productOwner BOther keysOwner COther keys
Measure the busiest owner, not only the average owner.

Move a partition through an ownership protocol

Hypothetical scenario. Move the range containing Q7 from owner A to owner B while product changes continue. Copying its current rows is only the snapshot stage. B also needs changes accepted after the snapshot boundary and an explicit authority cutover.

Move a partition through an ownership protocol — selected design evidence
Migration stateWriterCompletion evidence
Copying epoch 7ASnapshot boundary recorded
Catching upACaptured changes reach boundary
Cutover epoch 8B onlyA rejects stale writes; directory advances
Partition movement changes writer authority only after snapshot and change capture are accounted for.Partition movement changes writer authority only after snapshot and change capture are accounted for. Copy + capture to Catch up: replay later changes; Catch up to Fence + switch: boundary reached.Copy + captureCatch upFence + switchreplay later changesboundary reached
Partition movement changes writer authority only after snapshot and change capture are accounted for. Enlarge the diagram to inspect its labels; the surrounding records and explanation give the text alternative.

Use a routing epoch with one admitted writer per range. During copy, A remains authoritative and captures subsequent changes. B catches up to a stated boundary; cutover prevents old-epoch writes, advances routing and admits B. Readers may follow a bounded redirect while stale routers learn the new epoch. A directory is useful only if the owner enforces the epoch rather than trusting every router to be current.

Hash placement spreads many keys statistically, whereas range placement preserves locality and can concentrate a popular range. Virtual nodes improve placement granularity, not the service rate of a single unsplittable hot key. Migration itself consumes disk and network resources; throttle it against foreground latency and recovery headroom. Rolling back after B accepts writes requires carrying those writes back or preserving B as authority.

Place actual keys before moving an owner

Hypothetical scenario. Use a toy ring with integer positions 0 through 99. Assign tokens 20 to A, 60 to B and 90 to C. A key belongs to the first token encountered clockwise, wrapping after 99. The chosen hash outputs are Q7 → 12, R2 → 35, S4 → 55, T8 → 75 and U9 → 95; these are illustrative inputs, not outputs of a named hash function.

Primary placement before and after adding one token
Key / positionOriginal ownerWith D at 40
Q7 / 12A / 20A / 20
R2 / 35B / 60D / 40
S4 / 55B / 60B / 60
T8 / 75C / 90C / 90
U9 / 95A / 20 after wrapA / 20 after wrap

D takes positions (20,40], previously assigned to B: 20 of 100 positions, or 20 ÷ 100 = 20% of this discrete space. One of the five selected keys moves, also 1 ÷ 5 = 20%; that agreement is constructed, not a prediction for arbitrary records. Byte movement is the sum of affected record sizes and catch-up changes. Request movement depends on those keys’ demand.

A virtual-node scheme assigns several tokens to each physical owner. For example, adding D at both 40 and 80 takes (20,40] from B and (60,80] from C: 20 + 20 = 40 positions. More tokens give finer placement choices but do not split Q7 itself. Cassandra’s placement documentation supplies the real token-ring mechanism; this small ring deliberately omits replica placement. Apply the ownership-transfer protocol above before admitting writes at a new owner.

Derive the hot owner’s demand

Hypothetical worked example. Choose three owners and a separate component workload of fifteen single-key reads per second. This is a selected placement exercise, not a conversion from fifteen catalogue HTTP requests: listings can retrieve several product records. Suppose one promoted key attracts nine of these key reads per second. The remaining six reads per second are evenly spread across the owners. Each receives 6 ÷ 3 = 2 background reads per second.

The owner of the promoted key receives 9 + 2 = 11 reads per second; the other two receive two each. The sum is 11 + 2 + 2 = 15. Average demand is 15 ÷ 3 = 5, which hides the owner receiving eleven. Even record counts would not change that calculation.

Now choose six owners while leaving the key indivisible. Background demand becomes 6 ÷ 6 = 1 per owner, but the hot owner still receives 9 + 1 = 10. Doubling owner count reduces that owner’s selected demand from eleven to ten, not to the average of 15 ÷ 6 = 2.5.

This does not prove that the hot owner is overloaded; no owner capacity has been measured for this storage operation. It demonstrates why an average is insufficient evidence. Candidate remedies depend on the resource: eligible read caching or replication can help some reads, while distributing one authoritative update may require a different data model.

Account for queries that cross ownership

If category membership spans owners, the application or database may need to request candidates from several partitions and merge them into the declared order. Requesting twenty items from every partition is different from obtaining the globally first twenty eligible results. Selection, filtering and merging must preserve the original contract.

With three partitions and a selected twenty candidates from each, the merger can receive 3 × 20 = 60 candidates before producing twenty results. That is an illustrative intermediate cardinality, not a universal distributed-query algorithm. Skew, eligibility filtering and continuation can require a different number of candidates or another access representation.

A category-specific read view can keep that access local, but it brings back the maintenance cost from the data-model lesson. Partitioning does not eliminate the relationship; it changes where the system pays to assemble it. Include that cost in the latency and freshness decision.

Moving data also moves responsibility

A rebalance can copy records to a new destination, catch up changes, establish a handoff point and update routing. Copy completion is not sufficient if both old and new owners can accept incompatible authoritative writes. The transition needs an explicit rule about who may accept which operation and how stale clients are handled.

For the current read-only catalogue interface, stale routing might produce an unavailable or older observation. The seller update path has stronger consequences: an acknowledged change must not vanish because it was sent to an owner whose authority had ended. Later correctness lessons develop operation identity and fencing; here record the required exclusion behavior.

In the wild: Cassandra exposes the partition choice

Apache Cassandra’s architecture documentation describes its partitioning and replication approach. It provides a real example of key placement, versioned changes and membership working together. Read the selected product’s semantics rather than treating every ring-based diagram as the same system.

The documentation supports examining those mechanisms. It does not establish Northwind’s selected workload, nor imply that adopting Cassandra is necessary for the fifteen-read-per-second exercise. Keep the architecture decision tied to demonstrated constraints and the team’s operational ability.

When a move creates two apparent owners

Hypothetical failure scenario. Northwind copies a product range to a new owner and updates most routing caches. An old client continues sending price changes to the previous owner, which still accepts them. The new owner serves a different history. A completed copy and a mostly updated map did not establish a safe authority transfer.

The repair must define the handoff and enforce it at a boundary stale callers cannot bypass. Forwarding, rejecting obsolete ownership or an implementation-specific transfer protocol may be appropriate. Merely reducing the routing cache lifetime leaves a period where the old writer can still act.

Partition when the boundary is real

Partitioning is useful when the dataset or workload must be divided and the chosen key can divide the constrained work. It adds routing, movement and cross-partition coordination. Keep one simpler store where measured needs and failure requirements allow it.

Northwind now has a placement proposal and explicit hot-key and listing consequences. The next lesson composes placement with the replication boundaries from Page 14. A distributed key-value store will need both answers: where the key belongs and what its copies are allowed to return.

Challenge the proposed mechanism

B is ready, but a router still sends an epoch-7 write to A. Which component must reject it?

Reveal the design reasoning

A must enforce the ownership transition or forward through a protocol that preserves the new authority. Eventual routing refresh alone cannot prevent two writers. Include lost cutover replies and a post-cutover rollback in the review.

Decision and reconsideration: Use epochs and one authoritative cutover per partition. Reconsider placement separately from hot-key replication or application changes.

Challenge the average

  1. Three owners average five reads per second. Is five the busiest owner’s demand?

    Place the hot key explicitly

    No. The selected hot owner receives nine promoted-key reads plus two background reads, or eleven. The average conceals the resource that may constrain the design.

  2. Will six owners divide the nine reads to one key into six equal shares?

    Separate keys from operations on a key

    No. Under indivisible placement, one owner still receives the nine reads. Only the background share falls to one, leaving ten on that owner.

  3. Records are copied and routing is updated. May the old owner keep accepting changes?

    Establish the handoff contract

    Only if the selected transfer protocol explicitly makes that safe. Otherwise stale callers can create divergent histories. Copying data and transferring authority are separate actions.

The partition map identifies an owner, and replication adds copies. Page 16 combines those mechanisms into a key-value design and asks what a read or uncertain write is allowed to conclude.

Northwind decision log

Choose a key against the measured resource and declared query locality.
Evaluate hot-owner demand and cross-partition intermediate work explicitly.
Require an enforced ownership handoff before treating a rebalance as complete.

Go deeper: placement and skew