Architecture
Consistent Hashing Explained: Spreading Keys Across Servers Without Reshuffling
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربيAs soon as data or traffic is spread across more than one server, you need a rule that says which server owns which key. The obvious rule, hashing the key and taking the remainder by the number of servers, works until you add or remove a server. Then almost every key moves, caches go cold and databases receive a flood of misses. Consistent hashing is a technique that keeps most keys where they are when the set of servers changes. This article explains the idea, its practical refinements, and where you meet it in everyday tools.
The Problem with Modulo Hashing
Suppose you have four cache servers and place each key on server hash(key) % 4. Distribution is even and lookups are instant. Now add a fifth server. The rule becomes hash(key) % 5, and a key that mapped to server 2 before may now map to server 0. On average, only about one in five keys stays on the same server; the rest move.
For a cache this means a sudden wave of misses that hits the database all at once. For a sharded data store it means moving most of the data. Either way, a routine capacity change becomes a risky event.
How the Hash Ring Works
Consistent hashing places both servers and keys on the same circular number space, often visualized as a ring:
- Hash each server's identifier (for example its name or address) to a position on the ring.
- Hash each key to a position on the same ring.
- Walk clockwise from the key's position; the first server you meet owns the key.
When a server is added, it takes over only the keys between its position and the previous server on the ring. When a server is removed, only its keys move to the next server. With N servers, adding or removing one moves roughly 1/N of the keys instead of nearly all of them.
Virtual Nodes for Even Distribution
With only a few servers, random positions on the ring can be uneven: one server may own a large arc and receive far more keys than the others. The standard fix is virtual nodes. Each physical server is hashed many times, for example cache-a#1, cache-a#2 and so on, so it appears at many points around the ring. The arcs average out and the load becomes much more balanced.
Virtual nodes also make weighting easy. A server with twice the memory can be given twice as many virtual nodes and will receive roughly twice the keys.
Comparing Placement Strategies
| Strategy | Keys moved when a server changes | Balance | Notes |
|---|---|---|---|
Modulo (hash % N) | most keys | good | simplest, fragile under change |
| Consistent hashing ring | about 1/N | uneven without virtual nodes | widely used in caches and proxies |
| Ring with virtual nodes | about 1/N | good | supports weights |
| Rendezvous (highest random weight) | about 1/N | good | no ring; score every server per key |
| Jump consistent hash | about 1/N | very good | servers must be numbered sequentially |
Rendezvous hashing is worth knowing: for each key, compute a score for every server and pick the highest. It achieves the same minimal movement without maintaining a ring, at the cost of scoring all servers per lookup, which is fine for small clusters.
Where You Will Meet It
Nginx upstreams
Nginx supports consistent hashing for load balancing. This routes each URL to the same backend cache consistently, and adding a backend remaps only a share of URLs:
upstream cache_nodes {
hash $request_uri consistent;
server 10.0.0.11:8080;
server 10.0.0.12:8080;
server 10.0.0.13:8080;
}The consistent parameter selects the ketama algorithm. Without it, Nginx uses plain hashing and remaps far more keys when the list changes.
HAProxy
HAProxy offers hash-type consistent together with balance methods such as balance uri or balance hdr(...).
Memcached and Redis clients
Many Memcached client libraries implement ketama-style consistent hashing on the client side. Redis Cluster takes a different approach: it divides the key space into 16384 fixed hash slots and assigns slots to nodes, which achieves controlled movement through explicit slot migration rather than a ring.
Distributed databases
Systems in the Dynamo family, such as Cassandra, use token rings derived from consistent hashing to decide which nodes store each partition and its replicas.
Practical Tips
- Hash a stable identifier for each server, not its position in a list.
- Use enough virtual nodes to smooth out distribution; measure the actual spread with your real keys.
- Remember that hot keys still overload one server; consistent hashing balances key counts, not request popularity.
- Combine with health checks so a failed server is removed and its keys move to the next owner.
- When warming a new cache node, expect a temporary rise in misses for its share of keys.
If you are designing a multi-node cache or storage layer, accurate time and controlled overload matter too; see our article on clock skew. You can spin up several small servers to experiment from the deploy page.
FAQ
Does consistent hashing guarantee perfectly even load?
No. It minimizes how many keys move when servers change, and with virtual nodes the number of keys per server becomes fairly even. Request load can still be uneven if some keys are much more popular than others, so watch for hot keys separately.
How many virtual nodes should each server have?
There is no universal number. Implementations commonly use from dozens to a few hundred per server. More virtual nodes give smoother distribution at the cost of a slightly larger lookup table. Test with a sample of your own keys and check the spread.
Is consistent hashing useful with only two or three servers?
Yes, if servers are added or removed and the cost of remapping matters, such as with caches. With a fixed, small set of stateless backends, simple round robin is often enough.