Consistent Hashing
Think of a library with four rooms, where each book goes to the room that its code points to. If the library opens a fifth room and changes the rule, almost every book must move. Consistent hashing is a rule that keeps most books where they are.
6 min read6 মিনিটে পড়া যায় · 7 sections7টি সেকশন · 3 sources3টি সোর্স · updated Nov 2026আপডেট: Nov 2026
Consistent hashing on a ringরিংয়ে কনসিসটেন্ট হ্যাশিং
Add and remove a server, and count how many keys have to move.একটা সার্ভার যোগ করুন বা সরান, আর গুনুন কয়টা কী-কে সরতে হয়।
You’ll learnআপনি শিখবেন
- Hash keys onto a ringকী-গুলোকে হ্যাশ করে রিংয়ে বসানো✓
- Find a key's server by walking clockwiseঘড়ির কাঁটার দিকে হেঁটে কীয়ের সার্ভার খোঁজা
- Count how few keys move when a server joinsসার্ভার যোগ হলে কত কম কী সরে তা গোনা
- See why modulo moves most keysমডুলো কেন বেশিরভাগ কী সরায় তা বোঝা
- Use virtual nodes to even out the loadভার্চুয়াল নোডে লোড সমান করা
In plain words
A hash function turns a key (such as a user id) into a number. A simple way to pick a server is to take that number modulo the number of servers. The weak point is a change in the server count. With a plain modulo hash, almost every item hashes to a new place when the range changes, and cached data becomes useless Source 1সোর্স 1. Cassandra's documentation says the same: adding one node can invalidate almost all mappings Source 3সোর্স 3.
Consistent hashing is a hash scheme that changes minimally when the set of buckets (servers) changes Source 1সোর্স 1. Changing the bucket set a little does not remap all the items Source 1সোর্স 1. When the number of nodes changes, only a small fraction of the keys must move Source 3সোর্স 3.
সহজ English
A hash function turns a key (such as a user id) into a number. A simple way to pick a server is to take that number modulo the number of servers. The weak point is a change in the server count. With a plain modulo hash, almost every item hashes to a new place when the range changes. Then cached data becomes useless Source 1সোর্স 1. The Cassandra documentation says the same: adding one node can invalidate almost all mappings Source 3সোর্স 3.
Consistent hashing is a hash scheme that changes minimally when the set of buckets (servers) changes Source 1সোর্স 1. A small change to the set of buckets does not remap all the items Source 1সোর্স 1. When the number of nodes changes, only a small fraction of the keys must move Source 3সোর্স 3.
বাংলা ব্যাখ্যা
হ্যাশ ফাংশন (hash function) একটি কী (key), যেমন ইউজার আইডি, একটি সংখ্যায় বদলায়। সার্ভার বাছার একটি সহজ উপায় হলো সেই সংখ্যাকে সার্ভারের সংখ্যা দিয়ে মডুলো (modulo) করা। দুর্বলতা হলো সার্ভারের সংখ্যা বদলানো। সাধারণ মডুলো হ্যাশে রেঞ্জ বদলালে প্রায় প্রতিটি আইটেম নতুন জায়গায় যায়। তখন ক্যাশ করা ডেটা অকেজো হয়ে যায় Source 1সোর্স 1। Cassandra-র ডকুমেন্টেশনও একই কথা বলে: একটি নোড যোগ করলে প্রায় সব ম্যাপিং বাতিল হতে পারে Source 3সোর্স 3।
কনসিসটেন্ট হ্যাশিং (consistent hashing) এমন একটি হ্যাশ স্কিম, যেখানে বাকেটের (সার্ভারের) সেট বদলালে পরিবর্তন সবচেয়ে কম হয় Source 1সোর্স 1। বাকেটের সেটে ছোট পরিবর্তন করলে সব আইটেম আবার ম্যাপ করতে হয় না Source 1সোর্স 1। নোডের সংখ্যা বদলালে কী-এর শুধু ছোট একটি অংশ সরাতে হয় Source 3সোর্স 3।
When you need it
You need it when data or cache entries are spread over many servers, and servers join or leave from time to time. The original paper was about caches for the web Source 1সোর্স 1. Amazon's Dynamo key-value store uses it to spread data over its storage hosts Source 2সোর্স 2. Apache Cassandra uses it to split data over its nodes Source 3সোর্স 3.
সহজ English
You need it when data or cache entries are spread over many servers, and servers join or leave from time to time. The original paper was about caches for the web Source 1সোর্স 1. The Dynamo key-value store of Amazon uses it to spread data over its storage hosts Source 2সোর্স 2. Apache Cassandra uses it to split data over its nodes Source 3সোর্স 3.
বাংলা ব্যাখ্যা
ডেটা বা ক্যাশ এন্ট্রি অনেক সার্ভারে ছড়িয়ে থাকলে এবং মাঝে মাঝে সার্ভার যোগ হলে বা চলে গেলে এটি দরকার। মূল পেপার ছিল ওয়েবের ক্যাশ নিয়ে Source 1সোর্স 1। Amazon-এর Dynamo কী-ভ্যালু স্টোর এটি দিয়ে তার স্টোরেজ হোস্টগুলোতে ডেটা ছড়ায় Source 2সোর্স 2। Apache Cassandra এটি দিয়ে তার নোডগুলোতে ডেটা ভাগ করে Source 3সোর্স 3।
How it works
The ring. Dynamo describes the idea this way. The values that the hash function can give are laid out as a fixed circle, a ring, where the largest value wraps around to the smallest Source 2সোর্স 2. Each node gets a random value in this space, which is its position on the ring Source 2সোর্স 2. To store a key, you hash the key to a position, then walk the ring clockwise to the first node with a larger position Source 2সোর্স 2. Each node is then responsible for the region between it and the node before it Source 2সোর্স 2.
Replication. Cassandra uses the same walk, and calls the ring positions tokens Source 3সোর্স 3. For replication, it keeps walking until it meets as many distinct nodes as the replication factor Source 3সোর্স 3. With a factor of 3, it walks to three distinct nodes, and these hold the replicas Source 3সোর্স 3.
Tutorial example: positions 0 to 99. Clockwise means "up", and 99 wraps to 0.
0 10(A) 40(B) 70(C) 90(D) 99
|---------|------------|------------|------------|----------|
keys 91-99, 0-10 -> A 11-40 -> B 41-70 -> C 71-90 -> D
A new server E joins at position 55:
0 10(A) 40(B) 55(E) 70(C) 90(D) 99
keys 41-55 move from C to E. All other keys stay where they were.Why few keys move
In the example above, the ring and the positions are the tutorial's own. When E joins, only the keys between B and E change owner. This is the rule that Dynamo states: the arrival or departure of a node affects only its immediate neighbors, and the other nodes stay unaffected Source 2সোর্স 2.
Monotonicity. The paper names a related rule, monotonicity. When new buckets are added, an item can move to a new bucket, but never between two old buckets Source 1সোর্স 1.
Smoothness. The paper also describes a smoothness goal. When a machine joins or leaves, the expected share of objects that move is the minimum needed to keep the load balanced Source 1সোর্স 1.
Virtual nodes
The basic ring has two weak points. Random positions give uneven data and load, and the basic scheme ignores the fact that nodes differ in performance Source 2সোর্স 2. The fix in Dynamo is virtual nodes: a node is not one point on the ring but several points, which Dynamo calls tokens Source 2সোর্স 2. A virtual node looks like one node to the rest of the system Source 2সোর্স 2.
The Dynamo paper lists these benefits Source 2সোর্স 2:
- When a node is unavailable, its load is spread over the other nodes.
- A new or returning node takes about the same share of work from every other node.
- The number of virtual nodes of a machine can follow its capacity.
Small clusters. Cassandra adds a view from the small cluster. With few nodes and evenly spaced tokens, it is hard to add just one node and keep the ring balanced Source 3সোর্স 3. With a node that owns many tokens, a new node takes many small pieces of data from many neighbors Source 3সোর্স 3. The Karger paper also gives each bucket more than one point on its unit interval Source 1সোর্স 1.
Cost of many tokens. Many tokens have a cost. Each token adds up to 2 x (RF - 1) neighbors on the ring Source 3সোর্স 3. So more combinations of node failures can make part of the ring unavailable, and cluster maintenance can be slower Source 3সোর্স 3.
সহজ English
Dynamo describes the idea this way. The values that the hash function can give are laid out as a fixed circle, a ring. The largest value wraps around to the smallest Source 2সোর্স 2. Each node gets a random value in this space. This value is its position on the ring Source 2সোর্স 2. To store a key, you hash the key to a position. Then you walk the ring clockwise to the first node with a larger position Source 2সোর্স 2. Each node is then responsible for the region between it and the node before it Source 2সোর্স 2.
Cassandra uses the same walk. It calls the ring positions tokens Source 3সোর্স 3. For replication, it keeps walking until it meets as many distinct nodes as the replication factor. With a factor of 3, it walks to three distinct nodes, and these hold the replicas Source 3সোর্স 3.
Why few keys move. This example is the tutorial's own. The positions are 0 to 99, and 99 wraps to 0. Servers A, B, C, and D sit at 10, 40, 70, and 90. Keys 91 to 99 and 0 to 10 go to A. Keys 11 to 40 go to B. Keys 41 to 70 go to C. Keys 71 to 90 go to D. A new server E joins at position 55. Keys 41 to 55 move from C to E. All other keys stay where they were. So only the keys between the new server and the node before it change owner. Dynamo states this rule: the arrival or departure of a node affects only its immediate neighbors, and the other nodes stay unaffected Source 2সোর্স 2. The paper names a related rule, monotonicity. When new buckets are added, an item can move to a new bucket, but never between two old buckets Source 1সোর্স 1. The paper also describes a smoothness goal. When a machine joins or leaves, the expected share of objects that move is the minimum needed to keep the load balanced Source 1সোর্স 1.
Virtual nodes. The basic ring has two weak points. Random positions give uneven data and load. The basic scheme also ignores the fact that nodes differ in performance Source 2সোর্স 2. The fix in Dynamo is virtual nodes. A node is not one point on the ring but several points, which Dynamo calls tokens Source 2সোর্স 2. A virtual node looks like one node to the rest of the system Source 2সোর্স 2.
The Dynamo paper lists these benefits Source 2সোর্স 2:
- When a node is unavailable, its load is spread over the other nodes.
- A new or returning node takes about the same share of work from every other node.
- The number of virtual nodes of a machine can follow its capacity.
Cassandra adds a view from the small cluster. With few nodes and evenly spaced tokens, it is hard to add just one node and keep the ring balanced Source 3সোর্স 3. If a node owns many tokens, a new node takes many small pieces of data from many neighbors Source 3সোর্স 3. The Karger paper also gives each bucket more than one point on its unit interval Source 1সোর্স 1.
Many tokens have a cost. Each token adds up to 2 x (RF - 1) neighbors on the ring. So more combinations of node failures can make part of the ring unavailable, and cluster maintenance can be slower Source 3সোর্স 3.
বাংলা ব্যাখ্যা
Dynamo ধারণাটি এভাবে বর্ণনা করে। হ্যাশ ফাংশন যে মান দিতে পারে সেগুলো একটি স্থির বৃত্তে সাজানো থাকে, যার নাম রিং (ring)। সবচেয়ে বড় মান ঘুরে সবচেয়ে ছোট মানে মিলে যায় Source 2সোর্স 2। প্রতিটি নোড এই জায়গায় একটি র্যান্ডম মান পায়। এই মান রিংয়ে তার অবস্থান Source 2সোর্স 2। একটি কী রাখতে হলে কী-কে হ্যাশ করে একটি অবস্থান পাওয়া যায়। তারপর রিংয়ে ঘড়ির কাঁটার দিকে হেঁটে বড় অবস্থানের প্রথম নোডে পৌঁছাতে হয় Source 2সোর্স 2। তখন প্রতিটি নোড দায়ী থাকে নিজের আর তার আগের নোডের মাঝের অংশের জন্য Source 2সোর্স 2।
Cassandra একই পথে হাঁটে। সে রিংয়ের অবস্থানগুলোকে টোকেন (token) বলে Source 3সোর্স 3। রিপ্লিকেশনের জন্য সে হাঁটতে থাকে, যতক্ষণ না রিপ্লিকেশন ফ্যাক্টরের সমান আলাদা নোড পায়। ফ্যাক্টর 3 হলে সে তিনটি আলাদা নোড পর্যন্ত যায়, এবং এরা রেপ্লিকা (replica) রাখে Source 3সোর্স 3।
কেন কম কী সরে। এই উদাহরণ টিউটোরিয়ালের নিজের। অবস্থান 0 থেকে 99, আর 99 ঘুরে 0-তে মেলে। A, B, C, D সার্ভার আছে 10, 40, 70, 90 অবস্থানে। 91 থেকে 99 আর 0 থেকে 10 কী A-তে যায়, 11 থেকে 40 কী B-তে, 41 থেকে 70 কী C-তে, এবং 71 থেকে 90 কী D-তে। নতুন সার্ভার E যোগ হয় 55 অবস্থানে। 41 থেকে 55 কী C থেকে E-তে সরে। বাকি সব কী আগের জায়গাতেই থাকে। তাই শুধু নতুন সার্ভার আর তার আগের নোডের মাঝের কী-গুলোর মালিক বদলায়। Dynamo এই নিয়ম বলে: একটি নোডের আসা বা যাওয়া শুধু তার সরাসরি প্রতিবেশীদের প্রভাবিত করে, বাকি নোডগুলো অপ্রভাবিত থাকে Source 2সোর্স 2। পেপারটি একটি সম্পর্কিত নিয়মের নাম দেয়, মনোটনিসিটি (monotonicity)। নতুন বাকেট যোগ হলে একটি আইটেম নতুন বাকেটে যেতে পারে, কিন্তু দুটি পুরোনো বাকেটের মধ্যে কখনো যায় না Source 1সোর্স 1। পেপারটি একটি স্মুথনেস (smoothness) লক্ষ্যও বর্ণনা করে। একটি মেশিন যোগ হলে বা চলে গেলে যে অবজেক্টগুলো সরে তার প্রত্যাশিত অংশ হয় লোড ব্যালেন্সড রাখার জন্য ন্যূনতম যতটা দরকার ততটা Source 1সোর্স 1।
ভার্চুয়াল নোড (virtual node)। সাধারণ রিংয়ের দুটি দুর্বলতা আছে। র্যান্ডম অবস্থান অসম ডেটা আর লোড দেয়। আর সাধারণ স্কিম এই বিষয়টি উপেক্ষা করে যে নোডগুলোর পারফরম্যান্স আলাদা Source 2সোর্স 2। Dynamo-তে সমাধান হলো ভার্চুয়াল নোড। একটি নোড রিংয়ে একটি বিন্দু নয়, বরং কয়েকটি বিন্দু, যাদের Dynamo টোকেন বলে Source 2সোর্স 2। বাকি সিস্টেমের কাছে একটি ভার্চুয়াল নোড একটি নোডের মতোই দেখায় Source 2সোর্স 2।
Dynamo পেপার এই সুবিধাগুলো বলে Source 2সোর্স 2:
- একটি নোড পাওয়া না গেলে তার লোড বাকি নোডগুলোতে ছড়িয়ে যায়।
- নতুন বা ফিরে আসা নোড প্রতিটি অন্য নোড থেকে প্রায় সমান অংশ কাজ নেয়।
- একটি মেশিনের ভার্চুয়াল নোডের সংখ্যা তার ক্যাপাসিটি অনুসারে হতে পারে।
Cassandra ছোট ক্লাস্টারের দিক থেকে আরেকটি কথা যোগ করে। কম নোড আর সমান দূরত্বের টোকেন থাকলে শুধু একটি নোড যোগ করে রিংকে ব্যালেন্সড রাখা কঠিন Source 3সোর্স 3। একটি নোডের অনেক টোকেন থাকলে নতুন নোড অনেক প্রতিবেশী থেকে অনেক ছোট ছোট ডেটার টুকরো নেয় Source 3সোর্স 3। Karger পেপারও প্রতিটি বাকেটকে তার ইউনিট ইন্টারভালে (unit interval) একাধিক বিন্দু দেয় Source 1সোর্স 1।
অনেক টোকেনের একটি খরচ আছে। প্রতিটি টোকেন রিংয়ে সর্বোচ্চ 2 x (RF - 1) প্রতিবেশী যোগ করে। তাই নোড ফেইলিউরের আরও বেশি কম্বিনেশনে রিংয়ের একটি অংশ পাওয়া নাও যেতে পারে, এবং ক্লাস্টারের রক্ষণাবেক্ষণ ধীর হতে পারে Source 3সোর্স 3।
A real-life example
A test team runs a cache with four servers. Tutorial example, with simple numbers: keys have the hash values 0 to 19. With the rule "hash modulo 4", a fifth server changes the rule to "hash modulo 5". Only 4 of these 20 keys (hash values 0 to 3) land on the same server as before. The other 16 keys, 80 %, move, and a cache looks empty for them.
With a ring, as in the diagram, adding the fifth server moves only the keys that fall between its neighbor and its own position. The team sees a few misses and not a full cold cache.
সহজ English
A test team runs a cache with four servers. This is the tutorial's own example, with simple numbers. The keys have the hash values 0 to 19. With the rule "hash modulo 4", a fifth server changes the rule to "hash modulo 5". Only 4 of these 20 keys (hash values 0 to 3) land on the same server as before. The other 16 keys (80 %) move, and a cache looks empty for them.
With a ring, as in the example above, adding the fifth server moves only the keys that fall between its neighbor and its own position. The team sees a few misses and not a full cold cache.
বাংলা ব্যাখ্যা
একটি টেস্ট টিম চারটি সার্ভার সহ একটি ক্যাশ চালায়। এটি টিউটোরিয়ালের নিজের উদাহরণ, সহজ সংখ্যা সহ। কী-গুলোর হ্যাশ মান 0 থেকে 19। "hash modulo 4" নিয়মে পঞ্চম সার্ভার আসলে নিয়ম হয় "hash modulo 5"। এই 20টি কী-এর মধ্যে শুধু 4টি (হ্যাশ মান 0 থেকে 3) আগের সার্ভারেই থাকে। বাকি 16টি কী (80 %) সরে যায়, এবং তাদের জন্য ক্যাশ খালি দেখায়।
উপরের রিংয়ের উদাহরণের মতো রিং থাকলে পঞ্চম সার্ভার যোগ করায় শুধু সেই কী-গুলো সরে যা তার প্রতিবেশী আর তার নিজের অবস্থানের মাঝে পড়ে। টিম কয়েকটি ক্যাশ মিস (cache miss) দেখে, পুরো কোল্ড ক্যাশ (cold cache) নয়।
Common mistakes
- Using hash modulo N and thinking the cache survives a resize. A change of N moves almost all keys Source 1সোর্স 1.
- Expecting a perfect balance from one point per node. Random positions give uneven load Source 2সোর্স 2.
- Adding many tokens without a limit. More tokens raise the number of ring neighbors and the chance of an outage Source 3সোর্স 3.
- Treating the ring as a safety net. The ring decides where a key lives. It does not copy data. Replication is a separate step Source 3সোর্স 3.
সহজ English
- Using hash modulo N and thinking the cache survives a resize. A change of N moves almost all keys Source 1সোর্স 1.
- Expecting a perfect balance from one point per node. Random positions give uneven load Source 2সোর্স 2.
- Adding many tokens without a limit. More tokens raise the number of ring neighbors and the chance of an outage Source 3সোর্স 3.
- Treating the ring as a safety net. The ring decides where a key lives. It does not copy data. Replication is a separate step Source 3সোর্স 3.
বাংলা ব্যাখ্যা
- hash modulo N ব্যবহার করে ভাবা যে রিসাইজে ক্যাশ টিকে থাকবে। N বদলালে প্রায় সব কী সরে যায় Source 1সোর্স 1।
- নোড প্রতি একটি বিন্দু থেকে নিখুঁত ব্যালেন্স আশা করা। র্যান্ডম অবস্থান অসম লোড দেয় Source 2সোর্স 2।
- সীমা ছাড়া অনেক টোকেন যোগ করা। বেশি টোকেন রিংয়ের প্রতিবেশীর সংখ্যা এবং আউটেজের সম্ভাবনা বাড়ায় Source 3সোর্স 3।
- রিংকে সেফটি নেট ভাবা। রিং ঠিক করে কী কোথায় থাকবে। সে ডেটা কপি করে না। রিপ্লিকেশন আলাদা একটি ধাপ Source 3সোর্স 3।
What a tester can check
Tutorial advice
These test ideas are the tutorial's own advice.
- Count the moved keys. Store a known set of keys. Add one node. Check that most keys keep their owner and that only a small share moves.
- Remove a node. Check that reads for its keys still work, from the node that now owns that range.
- Check the balance. Count keys for each node. A big gap may point to a poor token setup.
- Test during the change. Read and write while a node joins, and look for misses and errors.
সহজ English
These test ideas are the tutorial's own advice.
- Count the moved keys. Store a known set of keys. Add one node. Check that most keys keep their owner. Check that only a small share moves.
- Remove a node. Check that reads for its keys still work, from the node that now owns that range.
- Check the balance. Count the keys for each node. A big gap may point to a poor token setup.
- Test during the change. Read and write while a node joins. Look for misses and errors.
বাংলা ব্যাখ্যা
এই টেস্টের ধারণাগুলো টিউটোরিয়ালের নিজের পরামর্শ।
- সরে যাওয়া কী গুনুন। পরিচিত একটি কী-এর সেট রাখুন। একটি নোড যোগ করুন। দেখুন বেশিরভাগ কী তাদের মালিক রাখে কি না। দেখুন শুধু ছোট একটি অংশ সরে কি না।
- একটি নোড সরান। দেখুন তার কী-গুলোর রিড এখনো কাজ করে কি না, এখন যে নোড ওই রেঞ্জের মালিক তার কাছ থেকে।
- ব্যালেন্স দেখুন। প্রতিটি নোডের কী গুনুন। বড় ফারাক খারাপ টোকেন সেটআপের ইঙ্গিত হতে পারে।
- পরিবর্তনের সময় টেস্ট করুন। নোড যোগ হওয়ার সময় রিড আর রাইট করুন। মিস আর এরর খুঁজুন।
Key words
| Word | Meaning |
|---|---|
| Consistent hashing | A hash scheme that changes minimally when the set of buckets changes Source 1সোর্স 1 |
| Ring | The hash range as a circle, where the largest value wraps to the smallest Source 2সোর্স 2 |
| Token | One position of a node on the ring Source 3সোর্স 3 |
| Virtual node (vnode) | One of several ring positions that a physical node owns Source 3সোর্স 3 |
| Token range | A range of keys that maps to the same set of nodes Source 3সোর্স 3 |
| Replication factor (RF) | The number of nodes that hold a copy of a key Source 3সোর্স 3 |