Multiple memcached servers question

前端 未结 2 591
鱼传尺愫
鱼传尺愫 2020-12-31 12:32

hypothetically - if I have multiple memcached servers like this:

//PHP 
$MEMCACHE_SERVERS = array(
    \"10.1.1.1\", //web1
    \"10.1.1.2\         


        
2条回答
  •  耶瑟儿~
    2020-12-31 13:04

    some days ago i was looking for a solution to optimize scaling of our memcached servers and found this answer. From experiences we had made, the descriped solution with generating a hash and MOD number of servers to find the target-server, isn't the best one.

    If you'll up- or downscale the number of your servers, it could likely result in the same scenario when flushing the cache. Most of the hashes get another server and so there won't be a result out of the cache for the first request.

    The best solution to use for such scenarios is consistent hashing. With consistent hashing, every server gets a fixed hashrange. So if you now up- or downscale the number of servers, only the hashes in this particular hashrange will be switched to another server. All other hashes remains to there servers and only a little part will be regenerated.

    For PHP there is a library called 'flexihash' which does the consistent hashing for you.

    In our Blog, you can find an example how to use it with your own cache-client. The article is in german but the sourcecode should be selfexplained.

提交回复
热议问题