Duplicate keys in memcached

I am having problems with memcache on my php site. Sometimes I get a report that the site is behaving badly, and when I look at memcache, I find that there are several keys on both cluster servers. Data does not match between two records (one older).

My understanding of memcached was that this should not happen ... the client must hash the key, and then always select the same server. Therefore, either my understanding is incorrect, or my code. Can someone explain why this could happen?

FWIW servers hosted on Amazon EC2.

All my memcache connections open with this function:

$mem_servers = array( array('ec2-000-000-000-20.compute-1.amazonaws.com', 11211, 50), array('ec2-000-000-000-21.compute-1.amazonaws.com', 11211, 50) ); function ConnectMemcache() { global $mem_servers; if ($memcon == 0) { $memcon = new Memcache(); foreach($mem_servers as $server) $memcon->addServer($server[0], $server[1], true); } return($memcon); } 

and the values ​​are saved through this:

 function SetData($key,$data) { global $mem_global_key; if(MEMCACHE_ON_OFF) { $key = $mem_global_key.$key; $memcache = ConnectMemcache(); $memcache->set($key, $data); return true; } else { return false; } } 
+4
source share
1 answer

I think this blog post touches on the issues you have.

http://www.caiapps.com/duplicate-key-problem-in-memcache-php/

From the article it sounds like this happens:
- memcache server, which has a key, initially drops out
- the key is recreated on the second server with updated data
- The 1st server returns to the network and to the cluster with the old data.
- Now you have keys on two servers with different data

It looks like you might need to use Memcache :: flush to flush the memcache cluster before your write, to minimize how long duplicates can exist in your cluster.

+1
source

Source: https://habr.com/ru/post/1394456/


All Articles