Would like to “lock” an external resource, in this case it will be a “blob” stored on rackspace cloud servers, similar so Amazon S3. (Using Python)
Would like a race condition to be handled like this:
- First process creates lock on resource
- First process begins editing resource
- Second process attempts to edit the same resource and finds that the resource is locked.
- Second process requests to be notified when the lock is taken off
- First process finishes editing the resource and removes the lock
- Second process receives the notification of the freed resource and is able to make its own edits
Would like to use Memcached or Redis to create the lock, however a different mechanism would be fine as well.
Most of this is pretty simple, the part I am struggling with is how to notify the second process when the lock is removed as opposed to forcing it to wait and retry.
Redis has publish / subscribe functionality, would this be appropriate to use in this situation?
Or is there some other way that I should go about solving this problem?
Thanks so much!
As for Redis, in case you really need to have locking, you can use
SETNXto create such a lock, more information can be found in SETNX documentation.If you expect low contention for the given lock and also you don’t have any resource hungry processing for the locked information you can also use
WATCH/MULTI/EXECtransactions. First you set WATCH on the key you want to edit, then inMULTIyou edit the key itself and then finally afterEXEC, it either fails the edit because someone meanwhile edited that watched key (and you then need to retry) or it return OK and you are fine. This is how you implement opportunistic locking in Redis. More info in transaction documentation