<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Design Consistent Hashing]]></title><description><![CDATA[Design Consistent Hashing]]></description><link>https://consistenthashing.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 14 Oct 2026 03:00:20 GMT</lastBuildDate><atom:link href="https://consistenthashing.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[🔁 System Design Series: Design Consistent Hashing]]></title><description><![CDATA[🚀 Introduction
In distributed systems, we often need to distribute data across multiple machines or servers. But what happens when a new server is added or removed?
A naive hash function would require almost complete data reallocation, which is extr...]]></description><link>https://consistenthashing.hashnode.dev/system-design-series-design-consistent-hashing</link><guid isPermaLink="true">https://consistenthashing.hashnode.dev/system-design-series-design-consistent-hashing</guid><category><![CDATA[System Design]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Java]]></category><category><![CDATA[Node.js]]></category><category><![CDATA[Express]]></category><category><![CDATA[CDN]]></category><category><![CDATA[Springboot]]></category><category><![CDATA[C#]]></category><category><![CDATA[C]]></category><category><![CDATA[C++]]></category><category><![CDATA[Developer]]></category><dc:creator><![CDATA[Yashveer Singh]]></dc:creator><pubDate>Sun, 06 Jul 2025 18:42:24 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1751826808966/04297973-1367-4c64-8987-c6f5ba9d0e41.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction"><strong>🚀 Introduction</strong></h2>
<p>In distributed systems, we often need to distribute data across multiple machines or servers. But what happens when a new server is added or removed?</p>
<p>A naive hash function would require almost <strong>complete data reallocation</strong>, which is extremely inefficient at scale.</p>
<p>This is where <strong>consistent hashing</strong> comes in — a beautifully elegant technique used by large-scale systems like <strong>Amazon Dynamo</strong>, <strong>Cassandra</strong>, and <strong>Redis Cluster</strong> to maintain balance and minimize re-distribution when nodes change.</p>
<p>In this post, we’ll break down:</p>
<ul>
<li><p>What consistent hashing is</p>
</li>
<li><p>Why it matters in system design</p>
</li>
<li><p>How it works conceptually</p>
</li>
<li><p>Real-world use cases and benefits</p>
</li>
<li><p>Trade-offs and design extensions</p>
</li>
</ul>
<h2 id="heading-the-problem-with-traditional-hashing"><strong>🧠 The Problem with Traditional Hashing</strong></h2>
<p>Let’s say you use the simplest way to distribute keys:</p>
<p>key % number_of_servers</p>
<p>If you have 4 servers (0, 1, 2, 3), the key user123 might go to:</p>
<pre><code class="lang-plaintext">hash(user123) % 4 → Server 2
</code></pre>
<p>But what if you add a 5th server?</p>
<p>Now, hash(user123) % 5 could result in:</p>
<pre><code class="lang-plaintext">→ Server 3 (not 2 anymore!)
</code></pre>
<p><strong>Result:</strong></p>
<p>Almost all keys are remapped to different servers — <strong>massive data movement</strong>, cache misses, and inefficiency.</p>
<p>This is why we need <strong>consistent hashing</strong>.</p>
<h2 id="heading-what-is-consistent-hashing"><strong>🔄 What is Consistent Hashing?</strong></h2>
<p>Consistent hashing is a <strong>key distribution algorithm</strong> that maps both data and servers onto the <strong>same hash ring</strong>.</p>
<p>Instead of assigning keys to servers based on modulo, it places both keys and nodes on a circular “hash space” (usually from 0 to 2³²-1).</p>
<blockquote>
<p><strong>Keys are assigned to the closest server in a clockwise direction.</strong></p>
</blockquote>
<hr />
<h3 id="heading-visualizing-the-hash-ring"><strong>🔁 Visualizing the Hash Ring</strong></h3>
<p>Imagine a circle numbered from 0 to MAX_HASH (e.g., 2³²).</p>
<ul>
<li><p>Each server is assigned a position on the circle via a hash function.</p>
</li>
<li><p>Each key is also hashed and placed on the circle.</p>
</li>
<li><p>A key belongs to the first server <strong>clockwise</strong> from its position.</p>
</li>
</ul>
<p>So even if a server is added or removed, <strong>only a small subset of keys</strong> needs to be re-mapped.</p>
<hr />
<h2 id="heading-how-it-works-step-by-step"><strong>📌 How It Works – Step by Step</strong></h2>
<ol>
<li><p><strong>Hash Function:</strong></p>
<p> Both servers and data keys are hashed using the same function.</p>
</li>
<li><p><strong>Placement on Circle:</strong></p>
<p> All hash outputs are mapped to points on a circle (e.g., 0 to 2³²).</p>
</li>
<li><p><strong>Assigning Keys:</strong></p>
<p> Each key is stored in the first server found when moving clockwise from the key’s position on the circle.</p>
</li>
<li><p><strong>Server Add/Remove:</strong></p>
<p> When a new server is added, only the keys between the new server and its predecessor are reallocated.</p>
<p> The rest of the keys stay untouched.</p>
</li>
</ol>
<hr />
<h2 id="heading-example"><strong>🧪 Example</strong></h2>
<p>Assume servers A, B, and C are placed at positions:</p>
<ul>
<li><p>A → hash(A) = 100</p>
</li>
<li><p>B → hash(B) = 300</p>
</li>
<li><p>C → hash(C) = 600</p>
</li>
</ul>
<p>Key X → hash(X) = 350</p>
<p>So, X maps to <strong>Server C</strong>, the next clockwise node.</p>
<p>Now if you add <strong>Server D</strong> at hash = 400:</p>
<p>Only keys between 300 and 400 get reassigned to D.</p>
<p>Key X still goes to C — <strong>minimal disruption</strong>.</p>
<hr />
<h2 id="heading-why-consistent-hashing-is-useful"><strong>🎯 Why Consistent Hashing Is Useful</strong></h2>
<ol>
<li><p><strong>Scalable:</strong> Add or remove nodes with minimal rehashing.</p>
</li>
<li><p><strong>Load Balancing:</strong> Evenly distributes keys (with help of virtual nodes).</p>
</li>
<li><p><strong>Fault Tolerant:</strong> Easy to redistribute keys when nodes fail.</p>
</li>
<li><p><strong>Used in Real Systems:</strong> DynamoDB, Cassandra, Redis Cluster, Akamai, Memcached.</p>
</li>
</ol>
<hr />
<h2 id="heading-virtual-nodes-vnodes"><strong>🧩 Virtual Nodes (VNodes)</strong></h2>
<p>One downside of basic consistent hashing is that servers might be unevenly distributed on the hash ring.</p>
<p>To solve this, we introduce <strong>Virtual Nodes</strong>:</p>
<ul>
<li><p>Each physical server is assigned <strong>multiple virtual positions</strong> on the ring.</p>
</li>
<li><p>When a server joins or leaves, the load is <strong>spread more evenly</strong>.</p>
</li>
<li><p>Virtual nodes improve balancing and reduce hot spots.</p>
</li>
</ul>
<p>Example:</p>
<p>Server A has 3 virtual nodes at hash positions: 101, 205, 999</p>
<p>Each vnode is treated as a separate node in key assignment.</p>
<hr />
<h2 id="heading-implementation-details"><strong>⚙️ Implementation Details</strong></h2>
<ul>
<li><p><strong>Hash Function:</strong> Use a uniform and fast hash like SHA-1 or MurmurHash.</p>
</li>
<li><p><strong>Data Replication:</strong> Often, data is stored on <strong>multiple successor nodes</strong> for redundancy.</p>
</li>
<li><p><strong>Rebalancing:</strong> Only small portions of data move during topology changes.</p>
</li>
</ul>
<hr />
<h2 id="heading-real-world-use-cases"><strong>📦 Real-World Use Cases</strong></h2>
<h3 id="heading-distributed-caches-eg-memcached-redis-cluster"><strong>🧵 Distributed Caches (e.g., Memcached, Redis Cluster)</strong></h3>
<p>Ensures cache keys are distributed fairly, and cache misses are minimized during scaling.</p>
<h3 id="heading-dynamo-style-databases"><strong>🌩️Dynamo-Style Databases</strong></h3>
<p>Amazon Dynamo, Cassandra, and Scylla use consistent hashing for partitioning and replication.</p>
<h3 id="heading-content-delivery-networks-cdns"><strong>🧭Content Delivery Networks (CDNs)</strong></h3>
<p>Route users to the right edge node based on consistent hash of content or IP.</p>
<hr />
<h2 id="heading-interview-tips"><strong>🧠 Interview Tips</strong></h2>
<p>In a system design interview, if you’re asked to:</p>
<ul>
<li><p>Build a <strong>distributed cache</strong></p>
</li>
<li><p>Design a <strong>database sharding strategy</strong></p>
</li>
<li><p>Distribute data among <strong>multiple storage nodes</strong></p>
</li>
</ul>
<p>…then bringing up consistent hashing (and why you’d use it) shows strong architectural thinking.</p>
<p>👉 Mention concepts like:</p>
<ul>
<li><p>“Only K/N keys are re-mapped on server change.”</p>
</li>
<li><p>“We’d use virtual nodes for better balance.”</p>
</li>
<li><p>“Hash ring allows us to scale out gracefully.”</p>
</li>
</ul>
<hr />
<h2 id="heading-summary"><strong>✅ Summary</strong></h2>
<p>Consistent hashing is a <strong>cornerstone of scalable, fault-tolerant distributed systems</strong>. It allows systems to:</p>
<ul>
<li><p>Avoid complete key reshuffling</p>
</li>
<li><p>Balance load across servers</p>
</li>
<li><p>Recover quickly from node failures</p>
</li>
<li><p>Grow without significant re-architecture</p>
</li>
</ul>
<p>If you’re working on a backend system that requires data distribution or partitioning — this is a concept you’ll use or be asked to design</p>
]]></content:encoded></item></channel></rss>