Consistent Hashing Explained: The Hash Ring and Virtual Nodes

Blog / Consistent Hashing Explained: The Hash Ring and Virtual Nodes

What exactly is consistent hashing? And why do systems like Cassandra and Discord use it to spread data across servers?

Adding One Server Moves Almost Every Key

The simple way to spread keys across servers is to hash the key and then modulo it by N, where N is the number of servers: hash(key) % N

Imagine you have four servers, and then a fifth server is added. The divisor changes from 4 to 5, and because the math changes, about 80% of the keys now map to a different server.

For a cache, that means a flood of misses that all hit the database at once. For a database, it means moving most of the data.

Modulo hashing: when N goes from 4 to 5, 8 of 10 keys move to a different server, and every cache miss hits the database at once

The exact same massive data movement happens when a server crashes. So how do we prevent this massive data reshuffle? This is where the hash ring comes in.

How the Hash Ring Works

Picture the hash output range as a circle. You hash each server onto the ring, and you hash each key onto the same ring.

A key belongs to the first server you reach, walking clockwise from the key.

The server positions are kept sorted, so finding the next one is a binary search, O(log n). Our Big O Notation post covers this in more detail.

Adding or Removing a Server

When a new server is added, it only takes the keys located between it and the server before it. These keys used to belong to its clockwise neighbor.

When a server is removed, only its keys move, landing on the next server clockwise. Every other key stays exactly where it is.

The hash ring: when server E joins, only key 48 moves from C to E, and when B leaves, only keys 22 and 30 move to E

On average, only about 1/N of the keys move during these changes. But placing physical servers directly onto the ring creates a new problem.

Uneven Slices

With only a few servers placed at random points, the gaps between them can be uneven. One server can own a much bigger slice of the ring than another.

And when a server fails, its whole slice lands on one neighbor, which can overload it. So how do we balance this load evenly?

Virtual Nodes

Each physical server can be hashed onto the ring many times at many points. These points are called virtual nodes.

Because there are many points, the slices even out. When a server fails, its keys spread across many servers instead of hitting just one neighbor.

Virtual nodes: servers A, B and C each appear at four points on the ring, so each owns about a third of it

Stronger servers can simply be given more virtual nodes to handle a larger share of the work.

Replicas

To ensure we do not lose data if a physical server goes offline entirely, we need backups.

To keep copies of a key, you walk clockwise from the key and store it on the next few servers. The algorithm skips virtual nodes that belong to a physical server that already holds a copy, so the copies land on different machines.

This is how Amazon's Dynamo places its replicas using its preference list.

The Hot Key Trap

While this makes the system reliable, there is one major catch you must know for system design interviews.

Virtual nodes balance how many keys each server owns, but they do not balance how often each key is requested. One very popular key, like a viral post, still hashes to one spot and lands on one server.

Consistent hashing cannot fix this hot key problem. However, you can solve this by caching the hot key locally or splitting it into several sub-keys.

Where It's Used

Consistent hashing is the backbone of many distributed systems:

  • Cassandra and Amazon's Dynamo use a hash ring to decide which nodes store the data.

  • Discord uses it to route chat traffic to specific servers.

  • Memcached client libraries use it to pick a cache server.

Recap

Feature

Modulo Hashing

Consistent Hashing

Finding a server

Math operation on the key

Walk clockwise on the ring

Keys moved on change

Almost all keys

A small fraction of keys

When a server fails

Reshuffles all data

Shifts a slice to neighbors

Load balance

Spreads keys evenly by default

Needs virtual nodes for even slices

Lookup cost

O(1) math operation

O(log n) binary search

The hash ring is the circular space where both servers and keys are mapped.

Virtual nodes are the multiple points on the ring that represent a single physical machine.

Replicas are the backup copies of data placed on the next clockwise physical servers.

The hot key is a highly requested item that hits a single server and requires extra caching.

About TechPrep

Never walk into a technical interview unprepared again. TechPrep empowers software engineers to stop guessing and start getting offers. We provide the exact questions asked by tech companies across Data Structures & Algorithms, System Design, Low-Level Design & AI coding rounds. Don't leave your career up to chance. Join thousands of engineers who have successfully navigated the tech hiring maze and landed roles at top tech companies.