System Design Academy

Saving…

Lesson 5 of 10 in Module 50 of 54 lessons complete

Consistent Hashing

Module 5, lesson 5Concept12 min

Definition

Hashing

In plain English
Hashing turns a key into a number in a fixed range using a formula that always gives the same number for the same key.
Think of it like
It is like a cloakroom assigning each coat a hook number from its owner's name.

Consistent hashing

In plain English
Consistent hashing places servers and keys on a ring so that adding or removing a server moves only a small share of keys.
Think of it like
It is like seats around a round table where a new guest only takes over the seats beside them.

Why it exists

You add a fifth cache server. With hash(key) % N, about 80% of keys now point somewhere different.

Most lookups miss their old cached values and reach the database. Scaling the cache can briefly overload the system it protects.

How it works

Virtual nodes

In plain English
Virtual nodes give each real server many points on the ring so keys spread more evenly.
Think of it like
Like a large restaurant with several entrances around the block.

This extends Module 3 sharding and Module 2 hash-based load balancing. The ring changes how keys map to machines.

Three servers form a cycle; a key between A and B belongs to B, the next server clockwise.

Walk clockwise to the next server position.

Three servers form a cycle; a key between A and B belongs to B, the next server clockwise.

  1. Place servers

    Hash server names to positions on a ring.

  2. Place a key

    Hash the key and choose the first server clockwise, wrapping around if needed.

  3. Add a server

    Move keys between the new server and its predecessor to the new server.

  4. Balance ownership

    Use many virtual nodes per machine, weighted by capacity.

MethodExpected movement: four to fiveSpreadCost
hash % N80%Even with uniform hashesSmall formula
Consistent hashing20% with balanced equal capacityVirtual nodes reduce imbalanceMaintain sorted ring positions
A tiny ring uses sorted hash positions and wraps around; production rings also handle collisions and membership changes.
Go
package main

import (
	"fmt"
	"hash/crc32"
	"sort"
)

type node struct {
	point uint32
	name  string
}

func main() {
	ring := []node{}
	for _, name := range []string{"A", "B", "C"} {
		ring = append(ring, node{crc32.ChecksumIEEE([]byte(name)), name})
	}
	sort.Slice(ring, func(i, j int) bool { return ring[i].point < ring[j].point })
	for _, key := range []string{"episode-1", "episode-2"} {
		h := crc32.ChecksumIEEE([]byte(key))
		i := sort.Search(len(ring), func(i int) bool { return ring[i].point >= h })
		if i == len(ring) {
			i = 0
		}
		fmt.Println(key, ring[i].name)
	}
}

Examples

Grow an episode cache

The cache grows from four equal servers to five.

Expected movement depends on balanced placement.
Text
Uniform hashes: compare hash % 4 with hash % 5
Check 20 equally likely remainders: 4 unchanged, 16 changed
16 / 20 = 80% movement
Balanced five-server ring: new server owns about 1 / 5 = 20%

The ring moves only keys the new server takes over. Twenty percent is an expectation, not a guarantee for every small ring.

The insight

Consistent hashing reduces cache movement during scaling.

Trade-offs

Pros

  • Moves fewer keys when membership changes.
  • Limits reassignment after failures.
  • Can weight unequal machines.

Cons

  • Needs ring membership coordination.
  • Small rings can be uneven.
  • Popular keys can overload one owner.

Use when

  • Caches grow or shrink.
  • Sharded data needs stable assignments.

Avoid when

  • A fixed mapping already meets your needs.
  • You expect routing alone to recover lost data.

Quick check

Going from 4 to 5 servers with hash % N, roughly how many keys move?
What do virtual nodes improve?

Takeaways

  • Changing mod-N moves most keys when capacity grows from four servers to five.
  • Consistent hashing moves only the affected ring ranges.
  • Virtual nodes improve expected balance but do not remove hot keys.

Try the quick check above whenever you feel ready.

Previous: Circuit Breakers & Bulkheads