DEV Community

Cover image for Consistent Hashing Explained with an Example
ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Consistent Hashing Explained with an Example

πŸ”„ 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 to S2 (30)
  • K2 (25) β†’ goes to S2 (30)
  • K3 (40) β†’ goes to S3 (60)
  • K4 (65) β†’ goes to S4 (85)
  • K5 (70) β†’ goes to S4 (85)
  • K6 (90) β†’ goes to S1 (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)