SQL error while adding to cache (race condition)

mauritsl - September 2, 2009 - 23:38
Project:Memcache API and Integration
Version:6.x-1.4
Component:Code
Category:bug report
Priority:critical
Assigned:Unassigned
Status:duplicate
Description

The following can give a mysql "duplicate key" error in race conditions:

db_query("DELETE FROM {%s} WHERE cid = '%s'", $table, $cid);
db_query("INSERT INTO {%s} (cid, data, expire, created, headers, serialized) VALUES ('%s', %b, %d, %d, '%s', '%s')", $table, $cid, $data, $expire, $created, $headers, $serialized);

Code is from memcache.db.inc (line 148). On high traffic sites this code can be executed simultaneously resulting in the following order of query execution:

DELETE query from process A
DELETE query from process B
INSERT INTO query from process A
INSERT INTO query from process B

The last query will fail cause of a duplicate key.

I replaced the two queries by one INSERT INTO ... ON DUPLICATE KEY UPDATE ... query (which is not standard SQL but a MySQL vendor extension). See also the issue on http://drupal.org/node/188751 , that one was resolved by replacing the ON DUPLICATE KEY UPDATE by the 2 seperate queries...

I'm using InnoDB, but I think this can also happen with MyISAM.

#1

bertboerland@ww... - September 2, 2009 - 23:38

by itself this is not that bad (only an php error in the logging) but can lead to a condition where anonymous users will not see new content until the cache is cleared.

#2

bertboerland@ww... - September 2, 2009 - 23:43
Priority:normal» critical

racing prio, if you think this is not the case please lower. but in our case it lead -as described above- to the situation where anonymous users would not see any new content

#3

bertboerland@ww... - September 2, 2009 - 23:53

this if statement might be a better solution? http://drupal.org/node/538042

#4

mauritsl - September 3, 2009 - 00:07
Status:active» duplicate

Duplicate: http://drupal.org/node/538042

 
 

Drupal is a registered trademark of Dries Buytaert.