Im wondering what is the best way to make my Memcache operations atmoic in my web app.
Take the following scenario into consideration:
Client 1 connects and retrieves data from key 1
Client 2 connects a few microsecond after Client 1, requests the same data from key 1
Client 1 saves new data to key 1
Client 2 saves new (different data) to key 1, not taking into account that Client 1 modified the value already
In this case, there is no atomicity in the process.
My (potential) solution is to set, acquire and release locks on keys from within my app.
So the above process would work like this after my implementation:
Client 1 connects, checks for an active lock on key 1, finds none, and gets the data
Client 2 connects a few microsecond after Client 1, requests the same data from key 1, but finds a lock
Client 2 enters a retry loop until Client 1 releases the lock
Client 1 saves new data to key 1, releases the lock
Client 2 gets the fresh data, sets a lock on key 1, and continues
Thoughts? Will this method work, and will there be any performance hits I should be wary of?