Home

Posts

Tags

Consistent Hashing

Consistent hashing is a technique used in distributed systems to distribute data across a changing set of nodes (like servers) with minimal disruption.

In a traditional hashing system, if you add or remove a server, almost every single piece of data has to be moved to a new location. Consistent hashing solves this by ensuring that when the number of servers changes, only a small fraction of keys need to be remapped.

The Hash Ring Mechanism

In a standard system, you might use a simple formula like server = key(mod n), where n is the number of servers. But if n changes (a server crashes or you add a new one), almost every key’s location changes. Consistent hashing avoids this mess.

How the Ring Works

Imagine a circle representing a range of numbers (for example, from 0 to 232−1).

Virtual Servers

The Problem of Clumping

In the real world, hash functions don’t always distribute servers perfectly evenly around the circle. You might end up with a situation where Server A and Server B are very close together, but there is a massive gap between Server B and Server C.

If the gap between B and C is huge, Server C ends up being responsible for a much larger portion of the ring than the others.

If Server C is doing 8 times the work of the others, it might crash under the pressure, leading to a “cascading failure” where other servers start failing one by one.

Using Virtual Servers

Instead of placing a server on the ring just once, we hash it multiple times using different labels (e.g., “Server A#1”, “Server A#2”, “Server A#3”). This scatters many “virtual” points for a single physical server all across the ring.

The virtual nodes are completely mixed together on the ring.

The “Hot Key” Challenge

Even with a perfectly balanced ring and virtual nodes, you can still run into trouble. Imagine a social media app where 99% of your users are looking at one specific “celebrity” profile.

In a consistent hashing setup:

If everyone wants to see the same celebrity profile, one server gets crushed even with a perfect hash ring. To solve this, systems often use layering:

The Metadata Trade-off

Consistent hashing isn’t “free.” For a client, to find data, it needs to know the current state of the ring.

Replication

In systems like Apache Cassandra, they don’t just store data on the “first” server they hit. To ensure they don’t lose your data if a server blows up, they use a Replication Factor (RF).

If RF=3, the system finds the first server (the “Coordinator”), and then automatically saves copies of that data on the next two distinct physical servers walking clockwise around the ring.

The Replication Dilemma 🛡️

When we talked about replication, a tricky question arises: If you write data to the ring, do you wait for all 3 servers to confirm they have it before telling the user “Success”?

Most systems like Cassandra use a “Quorum” (N/2) + 1. For a replication factor of 3, they usually wait for 2 servers to acknowledge the write. This balances speed with safety.

Case Studies

Discord: Managing Millions of Voice Chats

Discord uses a variation of consistent hashing to manage their voice servers.

The Problem: When you join a voice channel, you have a persistent connection to a specific server. if Discord used simple hashing and added a new server to handle more users, the “modulo” would shift, and everyone would get disconnected and have to reconnect.

The Solution: With the hash ring, they can add new servers to their “fleet” during peak hours, and only a tiny fraction of users—those who land in the new server’s slice—need to move. Everyone else keeps talking without a hiccup.

Netflix: The Content Delivery “Open Connect”

Netflix doesn’t just use this for small bits of data; they use it to decide which physical hard drives across the world should hold specific movie files.

The Problem: Some servers have 200TB of spinning disks, while others are ultra-fast SSDs with only 18TB. A “fair” ring would overwhelm the small ones.

The Solution: They use Weighted Virtual Nodes. They give the big 200TB servers thousands of virtual nodes on the ring, while the 18TB servers get far fewer. This ensures that the movies are distributed exactly in proportion to what each server can handle.

Tags: