Course contents 03 / 35

03 / 35 · Beyond One Machine

Partitioning and rebalancing

Choose a key for point lookups and range scans, then follow the data moved when a node joins.

The sales service keeps one record per order. Looking up order 106 should reach its storage node directly. A report for one trading day should read the orders of that day. The key that helps one request may spread the other over the whole cluster.

A key determines a part#

Partitioning assigns different records to different logical parts. Routing has two steps. A function maps a key to a part. A placement map tells us which node currently serves that part. A node can serve several parts. Moving a part changes placement while the key-to-part rule can stay fixed. Replication makes extra copies of a part and will enter in the next note.

In range partitioning, each part owns a contiguous interval of ordered keys. If the key begins with the trading date, a one-day scan can identify the intervals to visit. The half-open intervals [a,b)[a,b) and [b,c)[b,c) share a boundary without assigning the key bb twice. A write-heavy current day can concentrate work on one interval. Splitting it into smaller ranges only helps if the traffic reaches several of them. The original Bigtable uses lexicographically ordered row keys and divides the key space into ranges called tablets. (Chang et al., §§2 and 5)

In hash partitioning, we map the key through a hash function and assign an interval or bucket in its output space. Nearby order IDs need not reach nearby parts, so a range of order IDs generally requires a wider scan. With a stable hash and the current map, an exact order ID has one destination. A good distribution of distinct keys can spread writes. It cannot split the writes made to one very popular key. (DeCandia et al., §4.2)

Keep the number of logical slots fixed#

Consider twelve equal-sized toy records with keys 0 through 11. Use h(k)=kh(k)=k only to make the arithmetic visible. With three nodes numbered from 0, the rule h(k) mod 3h(k)\bmod 3 assigns four records to each. Switching to h(k) mod 4h(k)\bmod 4 leaves only keys 0, 1 and 2 with their old owner. The other nine must move. A hash of real keys can still suffer this broad remapping when the divisor changes.

Instead keep twelve logical slots and set slot(k)=h(k) mod 12\mathrm{slot}(k)=h(k)\bmod 12. Initially give four slots to each of A, B and C. Give D slots 9, 10 and 11 after it joins. Each old owner gives away one slot, and all four nodes end with three. Only three records move in the example. The same reasoning applies to bytes: the transfer volume is the sum of the sizes of the moved slots. Equal slot counts imply equal bytes only in the toy setup.

Figure

Three slots change owner

Three slots change ownerThe slot function stays fixed. Only slots 9, 10 and 11 move to D.BeforeAfterA0, 3, 6, 90, 3, 6B1, 4, 7, 101, 4, 7C2, 5, 8, 112, 5, 8D–9, 10, 11
The slot function stays fixed. Only slots 9, 10 and 11 move to D.

Redis Cluster uses a fixed hash-slot space and reassigns slots among nodes. Dynamo’s consistent-hashing ring is another design. A node can own several token ranges, and a membership change transfers selected ranges. In both cases the placement rule aims to move less data than a fresh modulo over the node count. The amount moved depends on what the transferred slots or ranges contain. (Redis Cluster Specification, DeCandia et al., §§4.2 and 6)

Move ownership while writes continue#

Suppose slot 9 moves from A to D. A can serve requests while D copies a snapshot and catches up with updates after that snapshot. Before switching routing, the service must establish which writes D has applied and prevent A from accepting writes under the old assignment. One simple implementation pauses writes to the slot briefly, copies the tail, then publishes a new version of the placement map. Requests using an older version have to refresh or be redirected. Sending new writes to D before that boundary could make a later read miss an update still accepted by A. Redis Cluster’s actual migration protocol uses per-slot states and redirects. The snapshot-plus-tail sequence is a separate example. (Redis Cluster Specification, Live reconfiguration)

A placement map also needs a load measure. Suppose 600 of 1000 writes target one store key. All 600 go to one part if the store ID is the key. Moving that part changes its host and leaves the load concentrated. Splitting a store’s keys by a suffix derived from order ID can spread those writes. A lookup with the order ID can compute the suffix. A store-wide report now needs every suffix and must merge their results. Choose the key from the requests and the workload, including how unevenly they occur.

Next#

Placement tells us where a part is served. If its only copy disappears with a node, routing has nowhere useful to send a retry. Replication and failover adds copies and explains which ones can take over safely.

References#

  1. Fay Chang et al. Bigtable: A Distributed Storage System for Structured Data. OSDI, 2006, §§2 and 5. Ordered row keys and range-based tablet placement.
  2. Giuseppe DeCandia et al. Dynamo: Amazon’s Highly Available Key-value Store. SOSP, 2007, §§4.2 and 6. Consistent hashing, virtual nodes and movement under membership changes.
  3. Redis. Redis Cluster Specification. Hash slots, live reconfiguration and routing changes.