続 memcached-client with ConsistentHashing
ちゃんと検証した
4つのサーバに1000個の値を追加した後、サーバを5つに増やした時のミスキャッシュ率
require 'rubygems' require 'memcache' require 'mem_cache_with_consistent_hashing' COUNT = 1000 def set m m.flush_all COUNT.times {|v| m.set("key#{v}", "value#{v}") } end def number_of_miss m miss = 0 COUNT.times {|v| v = m.get("key#{v}", "value#{v}") miss += 1 unless v } p "#{miss}/#{COUNT} miss" end servers = %w[localhost:11211 localhost:11212 localhost:11213 localhost:11214] m = MemCache::new m.servers = servers set m n = MemCache::new n.servers = servers | ['localhost:11215'] number_of_miss n m = MemCacheWithConsistentHashing.new m.servers = servers set m n = MemCacheWithConsistentHashing.new n.servers = servers | ['localhost:11215'] number_of_miss n
結果
"787/1000 miss" # object.hash % m.servers.size "191/1000 miss" # consistent_hashing
これで安心してサーバを追加できる。