Skip to the guide

Consistent hashing

IntermediateConcept3 pages

Hash sharding (Sharding strategies) needs a rule that maps each key to a server. The obvious rule, hash modulo the number of servers, moves almost every key when you add or remove one. Consistent hashing moves only the keys that have to move. Interviewers expect it in any design with a cache cluster or a partitioned store, and they probe whether you know why it needs virtual nodes.

Lifeguards around a lake

Lifeguards sit in chairs at points around a round lake. Each one watches the stretch of shore from the previous chair, going clockwise, up to their own. A swimmer belongs to the next chair clockwise from where they went in.

A new lifeguard arrives and puts a chair down somewhere: only the stretch just before the new chair changes hands, all of it taken from one neighbour. When a lifeguard goes home, the next one clockwise takes over their stretch, and nobody else is affected. The flaw: chairs put down at random leave some stretches long and others short, and the lifeguard who covers for a colleague now watches twice as much. So the lifeguards agree that each of them sits in several short stretches spread around the lake.