← Lessons

quiz vs the machine

Gold1420

Networking

Consistent Hashing for Load Balancing, Deep

Mapping keys to backends on a ring so adding or removing a node moves minimal traffic.

6 min read · core · beat Gold to climb

The remapping problem

A naive hash assigns a key to a server with hash of key modulo N. When N changes, almost every key maps somewhere new, wrecking caches and sticky state.

The ring

Consistent hashing places both servers and keys on a circular hash space. A key is owned by the first server found by walking clockwise from the key's position.

  • Removing a server hands only its arc to the next server.
  • Adding a server steals only a slice from one neighbor.
  • On average just one over N of the keys move when N changes by one.

Virtual nodes

A handful of physical servers placed once on the ring create lumpy arcs and uneven load. The fix is virtual nodes: each physical server is hashed to many points on the ring.

  • More points means smoother distribution.
  • Weights are expressed as more or fewer virtual nodes.

Why balancers use it

For caching tiers and sticky backends, consistent hashing keeps the same key on the same server across scaling events, preserving cache hits and session affinity.

Key idea

Consistent hashing maps keys to backends on a ring so that adding or removing a node disturbs only a small slice of traffic, and virtual nodes keep that slice even.

Check yourself

Answer to earn rating on the learn ladder.

1. Why does plain modulo hashing scale poorly when servers change?

2. On a consistent hash ring, removing one server moves roughly:

3. Virtual nodes exist primarily to: