php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #45558 Memcache reads/writes are not synchronized
Submitted: 2008-07-18 19:14 UTC Modified: 2008-07-19 19:08 UTC
From: emailforalex at gmail dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.2.6 OS: any
Private report: No CVE-ID: None
 [2008-07-18 19:14 UTC] emailforalex at gmail dot com
Description:
------------
This "bug" is incredibly hard to track down and reproduce, but is a major issue for high-volume sites using memcache. There is no synchronization mechanism built in when multiple servers serving multiple requests are all trying to read/write to a memcache machine.

A race condition occurs where one request (r1) reads from the cache, then a second request (r2) reads from the cache, then r1 writes the same key back to the cache and r2 proceeds to also write that key to the cache. In effect, r2 overwrites the changes by r1.

The issue was discovered while using the symfony framework which stores an array of all of its keys in the cache to allow for pattern-based searching of the keys. What sometimes happened is that two writes in a row would happen as described above, and the keys that were added to the array during the first write disappeared.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-07-18 23:22 UTC] scottmac@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

This is a problem with the application design, you need to do some sort of locking before you read and write the value.
 [2008-07-19 08:39 UTC] emailforalex at gmail dot com
Well, the reason it's under "Feature Request" is that this really should be a feature of PHP. Doing locking on a cache server (or multiple servers) from many web servers trying to access the simultaneously is incredibly hard to implement in PHP as the only place the servers can share a common lock is the caching machine itself. I believe it would be much easier for you to implement this than for developers to write it using PHP.
 [2008-07-19 13:27 UTC] scottmac@php.net
There is no locking performed for any external resources in PHP by default. It would make normal reads slow since PHP wouldn't be able to predict that you were going to do a write to something you had read.

Implementing a locking mechanism isn't that hard by using add with Memcache.
 [2008-07-19 17:52 UTC] emailforalex at gmail dot com
I wasn't suggesting doing it by default. What I would like are functions such as Memcache::acquireLock and Memcache::releaseLock - I'm sure this would come in handy for many more people than myself who would have trouble writing this on their own.
 [2008-07-19 19:08 UTC] scottmac@php.net
We wrap around the memcache API and there are no such functions in memcache for us to use.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 08 22:01:31 2025 UTC