π Consistent Hashing Explained with an Example
To understand consistent hashing, letβs walk through a simple example step by step.
Imagine we have a hash ring with positions from 0β100
. Both servers (nodes) and keys (data items) are placed on this ring using a hash function.
π’ Step 1: Initial Setup
We start with 4 servers placed on the ring:
- S1 β 10
- S2 β 30
- S3 β 60
- S4 β 85
And we have 6 keys:
- K1 β 12
- K2 β 25
- K3 β 40
- K4 β 65
- K5 β 70
- K6 β 90
πΉ Mapping Rule: Each key is assigned to the first server encountered while moving clockwise.
-
K1 (12)
β goes toS2 (30)
-
K2 (25)
β goes toS2 (30)
-
K3 (40)
β goes toS3 (60)
-
K4 (65)
β goes toS4 (85)
-
K5 (70)
β goes toS4 (85)
-
K6 (90)
β goes toS1 (10)
(wraps around the ring)
β All keys are evenly distributed.
π΄ Step 2: Server Failure
Now, suppose S3 (60) crashes.
- Keys assigned to S3 (
K3
) need a new home. - In consistent hashing, these keys are reassigned to the next available server in clockwise direction, which is S4 (85).
πΉ New distribution:
-
K1, K2
β S2 -
K3
β S4 (moved from S3) -
K4, K5
β S4 -
K6
β S1
β
Only K3 is moved. Other keys remain unaffected.
This is the power of consistent hashing: minimal disruption.
π‘ Step 3: Adding a New Server
Now, letβs add a new server S5 at position 50.
- Keys between S2 (30) and S5 (50) will now move to S5.
- In our case, only
K3 (40)
falls in this range.
πΉ New distribution:
-
K1, K2
β S2 -
K3
β S5 (moved from S4) -
K4
β S3 (back online in this step, assumed) -
K5
β S4 -
K6
β S1
β Again, only one key (K3) was affected.
π― Key Takeaways from the Example
- Keys are always mapped clockwise to the nearest server.
- When a server fails, only the keys belonging to it move to the next server.
- When a server is added, only the keys in its segment move.
- This ensures stability and scalability β unlike traditional hashing where all keys might need to be remapped.
π This simple example shows why consistent hashing is a backbone for scalable distributed systems like caching (Memcached, Redis clusters), databases, and load balancers.
More Details:
Get all articles related to system design
Hastag: SystemDesignWithZeeshanAli
Git: https://github.com/ZeeshanAli-0704/SystemDesignWithZeeshanAli
Top comments (0)