You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The reason will be displayed to describe this comment to others. Learn more.
That's very practical, I was thinking it would be important to have a locking mechanism though, because modifying shared resources could go along the lines of:
(assuming the value is a serialized Json object with 10 members)
user 1 gets the value of key A
user 1 modifies the value of member 2 in key A
user 2 gets the value of key A
user 1 sets a new value for key A
user 2 modifies the value of member 10 of key A
user 2 writes the value of key A
So, the idea would be to embed lock()ing methods, in these redis objects, implemented with getSet(key~"_lock", 1) == 0?"OK":"wait..." which you can use like this
synchronized(redisVar){ // get and modify the value freely }
The reason will be displayed to describe this comment to others. Learn more.
I'm also planning to add an "idioms" module that implements some common patterns. A lock would fit in there I guess. The other thing is that transactions (MULTI) will also be supported at some point. This file here is just meant as a thin layer above what Redis provides for accessing fields.
The reason will be displayed to describe this comment to others. Learn more.
@etcimon I am not sure if it is such a good idea to implement this in our API layer. For people not familiar with Redis it may appear to be implemented in the database, so accessing it using this locks from a multi process app is safe too where in fact it is not.
That being said it is actually sad that Redis does not support CAS ;)
UPDATE: i am just seeing they support it using this crazy Lua scripting facility now... XD so ... nvm
The reason will be displayed to describe this comment to others. Learn more.
I've thought a while about this and, transactions aren't a replacement for locking "GET redis->D, D modify, D->redis SET". Transactions would return the get result after the transaction is over, and setting afterwards is not going to be safe. It looks like GETSET is really meant for locking, with a ttl it's possible to avoid deadlocks too. I'm working on a drop-in replacement for Redis that will implement this locking mechanism in a "coordinator" instance, which also takes care of distributing the hashes in a cluster installation. In the meantime, this is the only acceptable workaround I can think of
The reason will be displayed to describe this comment to others. Learn more.
Right, transactions are not always a replacement, but can be in certain cases. But a general lock idiom implementation would provide full locking and means a much better separation of concerns than integrating it into the Redis type wrappers. It then also allows to lock multiple keys with a single lock.
dcc09b0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's very practical, I was thinking it would be important to have a locking mechanism though, because modifying shared resources could go along the lines of:
(assuming the value is a serialized Json object with 10 members)
So, the idea would be to embed
lock()
ing methods, in these redis objects, implemented withgetSet(key~"_lock", 1) == 0?"OK":"wait..."
which you can use like thissynchronized(redisVar){ // get and modify the value freely }
dcc09b0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also planning to add an "idioms" module that implements some common patterns. A lock would fit in there I guess. The other thing is that transactions (MULTI) will also be supported at some point. This file here is just meant as a thin layer above what Redis provides for accessing fields.
dcc09b0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@etcimon I am not sure if it is such a good idea to implement this in our API layer. For people not familiar with Redis it may appear to be implemented in the database, so accessing it using this locks from a multi process app is safe too where in fact it is not.
That being said it is actually sad that Redis does not support CAS ;)
UPDATE: i am just seeing they support it using this crazy Lua scripting facility now... XD so ... nvm
dcc09b0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've thought a while about this and, transactions aren't a replacement for locking "
GET
redis->D, D modify, D->redisSET
". Transactions would return the get result after the transaction is over, and setting afterwards is not going to be safe. It looks likeGETSET
is really meant for locking, with a ttl it's possible to avoid deadlocks too. I'm working on a drop-in replacement for Redis that will implement this locking mechanism in a "coordinator" instance, which also takes care of distributing the hashes in a cluster installation. In the meantime, this is the only acceptable workaround I can think ofdcc09b0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, transactions are not always a replacement, but can be in certain cases. But a general lock idiom implementation would provide full locking and means a much better separation of concerns than integrating it into the Redis type wrappers. It then also allows to lock multiple keys with a single lock.