Search code examples
cachingmemcachedspymemcached

How to make sure that two spymemcached client store the same key to the same server?


The setup is suppose I have 2 memcached servers, and 2 web servers to connect to these two memcached servers. I am using spymemcached client.

Suppose:

1) web1 insert a key "abc" to memcached. Based on some mechanism it stored in memcached1.

2) When web2 tries to get key "abc" how does it know that it will go to memcached1 to get the key?

Do I need to do any special setting in spymemcached client side to make sure that the memcached server that will store a key is always determined?


Solution

  • You do not need any special logic to do this. Memcached is a distributed cache and works by hashing keys to different servers. As long as you list all of the servers in your memcached cluster you should have no problem.

    I also want to note that the one parameter you can change is the hashing algorithm that is used by the clients. This can be done in whatever ConnectionFactory class you use to build your connection.