arthrop0d

Brief articles on varied topics.

Consistent Hashing Rings and Virtual Nodes

algorithmsdistributed systems

A consistent-hashing ring maps both keys and servers onto the same circular hash space. A key belongs to the first server encountered while moving clockwise from the key’s position, so adding or removing a server changes only nearby ownership rather than reshuffling every key.

The problem: changing the server set

Suppose a cache or key-value store has three servers and assigns each key with ordinary modulo arithmetic:

server = hash(key) % 3

This is simple and often reasonably balanced. But if a fourth server joins, the calculation becomes hash(key) % 4. The result changes for most keys, even though only one server was added. A cache loses most of its entries at once; a database-backed service may send a large burst of reads to the database while repopulating them.

The same problem occurs when a server fails and the divisor changes from four back to three. Almost every key appears to belong somewhere else. This widespread reassignment is called remapping: changing the server responsible for a key.

Consistent hashing exists to keep that remapping small when the membership of a distributed system changes. It does not prevent data from needing to move, and it does not itself copy or delete data. It provides a stable ownership rule that tells the system which keys should move.

The ring

A hash function turns input such as a string into a number from a large, fixed range. A consistent-hashing implementation treats that range as a circle: after the largest value comes the smallest value again. This circular structure is the ring.

For example, imagine a tiny ring containing values from 0 through 99. Hash a key onto that ring, and hash each server onto it too:

key "user:42"  -> 17
server A       -> 12
server B       -> 46
server C       -> 78

To find the owner of user:42, start at position 17 and move clockwise. The next server is B at 46, so B owns the key. A key at 90 wraps around the end of the ring and is owned by A at 12.

The region from the previous server position up to a server’s position is that server’s range, meaning the keys for which it is responsible. The ring has no special first or last server; wraparound makes the rule uniform everywhere.

When server D joins at position 30, only keys between A at 12 and D at 30 change ownership. They used to continue clockwise to B, but now stop at D. Keys in all other ranges still find the same server. If B leaves, the keys in B’s range continue clockwise to C. Again, the rest of the ring is unchanged.

This is the central property: a membership change affects the ranges immediately associated with the changed server, not every range on the ring.

Why one position per server is not enough

Putting one point per physical server on the ring can produce poor balance. Hashing is designed to spread individual values, but a small number of random points can still land unevenly. One server might own a huge arc while another owns a tiny one.

The usual solution is virtual nodes, often called vnodes. Instead of hashing each physical server once, the system hashes many distinct labels for it, such as A-0, A-1, and A-2. Each label places a separate point on the ring, but all those points refer back to the same physical server.

With 1,000 virtual nodes per server, the ring contains many small ownership ranges. A server owns the ranges ending at all of its virtual-node positions. Random unevenness is averaged across those many samples, so total ownership is usually much closer to the desired share.

Virtual nodes also make membership changes less lopsided. When a new server joins, it receives many small ranges spread around the ring instead of one potentially enormous contiguous range. Those ranges are taken from several existing servers, so the work and traffic are distributed among them.

If there are N equally weighted servers, a joining server should receive roughly 1 / (N + 1) of the ring’s key space. A leaving server’s ranges, roughly 1 / N of the space, are redistributed among the survivors. The exact amount varies because hash placement is probabilistic, but it is far smaller than remapping nearly all keys with modulo-based assignment.

The lookup rule in practice

A ring implementation generally maintains a sorted list of virtual-node positions and the physical server attached to each position. For a key, it:

  1. Hashes the key to a ring position.
  2. Finds the first virtual-node position at or clockwise after that position.
  3. Uses the physical server attached to that virtual node.
  4. Wraps to the first virtual node if the key lies after the final position.

The sorted positions can be searched efficiently, commonly with binary search, so lookup does not require checking every server. Every client or routing service must use the same ring view and hashing rules; otherwise two clients can choose different owners for the same key.

A useful mental model is a circular set of signposts: the key lands somewhere on the road, and the next signpost clockwise names its owner. Virtual nodes add many signposts for each real server.

What changes during a join or failure

The ring changes in two separate senses:

Consistent hashing directly solves the first problem and limits the scope of the second. An implementation still needs membership coordination, a way to detect failures, and a rebalancing process. During rebalancing, requests may need forwarding or temporary fallback behavior.

The term may appear in cache configuration, a partitioning design, or an incident report saying that a node “caused a ring change” or “triggered token movement.” In each case, the important promise is the same: servers own portions of a shared circular hash space, virtual nodes smooth those portions across physical machines, and adding or removing one machine changes only the portions adjacent to its ring positions.

← All articles