· Joseph · Online Course  · 4 min read

System design - Consistent hashing

ByteByteGo這篇介紹的是一致性雜湊,一般來說會用到雜湊,目的是為了把流量透過固定的演算法,分散到某台機器上面,那什麼是一致性雜湊,不好的雜湊演算法又會有什麼問題呢?這篇筆記來說一下ByteByteGo怎麼介紹的。

ByteByteGo這篇介紹的是一致性雜湊,一般來說會用到雜湊,目的是為了把流量透過固定的演算法,分散到某台機器上面,那什麼是一致性雜湊,不好的雜湊演算法又會有什麼問題呢?這篇筆記來說一下ByteByteGo怎麼介紹的。

TOC

rehashing problem

對於一致性雜湊,Wikipedia的說法是:

Consistent hashing is a special kind of hashing such that when a hash table is re-sized and consistent hashing is used, only k/n keys need to be remapped on average, where k is the number of keys, and n is the number of slots. In contrast, in most traditional hash tables, a change in the number of array slots causes nearly all keys to be remapped

key4 Servers
(s0, s1, s2, s3)
3 Servers
(s0, s2, s3)
1s1s2
2s2s3
3s3s0
4s0s2
5s1s3
6s2s0
7s3s2
8s0s3

以上面的結果來看,4台servers跟s1 offline時的3台servers比起來,所有的user都會被雜湊函數重新分配,就不太符合Consistent hashing的定義,這時候就有了雜湊還hash ring的概念出現。

hash ring

hash ring

hash ring在key分配節點上,都以同個方向下去找到最適合的server,新增或移除server就只要把原先配對異常的key,用同個方向下去找到更適合的節點即可。 用這個方式有下面三個優點:

  • 最小化資料重新分配 — 當節點數改變時,只需要重新分配極少數 key。
  • 更好的可擴展性 — 不必為了 scale up/down 而重新編排大量資料。
  • 提高可靠性 — 節點失效時系統仍然能保持大部分 key 不變。

但可以想像他伴隨著兩個問題

  • 資料分佈不均: 由於雜湊的隨機性,伺服器在環上的位置可能不均勻,導致某些伺服器承載過多資料。
  • 串級失效: 某台伺服器故障後,其負荷會全部壓在下一台伺服器上

所以又衍伸出了Virtaul Nodes的進化版本。

Virtual Nodes

virtual-nodes現在加上了圓形的nodes,他不是真的機器,是當被hash雜湊配對到時,知道實際上要redirect去到哪個node。當然這也需要更多的空間來儲存關於虛擬節點的資料,真的用到這情境應該代表nodes數量相當多,需要增加的virtual nodes也是倍數關係,就需要一點trade-off了。

Conclusion

總結來說,一致性雜湊解決了傳統雜湊在伺服器數量變動時導致大量資料遷移的問題。透過 Hash Ring 的設計,我們能最小化資料的重新分配。進一步引入 Virtual Nodes 則有效解決了資料分佈不均與潛在的串級失效風險,雖然犧牲了一點儲存空間,但換來了更佳的負載平衡與系統穩定性。這在分散式系統設計(如 Dynamo, Cassandra)中是非常關鍵的概念。

If you like this post, please connect with me on LinkedIn and give me some encouragement. Thanks.

Related Posts

View All Posts »
System design - A rate limiter

System design - A rate limiter

這篇要介紹的是Rate Limiter,是ByteByteGo System design裡面第一個design環節。現在很多後端主流框架都有內建或有對應套件,但對應的演算法卻被封裝起來,知其然不知其所以然。 今天這篇靠AI生成點簡單的示意圖,讓一張圖道盡千言萬語。

Algorithm - hash map and set

Algorithm - hash map and set

import { YouTube } from 'astro-embed'; Set and Map 平常就有在用,map存的是Key-value pair、Set存的是unique value。而且操作上來說平均都是O(1)時間複雜度。這篇就來看看他們適合解哪些題型吧。

Two Pointers Algorithm

import demo from './demo.mp4'; import triplet from './triplet.mp4'; import palindrome from './palindrome.mp4'; import largest_container from './largest_container.mp4'; import shift_zero from './shift_zero.mp4'; import next_permutation from './next_permutation.mp4'; 最近買了ByteByteGo裡的Lifetime Plan方案,才把Coding interview patterns裡的Two pointers algorithm跟System design interview裡的Design A Rate Limiter念完,就先用這篇來筆記一下Two pointers algorithm

use-device-camera

use-device-camera

在用了Javascript串webcam一年之後,我做了這個use-device-camera的react npm package。