Consistent hashing

A ring to split the load · Part of Databases

Key idea

Practice

7 questions · about 4 min

Explained step by step

Consistent hashing

The classic way to split is computing hash(key) % N, where N is the number of nodes. The problem shows up when adding or removing a node: N changes and almost every key moves to another node. With 4 nodes, going to 5 moves about 80% of keys. It's like numbering seats by guest count: if 1 more arrives, almost everyone has to change seats. Moving that much data is very inefficient.

Consistent hashing is a scheme that doesn't depend on the number of nodes: it places each node at a position on an abstract circle, the hash ring. That way servers can be added or removed without rearranging every key or touching every server. Only K/N keys need to move, where K is the number of keys and N the number of nodes. It's like kids in a circle: if a new one joins, only those next to them shift.

The ring's big advantage shows when the number of nodes changes. If a node is added, it only receives the keys between it and the previous node; the rest stays the same. If one is removed, its keys go to the next one on the ring. In both cases only a fraction moves. It's like a round table: if someone joins, only the 2 people next to them shift.

The ring also helps with replication: each piece of data is copied to the next N nodes clockwise, so if 1 fails, the copy is on its neighbor. Consistent hashing is used by systems like Amazon DynamoDB, Apache Cassandra, Discord and Akamai's CDN. It's like leaving a spare house key with the neighbor on your right and the next one: if one isn't home, the other lets you in.

Hash ring

The hash goes from 0 to m-1 and is drawn as a ring. Each node's hash is computed and placed on the ring; the same with each request. A request goes to the first node it finds moving clockwise (or counterclockwise, if chosen that way). If a node is added, only the requests between it and the previous node change owner. It's like a clock with 4 people at different hours: each minute belongs to the next person in the direction of the hands.

Hotspot

In theory the ring spreads evenly, but in practice it doesn't: nodes can be badly placed and one ends up with most requests. That node becomes a hotspot that slows down the whole system. Adding nodes can fix it, but it's expensive. It's like 3 vendors in a square: if 2 set up side by side, the third serves half the square alone.

Virtual node

Virtual nodes (VNodes) fix uneven splitting. Instead of placing each physical node at 1 single position on the ring, it's placed at many, using several hash functions. That way each real server covers many small arcs spread all over the ring, and the load evens out. It's like spreading the 3 vendors across 30 stalls around the square: each serves 10 small stalls instead of 1 huge area.

Terms in this lesson

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.

Key idea

A ring to split the load