SQL error while adding to cache (race condition)
| Project: | Memcache API and Integration |
| Version: | 6.x-1.4 |
| Component: | Code |
| Category: | bug report |
| Priority: | critical |
| Assigned: | Unassigned |
| Status: | duplicate |
Jump to:
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
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
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
this if statement might be a better solution? http://drupal.org/node/538042
#4
Duplicate: http://drupal.org/node/538042