Is it possible to implement distributed resource blocking using
memcached ?
Is it possible to implement them using following scheme (pseudocode)
lock(a)
1. if (add( key=>a, expiration=>5min )) {
2. if (cas( key=>a, expiration=>5min )) {
// resource locked by us
}
// Someone tried to lock resource the same time. Race condition
in add.
}
// resource is already locked
unlock(a)
1. delete(a)
What do you think?
Is race condition in add possible?
Will such double check work?
Or is there more simple way?
PS. Expiration in 5min is used to auto unlock resource in case of
client disconnection or other errors.