consistent hashing
termKept in this browser · My desk
A way of mapping keys to servers in which adding or removing one server moves only the keys that belonged to it, or a fair share to a newcomer, instead of reshuffling nearly all of them as a hash taken modulo the server count does.
It is how a load balancer, a cache cluster or a content network keeps clients and objects where they were while the set of servers changes. With plain modulo hashing, losing one server out of four moves two thirds of the clients that were on the survivors, emptying caches and breaking hash-based persistence; with consistent hashing only the dead server's clients move. HAProxy and NGINX offer both kinds and say so in their manuals: map-based or plain hashing for an even spread, consistent hashing when stability matters more than smoothness.
What it is. A hash function maps a key, a client address, a Uniform Resource Locator (URL), a session identifier, onto one of N servers. The obvious way, h(key) mod N, spreads keys evenly but depends on N: when a server leaves or joins, N changes and almost every key lands somewhere else. Consistent hashing removes that dependence. Each server is placed at several points of a fixed hash space, often drawn as a ring, a key goes to the nearest server point, and when a server leaves only the keys that were on its points move to their next neighbours; when one joins it takes a share from each.
Where it came from. The term comes from a paper by David Karger, Eric Lehman, Tom Leighton, Matthew Levine, Daniel Lewin and Rina Panigrahy of the Massachusetts Institute of Technology (MIT) Laboratory for Computer Science, Consistent Hashing and Random Trees: Distributed Caching Protocols for Relieving Hot Spots on the World Wide Web. Their problem was web caches: if objects were spread with a classical hash such as the linear congruential x to ax + b (mod p), any change in the range p, a cache added, removed or simply unknown to some clients, would move almost every item, which they called catastrophic. They defined consistent hash functions by properties such as smoothness, under which adding a machine moves only the items needed to keep the distribution even, and balance. A related idea reached the Internet Engineering Task Force (IETF) on 26 February 1998 as the Cache Array Routing Protocol (CARP), draft-vinod-carp-v1-03 by Vinod Valloppillil of Microsoft and Keith W. Ross of the University of Pennsylvania: each combination of URL and proxy receives a score from the two hashes, scaled by the proxy's capacity, and the highest score wins, so removing a proxy only reassigns the URLs it was winning. The draft expired, but the name survives: F5 BIG-IP's source address persistence has a hash-algorithm option, carp, that selects the pool member from the hash of the source address.
How load balancers implement it. HAProxy's hash-type directive names the two kinds. Map-based keeps a static array of the running servers: hashes are very smooth and respect weights, but since a server is selected by its position in the array, most mappings change when the server count changes, which the manual calls inconvenient with caches. Consistent fills a tree with many occurrences of each server and picks the closest one; when a server goes up or down only its associations move, and when one is added only a few mappings are redistributed, making it an ideal method for caches. The manual is candid about the price: the distribution will never be very smooth, a weight or a server identifier may need adjusting, and every load balancer must give the servers the same identifiers to produce the same mapping; consistent hashing uses the sdbm function with an avalanche step by default. NGINX's hash directive warns that adding or removing a server may remap most of the keys, and its consistent parameter selects the ketama method instead, compatible with the Cache::Memcached::Fast library with ketama_points set to 160, so that only a few keys move.
Worked, by hand. Twelve clients whose hashes are 0 to 11, spread over four servers by h mod 4: servers 0 to 3 receive {0, 4, 8}, {1, 5, 9}, {2, 6, 10} and {3, 7, 11}. Server 3 fails, and a map-based balancer recomputes h mod 3 over the three survivors: client 0 stays on server 0, 1 on 1, 2 on 2, but 4 moves from 0 to 1, 5 from 1 to 2, 6 from 2 to 0, 8 from 0 to 2, 9 from 1 to 0 and 10 from 2 to 1. Of the nine clients whose server never failed, six moved, two thirds, and lost whatever state or cache they had there; only the three clients of the dead server had to move. With consistent hashing those three are the only ones that move. The same arithmetic appears in routing: Request for Comments (RFC) 2992 (November 2000) analyses the hash-threshold method of equal-cost multipath, which gives each next hop a region of the key space, and computes that deleting the middle one of five next hops disrupts 3/10 of all flows, the least disruption being obtained when the region removed is near the middle.
In practice. Consistent hashing gives persistence without a table: as long as the server set is stable, the same key reaches the same server, and when it changes most keys still do. It is the method of choice for caches and for spreading requests by URL or by a session key, and it is the reason a load balancer can lose a member without sending every client's session to a new server. On a BIG-IP, hash persistence and the carp algorithm, and on HAProxy and NGINX the directives above, are where it is configured.
What goes wrong. With few points per server the spread is uneven, which is why implementations place each server many times and why HAProxy suggests adjusting weights. Several load balancers in front of the same servers must agree on identifiers and functions, or each will send the same key to a different server. A server that flaps moves its keys back and forth with every health check change. And consistent hashing keeps keys stable, not load: a key that is hot stays on one server however busy it becomes.
Neighbours. Session persistence is the job it does without state; the health check decides when the server set changes; equal-cost multipath and the five-tuple hash are its cousins in the network layer; content delivery networks use it to choose a cache.
Also known as: consistent hash, ketama, hash ring, hash consistente
Sources
- David Karger, Eric Lehman, Tom Leighton, Matthew Levine, Daniel Lewin, Rina Panigrahy (MIT Laboratory for Computer Science and Department of Mathematics), Consistent Hashing and Random Trees: Distributed Caching Protocols for Relieving Hot Spots on the World Wide Web, PDF course copy at Princeton (read 2026-11-23): consistent hashing defined; with a classical hash such as ax + b mod p, changing the range moves almost every item; the smoothness and balance properties
- V. Valloppillil (Microsoft Corporation), K. W. Ross (University of Pennsylvania), draft-vinod-carp-v1-03, Cache Array Routing Protocol v1.0, 26 February 1998, expired Internet-Draft, IETF Datatracker (read 2026-11-23): a hash function dividing URL space among proxies; a score for each URL and proxy combination, scaled by capacity, the highest score chosen
- HAProxy Technologies, HAProxy 3.2 Configuration Manual (read 2026-11-23): hash-type map-based, a static array where most mappings change when the server count changes; consistent, a tree with many occurrences of each server where only a server's own associations move, never very smooth, sdbm and avalanche by default, the same IDs needed on every load balancer
- NGINX, Module ngx_http_upstream_module (read 2026-11-23): hash key, where adding or removing a server may remap most keys; the consistent parameter selects ketama, compatible with Cache::Memcached::Fast with ketama_points 160, so only a few keys move
- IETF, RFC 2992, Analysis of an Equal-Cost Multi-Path Algorithm, C. Hopps, November 2000 (read 2026-11-23): hash-threshold with regions of the key space; disruption of 3/10 when the middle one of five next hops is deleted; least disruption near the middle
- F5, TMSH Reference, ltm persistence source-addr (read 2026-11-23): hash-algorithm carp, the Cache Array Routing Protocol selecting the pool member from the hash of the source address