SlideShare a Scribd company logo
1 of 20
Download to read offline
Consistent Hashing
についての小ネタ
@huydx
RightSegment
WIKI
コンシステントハッシュ法 (Consistent
hashing) はスロットの追加や削除に対して、
最小限のキーのスロットへのマッピングの変
更で、ハッシュテーブルの機能を提供すること
のできる特殊なハッシュ法。
問題の例:MySQL shard-ing
Server1 Server2 Server3
クライアント id 15持って
いる!
サーバ数 N = 3
問題の例:MySQL shard-ing
Server1 Server2 Server3
クライアント
サーバ数 N = 3
キーの場所 = hash(key) (mod N)
問題の例:MySQL shard-ing
Server1 Server2 Server3
クライアント
サーバ追加した! N = 4
Server4
全キーがシフトされる!!!!
キーの場所 = hash(key) (mod N+1)
できるだけマシンを追加する
ときに最低限のアイテムしか
インバリデートされていない
アルゴリズム欲しい!
Consistent Hashing 登場
キーポイント
キーだけではなく、サーバの場
所もハッシュする
同じ空間に同じハッシュアルゴ
リズムでやって、「近い」とい
う指標で場所を探せばよい
サーバの場所のハッシュ(bucket)
0 2^32-1
キーのハッシュ
0 2^32-1
近いところにアサイン!
この場合はなににアサインすべき?
丸のクロックフェースで
考えばよい!
なんかバランスなくない?
一つのサーバを複数にハッシュすればよい!
1 2 1 2
実装
• サーバを早く検索するために、RedBlack Tree
でサーバのハッシュ後のリストを使えばよい
• それで:追加、削除、検索、全てが O(logN)
かかる
val serverList = new TreeMap[String, Long]()
for (server <- servers) {
serverList.put(hash(server.ip), server.ip)
}
def findKey(key: String): String = {
serverList.tailMap(hash(key)).firstKey
}
ハッシュアルゴリズムはなにを
使えばよい?
⚫ 同じ値→同じハッシュ
⚫ Longの空間でのランダムさ
MD5を使ってハッシュして、
最初の4バイトをLongに変換
すればよい!
val md5 = MessageDigest.getInstance("MD5")
md5.reset()
md5.update(in.getBytes)
val bKey: Array[Byte] = md5.digest()
val res: Long =
(bKey(3) & 0xFF << 24 ).asInstanceOf[Long] |
(bKey(2) & 0xFF << 16 ).asInstanceOf[Long] |
(bKey(1) & 0xFF << 8 ).asInstanceOf[Long] |
(bKey(0) & 0xFF ).asInstanceOf[Long]
res
参考:
• http://www.metabrew.com/article/libketama-consistent-
hashing-algo-memcached-clients (ketamaのConsistent
Hashingでは各サーバのウェイトも設定できるってすごい!)
• http://people.csail.mit.edu/karger/Talks/Hash/sld027.htm
• https://www.akamai.com/us/en/multimedia/documents/
technical-publication/consistent-hashing-and-random-trees-
distributed-caching-protocols-for-relieving-hot-spots-on-the-
world-wide-web-technical-publication.pdf

More Related Content

More from Huy Do (10)

Distributed Tracing, from internal SAAS insights
Distributed Tracing, from internal SAAS insightsDistributed Tracing, from internal SAAS insights
Distributed Tracing, from internal SAAS insights
 
Write on memory TSDB database (gocon tokyo autumn 2018)
Write on memory TSDB database (gocon tokyo autumn 2018)Write on memory TSDB database (gocon tokyo autumn 2018)
Write on memory TSDB database (gocon tokyo autumn 2018)
 
Some note about GC algorithm
Some note about GC algorithmSome note about GC algorithm
Some note about GC algorithm
 
Engineering Efficiency in LINE
Engineering Efficiency in LINEEngineering Efficiency in LINE
Engineering Efficiency in LINE
 
GOCON Autumn (Story of our own Monitoring Agent in golang)
GOCON Autumn (Story of our own Monitoring Agent in golang)GOCON Autumn (Story of our own Monitoring Agent in golang)
GOCON Autumn (Story of our own Monitoring Agent in golang)
 
Story Writing Byte Serializer in Golang
Story Writing Byte Serializer in GolangStory Writing Byte Serializer in Golang
Story Writing Byte Serializer in Golang
 
CA15卒勉強会 メタプログラミングについて
CA15卒勉強会 メタプログラミングについてCA15卒勉強会 メタプログラミングについて
CA15卒勉強会 メタプログラミングについて
 
Making CLI app in ruby
Making CLI app in rubyMaking CLI app in ruby
Making CLI app in ruby
 
CacheとRailsの簡単まとめ
CacheとRailsの簡単まとめCacheとRailsの簡単まとめ
CacheとRailsの簡単まとめ
 
[Htmlday]present
[Htmlday]present[Htmlday]present
[Htmlday]present
 

Consistent Hashingの小ネタ