Consistent Hashing is a foundational algorithm in distributed systems used to distribute request traffic and data across a dynamically scaling cluster of servers. Whether you are building a distributed cache, sharding a database, or routing traffic across proxy tiers, consistent hashing solves a fundamental challenge: how to minimize data movement when servers join or leave the network.
Practice with guided hints and real time AI feedback.
When approaching infrastructure questions in a system design interview, follow these five structured steps to navigate complex distributed systems choices:
To support consistent hashing across an infrastructure cluster, define clear data structures for physical servers, virtual nodes, and key partitions.
PhysicalNode Entitynode_id (String, Unique identifier like cache-prod-01)ip_address (String, Network address for direct node communication)port (Integer, Service port for connection pooling)status (Enum: ACTIVE, DRAINING, OFFLINE)weight (Integer, Capacity multiplier for heterogeneous hardware sizing)VirtualNode Entityvnode_id (String, Synthetic label such as cache-prod-01-vn42)physical_node_id (Foreign Key referencing PhysicalNode.node_id)hash_token (Unsigned 32 bit Integer, Exact point location on the ring circle)KeyMapping Entity (Logical Partition Concept)key_hash (Unsigned 32 bit Integer, Hash code generated from key identifier)target_vnode_id (Foreign Key referencing VirtualNode.vnode_id)target_physical_node_id (Foreign Key referencing PhysicalNode.node_id)Define explicit management and query contracts between client proxies and cluster coordinator nodes.
Request Body:
Response Body (201 Created):
Response Body (200 OK):
Response Body (200 OK):
At a high level, consistent hashing coordinates traffic routing between incoming client requests, routing proxies, and distributed storage tiers:
Architecture Diagram Generation Prompt: A clean distributed systems architecture diagram illustrating consistent hashing routing flow. At the top, multiple Client applications send read and write requests. Below them sits a Stateless Routing Proxy Tier running a local Hash Ring Routing Table. To the right, an External Cluster Coordinator (such as Apache ZooKeeper or HashiCorp Consul) monitors Node Health via heartbeat signals and broadcasts Membership Updates to all proxy nodes. Below the proxy tier is a circular Hash Ring diagram ranging from 0 to 2^32 - 1. Physical database nodes DB1, DB2, DB3, and DB4 are mapped to multiple Virtual Nodes scattered around the ring perimeter. Client key requests are hashed, placed on the ring circle, and routed clockwise to the first encountered virtual node. Sub diagrams illustrate node addition where only adjacent key segments are transferred, leaving all other node assignments completely unaffected.
The naive method for distributing items across multiple storage nodes relies on basic modulo arithmetic:
While simple modulo hashing works well when the cluster size remains static, it fails dramatically in dynamic environments. Suppose you have 4 cache servers and wish to add a 5th server to handle growing traffic.
When total_servers changes from 4 to 5, the mathematical result of hash(key) % N changes for nearly every key in the database. Specifically, statistical analysis shows that N / (N + 1) of all cached items (80 percent when scaling from 4 to 5 nodes) suddenly map to completely different servers.
This causes massive cache invalidation across your entire cluster, triggering a massive cache stampede where requests overwhelm underlying backend databases simultaneously, leading to widespread system outages.
Consistent hashing eliminates mass cache invalidation by mapping both data keys and server nodes onto a shared conceptual ring.
To implement clockwise ring traversal efficiently in memory, routing proxies store sorted arrays of server tokens. Using binary search algorithms like Java TreeMap ceilingKey or C++ std::upper_bound, proxies locate target nodes in logarithmic time O(log K), where K represents the total number of ring tokens.
Pure consistent hashing with single server tokens introduces two critical operational problems:
To solve both issues, distributed systems implement Virtual Nodes (vnodes). Instead of placing a physical server at just one point, each physical database instance is assigned multiple synthetic tokens across the ring perimeter (for example, 100 to 250 virtual tokens per physical node).
With virtual nodes, when a physical server fails, its virtual nodes are scattered interleaved across the entire circle perimeter. Consequently, its lost request load redistributes evenly across all remaining cluster servers rather than crushing a single adjacent neighbor.
While virtual nodes ensure structural balance (an equal count of keys per server), they do not solve traffic hotspots created by popular individual keys. For instance, in a ticketing application, an event page for a major concert receives millions of read requests per minute, creating a massive hotspot on whichever node holds that specific key.
System design engineers use three complementary techniques to neutralize traffic hotspots:
taylor_swift_concert_0 through taylor_swift_concert_9). This splits write and read operations across multiple distinct hash ring positions. Client queries scatter read requests across all salted variants and aggregate results.When adding a new node to a consistent hash ring, only a fraction of keys need to move. If a cluster has N active physical nodes and introduces 1 new node, approximately 1 / (N + 1) of total keys are transferred.
During node addition, the new node connects to its clockwise neighbor, identifies the specific key range segment it is inheriting, and streams that data background segment asynchronously. Client reads fall back to the old owner until data streaming completes, ensuring zero service disruption during cluster scaling.
Apache Cassandra uses a consistent hashing ring to manage partitioned data across peer to peer cluster nodes. Cassandra configures Murmur3Partitioner to assign 64 bit integer tokens across nodes. Modern Cassandra deployments utilize virtual nodes (vnodes) by default, allowing seamless node replacement, automated repair streaming, and proportional data distribution based on heterogeneous disk capacity.
Amazon DynamoDB relies on consistent hashing principles within its internal request routing layer. DynamoDB storage nodes are partitioned across internal rings. As tables grow in storage volume or throughput capacity, DynamoDB partition split coordinators dynamically split token ranges and reassign storage segments without impacting application request latency.
Unlike pure consistent hashing, Redis Cluster uses a fixed hash slot model. Redis Cluster divides the entire key space into exactly 16,384 fixed slots using the formula CRC16(key) mod 16384. These 16,384 slots are distributed among physical cluster nodes. While consistent hashing allows dynamic continuous token placement, Redis chose fixed hash slots because explicit slot assignments make operational rebalancing, cluster resharding, and cluster state broadcasting significantly simpler to manage in memory.
Global CDNs employ consistent hashing within edge caching proxy tiers. When web requests hit edge points of presence, edge routers hash incoming URL strings to select specific caching proxy servers. Consistent hashing ensures that when an edge caching proxy is restarted or replaced, only a tiny fraction of cached web assets are invalidated, keeping cache hit ratios high across global networks.
You have mastered Consistent Hashing! Apply these architectural patterns in your next system design interview to build scalable, resilient distributed systems.