Phases/Working of Consistent Hashing
The following are the phases involved in the process of consistent hashing:
- Hash Function Selection: The first step in consistent hashing is to choose the hash function that will be used to associate keys with network nodes. For each key, this hash function ought to yield a different value and be deterministic. Keys will be consistently and predictably mapped to nodes using the chosen hash function.
- Node Assignment: Based on the hash function’s findings, nodes in the network are given keys in this phase. The nodes are organized in a circle, and the keys are given to the node that is situated closest to the key’s hash value in a clockwise direction in the circle.
- Key Replication: It’s critical to make sure that data is accessible in a distributed system even in the case of node failures. Keys can be copied across a number of network nodes to accomplish this. In the event that one node fails, this helps to guarantee that data is always accessible.
- Node Addition/Removal: In order to keep the system balanced as nodes are added to or removed from the network, it may be necessary to remap the keys to new nodes. Consistent hashing reduces the effect of new or removed nodes by merely remapping a small portion of keys to the new node.
- Load balancing: Consistent hashing aids in distributing the load among the network’s nodes. To keep the system balanced and effective when a node is overloaded, portions of its keys can be remapped to other nodes.
- Failure Recovery: Keys assigned to a node can be remapped to other nodes in the network in the event of a node failure. This makes it possible to keep data current and constantly accessible, even in the event that a node fails.
For example:
Let’s say we have 5 nodes in the ring and say node 3 fails, then the range of the next server node widens and any request coming in all of this range, goes to the new server node. This shows that due to use of consistent hashing only a small portion of keys are affected
Consistent Hashing | System Design
Consistent hashing is a distributed hashing technique used in computer science and distributed systems to achieve load balancing and minimize the need for rehashing when the number of nodes in a system changes. It is particularly useful in distributed hash tables (DHTs), distributed caching systems, and other distributed storage systems.
Important Topics for the Consistent Hashing
- What is Hashing?
- What is Consistent Hashing?
- What is the use of Consistent Hashing?
- Phases/Working of Consistent Hashing
- Implementation of Consistent Hashing algorithm
- Advantages of using Consistent Hashing
- Disadvantages of using Consistent Hashing