This blog discusses consistent hashing, which come in handy while scaling out the distributed systems. This idea is frequently applied in designing real-time applications and solving system design questions.
Before diving deep into Consistent Hashing, let’s understand what hashing is? Hashing is used to efficiently retrieve information and improve the performance of insert, delete and search operations. It uses hash function to map data to a storage pool based on key values. For example, we can generate a random number and use it as a key to map data to a server by taking the modulo of the total number of servers. But this approach is effective when the number of servers are fixed.
In distributed systems, the number and location of servers may change, which makes it necessary to have a more efficient way of handling requests for a scalable application. Consistent hashing is an improvement over normal hashing that addresses these issues.
In consistent hashing, user and server locations are virtually represented in a circular structure called a hash ring. The hash ring is considered infinite and can accommodate any number of servers, which are assigned to random locations based on a hash function. This allows for flexible allocation of servers and efficient handling of requests.
The traditional hashing method is not well-suited for use in distributed systems where multiple users are requesting services from multiple servers. This is because it assumes that the number of servers and their mapping locations are fixed, which is not the case in a distributed system. If a server fails or goes offline, the traditional method requires significant computation to remap requests to different servers, which can affect the throughput and latency of the service.
In distributed systems, multiple nodes interact with each other and the number of nodes can change due to changes in traffic or maintenance. For example, if we have five nodes and there is an increase in traffic, we may need to add two more nodes to the system, bringing the total to seven. Using traditional hashing, we would need to recompute the mapping of requests, as the hash was previously based on five nodes but now we have seven. Similarly, if the number of nodes decreases, we would need to recompute the mapping again. This is inefficient and requires a lot of computation and data reshuffling.
So in situations where the number of operational servers is not fixed, the traditional hashing method becomes increasingly inefficient, since it requires more and more computation and reassignment of requests. To address these shortcomings, the concept of consistent hashing was developed as a more dynamic solution.
Consistent Hashing helps us in effective distribution of resources by ensuring minimum reorganisation of requests in any failure. Here we use a hash function to map servers to locations in a virtual ring. The position of the server is just a random position obtained using the hash function.
Consistent Hashing is organised in the following manner:
So in this manner, the keys are assigned to the server in Consistent Hashing. The beauty of Consistent Hashing comes when we add or remove servers.
Imagine the hash space as a circle. Both servers and keys are hashed to positions on this circle. To find a key’s server, start at the key’s position and move clockwise to the first server. Suppose the clockwise order is: Server 4 → Server 1 → … → Server 4. So keys between Server 4 and Server 1 are assigned to Server 1.
Now add Server 5 between Server 4 and Server 1: Server 4 → Server 5 → Server 1 → … → Server 4. Only keys in the section from Server 4 up to Server 5 now map to Server 5. Keys between Server 5 and Server 1 still map to Server 1, and the mapping of keys in other sections of the ring remains unchanged. In other words, some keys previously assigned to Server 1 move to Server 5; not every key after Server 4 does.
This limited reassignment helps us scale: the system can add a server without redistributing every key. The new server can take on some of the workload, which can increase throughput and reduce latency if the existing servers were overloaded.

On a consistent hash ring, each key is assigned to the first server encountered when moving clockwise from the key position. So if a server fails, the system treats it as unavailable and continues the clockwise search to the next active server. Suppose Server 1 is the next active server clockwise after Server 4.
Now keys that were assigned to Server 4 are routed to Server 1, and keys assigned to other servers are unchanged. This can help keep the service available, but the newly asigned server need to handle the extra traffic. One more thing: Consistent hashing only redirects requests and does not recover data stored on the failed server. To preserve access to that data, systems need to replicate it to other servers. So replication, health checks and capacity planning work together with consistent hashing to ensure fault tolerance.
In basic consistent hashing, each server is mapped to one position on the ring. The keys in the clockwise interval after one server and up to the next are assigned to that next server. These intervals can vary greatly in size. If one server interval is much larger than the others, it will have more keys and handle more workload. In this scenario, an overloaded server can become a bottleneck and may be more likely to fail.
So, what is the solution? Here is the idea: use virtual nodes to improve distribution. Instead of placing each server at just one position, place it at several positions on the ring. These positions are called virtual nodes. For example, Server A might be represented by A-1, A-2, and A-3, each hashed to a different location.
A key is still assigned to the first server position encountered when moving clockwise. But each server owns several smaller ring intervals scattered around the circle, so its keys come from multiple parts of the ring.
When a physical server fails, all of its virtual nodes are removed from the ring. The keys assigned to those positions move to the next active virtual nodes clockwise. The other key assignments generally remain unchanged. Adding more virtual nodes usually improves balance, though it does not guarantee a perfectly uniform distribution. Also, virtual nodes are not the same as data replicas: they distribute ownership across the ring, while data replication stores copies of data on multiple servers for availability and recovery.
Consistent hashing is an important technique for distributing data or requests across servers. It makes scaling easier by limiting how many keys need to be reassigned when servers are added or removed. Combined with replication and health checks, it can also help a system remain available when a server fails.
Thanks to Suyash for his contribution in creating the first version of this content. If you have any queries/doubts/feedback, please write us at contact@enjoyalgorithms.com. Enjoy learning, Enjoy system design, Enjoy algorithms!