views:

14

answers:

1

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.

+1  A: 

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.

Harper Shelby
Right, so it will transparently do this if there are multiple servers in the pool? get_multi() will automatically retrieve the right keys from the right servers?
ensnare
That's what it's supposed to do - the memcached library does that under the covers. If it's not doing it, then there's a bug somewhere.
Harper Shelby
That's great ... I didn't know it automatically did this. I thought I had to write a wrapper class. Thanks.
ensnare