I am using memcache(not memcached) and I might have 10000 request at same time hitting cache . Which might cause race condition so I used this code http://terrychay.com/article/keeping-memcache-consistent.shtml to obtain locks and than set the key .
Now from logging I saw that while one request A is waiting on cache other request B might complete fetching data and puts in to cache so there is no point of request A to wait and overwrite the data .
So I thought one solution is : while a request is waiting for lock it will check if the data was there at key . If its there than return data from key instead of updating it . Can any one else think of any suggestions ? Will checking for key while waiting for lock blow up memcache server ?
I don’t think it’s worthwhile tinkering with the lock acquisition code to much. Once you have the lock, you can simply do a
getand if the data is there, clear the lock and simply return the data without hitting the database.Doing this should also reduce the overall waiting time.