Skip to content
Optimal Lab

System design · interactive

Consistent hashing

Spread keys over cache or database servers. With hash(key) % n, adding one server moves almost every key. Put servers and keys on a ring instead, and only one slice moves.

BACDclockwise →◯ = moved
  • Server A
    36
  • Server B
    16
  • Server C
    0
  • Server D
    8

60 keys on 4 servers. Busiest server holds 36 keys; a perfect split is 15.

Placement

Servers (tap to add or remove)

Virtual nodes per server

  1. 1

    A hash turns a name into a number from 0 to 2³²−1. Bend that range into a circle, and each server sits at its hash.

    A#0 → 275° · B#0 → 112° · C#0 → 284° · D#0 → 350°

  2. 2

  3. 3

  4. 4

  5. 5