title: How does django handle multiple memcached servers?
body: <p>In the django documentation it says this:</p>
  
  <blockquote>
    <p>...</p>
    
    <p>One excellent feature of Memcached is its ability to share cache over
    multiple servers. This means you can run Memcached daemons on multiple
    machines, and the program will treat the group of machines as a single
    cache, without the need to duplicate cache values on each machine. To
    take advantage of this feature, include all server addresses in
    LOCATION, either separated by semicolons or as a list.</p>
    
    <p>...</p>
  </blockquote>
  
  <p><a href="https://docs.djangoproject.com/en/1.3/topics/cache/#memcached" rel="noreferrer">Django's cache framework - Memcached</a></p>
  
  <p>How exactly does this work? I've read some answers on this site that suggest this is accomplished by sharding across the servers based on hashes of the keys. </p>
  
  <p><a href="https://stackoverflow.com/questions/4717559/multiple-memcached-servers-question">Multiple memcached servers question</a></p>
  
  <p><a href="https://stackoverflow.com/questions/5733972/how-does-the-memcachestore-really-work-with-multiple-servers">How does the MemCacheStore really work with multiple servers?</a></p>
  
  <p>That's fine, but I need a much more specific and detailed answer than that. Using django with pylibmc or python-memcached how is this sharding actually performed? Does the order of IP addresses in the configuration setting matter? What if two different web servers running the same django app have two different settings files with the IP addresses of the memcached servers in a different order? Will that result in each machine using a different sharding strategy that causes duplicate keys and other inefficiencies?</p>
  
  <p>What if a particular machine shows up in the list twice? For example, what if I were to do something like this where 127.0.0.1 is actually the same machine as 172.19.26.240?</p>
  
  <pre><code>CACHES = {
      'default': {
          'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
          'LOCATION': [
              '127.0.0.1:11211',
              '172.19.26.240:11211',
              '172.19.26.242:11211',
          ]
      }
  }
  </code></pre>
  
  <p>What if one of the memcached servers has more capacity than the others? If machine one has as 64MB memcached and machine 2 has a 128MB, Will the sharding algorithm take that into account and give machine 2 a greater proportion of the keys?</p>
  
  <p>I've also read that if a memcached server is lost, then those keys are lost. That is obvious when sharding is involved. What's more important is what will happen if a memcached server goes down and I leave its IP address in the settings file? Will django/memcached simply fail to get any keys that would have been sharded to that failed server, or will it realize that server has failed and come up with a new sharding strategy? If there is a new sharding strategy, does it intelligently take the keys that were originally intended for the failed server and divide them among the remaining servers, or does it come up with a brand new strategy as if the first server didn't exist and result in keys being duplicated?</p>
  
  <p>I tried reading the source code of python-memcached, and couldn't figure this out at all. I plan to try reading the code of libmemcached and pylibmc, but I figured asking here would be easier if someone already knew. </p>