arthrop0d

Brief articles on varied topics.

Consistent Hashing, Hash Rings, and Virtual Nodes

algorithmsdistributed systems

Consistent hashing assigns both keys and servers to positions on a circular hash space, called a hash ring. A key belongs to the first server found when moving clockwise from the key’s position. When a server is added or removed, only the keys in nearby ring segments usually move, rather than nearly every key.

The problem with ordinary hashing

A simple distributed cache or key-value store often starts with a rule such as:

server = servers[hash(key) % len(servers)]

This is easy to understand: hash the key to an integer, then use the remainder to select a server. It works until the server list changes.

Suppose four servers use hash(key) % 4. If a fifth server is added, the calculation becomes hash(key) % 5. The result changes for most keys, so most cached values are now looked up on the wrong server. The system may refill the cache, but the change can create a large burst of misses and load on the backing database. For persistent data, moving that much data is expensive.

The problem is not that the hash function is poor. The problem is that the number of servers is part of the mapping. Changing that number changes the mapping for almost every key.

Putting keys and servers on a ring

Consistent hashing separates the hash space from the number of servers. Imagine all possible hash values arranged in a circle. For example, a hash might produce a value from 0 through 999, with 999 connecting back to 0.

The system hashes each server identifier onto this ring. It also hashes each key onto the same ring. To find a key’s server:

  1. Hash the key to get its ring position.
  2. Move clockwise from that position.
  3. Choose the first server position encountered.
  4. If the search passes the maximum value, wrap around to the smallest position.

The server positions divide the ring into ownership ranges. A server owns the keys between the previous server position and its own position, treating the ring as circular.

For example, if servers appear at positions 200, 500, and 800, a key at 620 moves clockwise to 800 and belongs to that server. A key at 900 wraps around and belongs to the server at 200.

A practical implementation stores the server positions in sorted order. It uses a binary search to find the first position greater than or equal to the key’s hash, wrapping to index zero when there is no such position. The hash function and ring construction must be deterministic so every client makes the same choice.

Why membership changes affect fewer keys

Now add a new server at position 650. It takes ownership only of the range between the preceding position, 500, and 650. Keys in that interval move to the new server; keys elsewhere continue to find the same first server clockwise.

Likewise, if the server at 800 is removed, only the keys that it owned move. Those keys now continue clockwise to the next surviving server. The rest of the keyspace does not change ownership.

With evenly distributed positions, adding one server to a group of N servers remaps roughly 1 / (N + 1) of the keys. Removing one remaps roughly 1 / N, because the removed server’s range must be assigned elsewhere. These are approximations: the actual result depends on the hash distribution and the ring’s positions.

Consistent hashing limits the scope of remapping; it does not make membership changes free. The affected keys still need to be fetched, copied, rebuilt, or otherwise handled.

Virtual nodes make ownership less lopsided

A single ring position per server can produce uneven ranges. Hashing only three or four server identifiers may place two positions close together and leave a large empty arc. The server owning that large arc receives a disproportionate share of keys.

A virtual node, or vnode, is an additional ring position representing the same physical server. Instead of placing each server once, the system places it many times using identifiers such as server-a-0, server-a-1, and server-a-2. The ring still maps a key to one position, but that position points back to the physical server.

For example, a server might own hundreds of small ranges distributed around the ring rather than one large range. With more independent positions, the combined ownership of each physical server is usually closer to the desired share. Adding or removing a server also changes many small intervals spread across the ring instead of one contiguous interval.

Virtual nodes improve balance and make the amount of moved data more predictable. They do not eliminate remapping: a new server still takes ranges from existing servers, and a removed server’s ranges still need new owners. They also have costs, including a larger ring, more membership metadata, and more range-transfer operations during a change.

The system must define how many vnodes each server receives. Equal-capacity servers commonly receive equal counts. If machines have different capacity, a larger machine can receive more vnodes, giving it a larger expected share of the keyspace.

What this explains in practice

When you encounter “consistent hashing” in a cache client, sharded service, or membership configuration, look for the ring’s membership and vnode settings. A node-join or node-leave log often reports a number of moved ranges rather than a full redistribution. An incident involving a sudden cache-miss spike after scaling may indicate that ordinary modulo hashing—or an incorrectly coordinated ring—was used instead.

Consistent hashing works because ownership is based on local neighbors on a stable ring. Changing the set of servers changes only the intervals next to the changed positions, while virtual nodes make those intervals numerous and broadly distributed.

← All articles