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 o…


This content originally appeared on DEV Community and was authored by ZeeshanAli-0704

🔄 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


This content originally appeared on DEV Community and was authored by ZeeshanAli-0704


Print Share Comment Cite Upload Translate Updates
APA

ZeeshanAli-0704 | Sciencx (2025-08-28T01:31:55+00:00) Consistent Hashing Explained with an Example. Retrieved from https://www.scien.cx/2025/08/28/consistent-hashing-explained-with-an-example/

MLA
" » Consistent Hashing Explained with an Example." ZeeshanAli-0704 | Sciencx - Thursday August 28, 2025, https://www.scien.cx/2025/08/28/consistent-hashing-explained-with-an-example/
HARVARD
ZeeshanAli-0704 | Sciencx Thursday August 28, 2025 » Consistent Hashing Explained with an Example., viewed ,<https://www.scien.cx/2025/08/28/consistent-hashing-explained-with-an-example/>
VANCOUVER
ZeeshanAli-0704 | Sciencx - » Consistent Hashing Explained with an Example. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/08/28/consistent-hashing-explained-with-an-example/
CHICAGO
" » Consistent Hashing Explained with an Example." ZeeshanAli-0704 | Sciencx - Accessed . https://www.scien.cx/2025/08/28/consistent-hashing-explained-with-an-example/
IEEE
" » Consistent Hashing Explained with an Example." ZeeshanAli-0704 | Sciencx [Online]. Available: https://www.scien.cx/2025/08/28/consistent-hashing-explained-with-an-example/. [Accessed: ]
rf:citation
» Consistent Hashing Explained with an Example | ZeeshanAli-0704 | Sciencx | https://www.scien.cx/2025/08/28/consistent-hashing-explained-with-an-example/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.