Reputation: 3797
I have a hash in redis in which one the field has value as stringified array, whenever user register for an event,
There is a potential race condition possibility here if two users register at close enough time.
Race condition could be like this that the both users get the same stringified array from redis and then they modify, and only one update will happen as one will be overwritten by other.
Is there a way to prevent this race condition like transactions in SQL. I have read about multi
, but it does not allow to do computation between commands on server.
Or storing stringifying array and storing as hash field is a bad idea, and I should use a normal list for this on redis.
Upvotes: 0
Views: 4590
Reputation: 46523
The solution is to use atomic operations where you can. You have several options:
LPUSH
WATCH
command to track changesThe typical WATCH
usage involves attempting to execute the transaction until it succeeds. You can do this with a simple loop, however it's possible that your connector has a special convenience method exactly for that.
Upvotes: 5