今日已更新 168 条资讯 | 累计 21034 条内容
关于我们

Understanding Consistent Hashing Correct Me If I'm Wrong

/u/No-Resolution-4054 2026年06月06日 15:52 4 次阅读 来源:Reddit r/webdev

Why we need it ? Suppose we have multiple databases and want to distribute data among them. Instead of searching every database when we need some data, we use a rule that tells us exactly which database should store a particular record. Method 1: Modulo Based Distribution Imagine we have 3 databases DB-A , DB-B , DB-C Each record has a unique ID. Now We decide the database using ID % Number_of_Databases for e.g. 16 % 3 = 1 So record 16 goes to database index 1 (DB-B). This works fine until we add another database. The same record becomes 16 % 4 = 0 Now record 16 should be stored in DB-A instead of DB-B. The problem is that when the number of databases changes, a huge amount of data gets remapped to different databases. This can cause Massive data migration , Increased CPU and network usage Method 2: Consistent Hashing Instead of using modulo, imagine a circular ring numbered from 0 to 99. We place our databases on the ring: DB-A -> 0 DB-B -> 25 DB-C -> 50 DB-D -> 75 Now we pass the data unique id through a hash function and it will give the location of that data on the ring for e.g. User ID = 12345 hash(12345) = 42 now we get the position we Move clockwise. Store the data in the first database you encounter This means we store the 42 position data at the DB-C Now What Happens When We Add a New Database? Suppose we add DB-E -> 37 now only the data between 26 to 37 needs to move from DB-C to DB-E. The rest of the data stays exactly where it was. This is the biggest advantage of Consistent Hashing much less data migration , easier scaling , lower operational cost Now there is one more thing in this method which is Virtual Nodes One issue is that some databases may receive much more traffic than others. To balance the load, the same database can appear multiple times on the ring. DB-A -> 0, 40, 80 DB-B -> 25, 65 DB-C -> 13, 50, 90 DB-D -> 75 These extra positions are called virtual nodes. Any corrections? Is there anything else I should know about this topic? Please let

本文内容来源于互联网,版权归原作者所有
查看原文