Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multiple memcache servers in a pool

I'm going through the documentation and I'm a little confused as to how memcache does internal load-balancing if multiple servers are specified. For example:

import memcache
mc.set_servers(['127.0.0.1:11211','127.0.0.1:11212',])
mc.set("some_key", "Some value")
print mc.get("some_key")

Will the setting and retrieval of key "some_key" always hit the same server? Will the setting and retrieval of alternate keys, such as "some_key_2" or "some_key_3," automatically be distributed amongst the pool of servers? What happens if a server is added or deleted?

Similarly, what happens with get_multi:

import memcache
mc.set_servers(['127.0.0.1:11211','127.0.0.1:11212',])
mc.set_multi({42: 'adams', 46 : 'and of me'})
print mc.get_multi([46, 42])

Will this automatically set and retrieve each key from the right server? Is it necessary to write a wrapper class?

Thanks.

like image 879
ensnare Avatar asked Oct 27 '10 22:10

ensnare


1 Answers

memcached places keys on servers based on a hash of the key. As long as your server setup doesn't change, then a given key will always land on a given server.

like image 96
Harper Shelby Avatar answered Oct 21 '22 04:10

Harper Shelby