Consistent Hashing for Scalable System Design
Learn how consistent hashing and rebalancing enable scalability and fault tolerance in distributed systems.
At the heart of every large-scale system lies a fundamental challenge: distributing data across many servers.
In the previous lesson, we explored various techniques for database sharding, including hash-based sharding, which is a simple method for distributing data across nodes. That scheme works well when the cluster is static. At scale, clusters are dynamic, i.e., nodes fail, capacity is added, and maintenance happens.
When a system with millions of users needs to add or remove servers, a naive distribution strategy could trigger a catastrophic reshuffling of data, overwhelming the network and causing service-wide outages. This is a critical problem in System Design, where maintaining performance and availability during scaling events is non-negotiable.
This lesson explores consistent hashing, a powerful technique that elegantly solves the data distribution problem.
What is consistent hashing?
Consistent hashing is a technique for distributing data across servers using a circular
When servers are added or removed, only a small portion of data needs to be moved—unlike traditional sharding, where almost everything must be remapped. This makes consistent hashing ideal for modern, scalable systems that need to handle server joins, failures, or removals smoothly without heavy rebalancing.
The following diagram illustrates the core difference in data movement (key, and hence data changes for every node) and between traditional hashing and consistent hashing when a new node is added.
This visualization sets the stage for understanding the mechanics of the hash ring, which minimizes the blast radius of any change in the cluster’s topology.
How consistent hashing maps keys to nodes
At its core, consistent hashing works by mapping both nodes and data keys onto the same circular hash space. Imagine a circle representing the entire output range of a hash function, for instance, from