The Coding Gopher

The Coding Gopher

Consistent Hashing 101

The "one ring to rule them all" approach

The Coding Gopher's avatar
The Coding Gopher
Mar 14, 2026
∙ Paid

How a simple mathematical ring prevents catastrophic cache misses in distributed systems.

System Design: Consistent Hashing | Towards Data Science

Imagine you are building a massive application. To handle the traffic, you deploy three cache servers (Server A, B, and C) to store user sessions. When a request comes in for user:99, how do you know which server holds that data?

The standard, naive approach is to use the modulo operator. You hash the user ID to get a number, and then find the remainder when divided by the number of servers: hash("user:99") % 3.

Data distribution based on a hash function. The data is stored on servers with respect to corresponding hash values.

This works flawlessly—until your application goes viral.

Suddenly, you need to add a fourth server to handle the load. Your formula is now hash("user:99") % 4. Because the denominator changed, almost every single key in your system now mathematically maps to a completely different server.

Whenever any of the servers is shut down, its data is no longer accessible.
Analogous case of server shut down. Need to redistribute all data blocks onto remaining servers.

The result? A remapping catastrophe. You experience a 75% cache miss rate, all your requests bypass the cache, they hit your primary database simultaneously, and your entire system crashes.

In the case of any system configuration changes, all the data needs to be redistributed again.

Remapping catastrophe refers to the massive, system-wide data migration that occurs when traditional modulo-based hashing is used in distributed systems with a changing number of servers. When the server count (n) changes in a standard modulo approach (hash(key) % n), nearly all keys are assigned to new nodes, causing cache misses, database overloading, and potential system failure.

Enter Consistent Hashing.


What is Consistent Hashing?

Originally introduced in an academic paper by MIT researchers in 1997, consistent hashing is a distributed routing algorithm that solves the remapping problem. Instead of relying on the total number of servers to determine data placement, it maps both the servers and the data onto a fixed mathematical geometry: The Hash Ring.

Consistent Hashing 101: How Modern Systems Handle Growth and Failure

Step 1: Building the Ring

Imagine a circle. We take a standard hash function (like SHA-1 or MD5) that outputs a massive range of numbers, typically 0 to 232 - 1. We map this entire range onto the edge of the circle. The top is 0, and as you move clockwise, the numbers increase until you wrap back around to the top.

Step 2: Placing the Servers

Next, we hash the IP addresses or hostnames of our cache servers (e.g., hash("192.168.1.1")). This outputs a specific number on the ring. We place Server A, Server B, and Server C at their corresponding coordinates on this circle.

Step 3: Routing the Data

When a request comes in for user:99, we hash that key. It lands somewhere on the edge of the ring. To find which server holds the data, the algorithm simply moves clockwise around the ring from the key’s position until it hits the very first server.

Hash ring example. The hash range for server S1 is depicted in blue.

TL;DR. Consistent hashing uses a virtual circular ring to map data keys and server nodes, ensuring that when nodes are added or removed, only k/n keys (keys/nodes) require remapping, rather than the entire dataset. Servers and keys are hashed to positions on this ring, with keys assigned to the first node encountered moving clockwise.


The Magic of Scaling

The true brilliance of the hash ring reveals itself when your infrastructure changes.

Removing a Server

If Server 1 catches fire and dies, it is removed from the ring.

  • What happens? The keys that were assigned to Server 1 now simply continue clockwise and land on Server 2. Again, the rest of the cluster doesn’t even notice.

Instead of remapping 75% of your data, consistent hashing ensures that only k/n keys are moved (where k is the total number of keys and N is the number of servers).

Shutting down server S1 from the example above requires only transferring data previously stored on that server.
Shutting down S1 only requires migrating its stored data.

Adding a Server

Let’s say you add Server 4 to handle a traffic spike. Server 4 is hashed and lands between Server 3 and Server 0 on the ring.

  • What happens? Only the data that falls exactly between Server 3 and Server 4 gets remapped to the new server. All the data routed to Server 3, Server 2, and the rest of Server 0 remains completely untouched.

Adding a new server S4 to the system. Only part of the data stored on S0 has to be transferred to S4.
Adding a new server S4 to the system. Only part of the data stored on S0 has to be transferred to S4.

𝐋𝐞𝐚𝐫𝐧 𝐭𝐨 𝐛𝐮𝐢𝐥𝐝 𝐆𝐢𝐭, 𝐃𝐨𝐜𝐤𝐞𝐫, 𝐑𝐞𝐝𝐢𝐬, 𝐇𝐓𝐓𝐏 𝐬𝐞𝐫𝐯𝐞𝐫𝐬, 𝐚𝐧𝐝 𝐜𝐨𝐦𝐩𝐢𝐥𝐞𝐫𝐬, 𝐟𝐫𝐨𝐦 𝐬𝐜𝐫𝐚𝐭𝐜𝐡. Get 40% OFF CodeCrafters: https://app.codecrafters.io/join?via=the-coding-gopher

This Substack is reader-supported. To receive new posts and support my work, consider becoming a free or paid subscriber.


The Magic of Scaling

User's avatar

Continue reading this post for free, courtesy of The Coding Gopher.

Or purchase a paid subscription.
© 2026 The Coding Gopher · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture