How to handle multiple updates at once using watch feature which is limited or other way?

9 views
Skip to first unread message

michal...@spotad.co

unread,
Feb 20, 2018, 6:24:51 AM2/20/18
to Redis DB
Hi, I am using pipe to update bulk of keys. However the same key can be updated from multiple processes at once
and i want to make sure that if it happens i get the value again and update it including the other changes - perform a merge.
I saw that watch feature can do this as shown in the following example. However i need to know which key failed since another client changed it. But the WatchError does not hold this information.
Is there another way to merge like this or extract this information from watch?
Thanks


with redis_server.pipeline() as pipe:
while 1:
try:
# put a WATCH on the key that holds our sequence value
pipe.watch('key1', 'key2')
# after WATCHing, the pipeline is put into immediate execution
# mode until we tell it to start buffering commands again.
# this allows us to get the current value of our sequence
current_value = pipe.get('key1')
if current_value is None:
next_value = 1
else:
next_value = int(current_value) + 1
current_value2 = pipe.get('key2')
if current_value2 is None:
next_value2 = 1
else:
next_value2 = int(current_value2) + 1
# now we can put the pipeline back into buffered mode with MULTI
pipe.multi()
pipe.set('key1', next_value)
pipe.set('key2', next_value2)
# and finally, execute the pipeline (the set command)
pipe.execute()
# if a WatchError wasn't raised during execution, everything
# we just did happened atomically.
break
except WatchError as e:

Dvir Volk

unread,
Feb 20, 2018, 8:03:39 AM2/20/18
to redi...@googlegroups.com
I don't think it's possible. Perhaps a Lua script will sort you since it will execute atomically and there is no need to watch anything. 

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
To post to this group, send email to redi...@googlegroups.com.
Visit this group at https://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.

Jean-Michel Hiver

unread,
Feb 20, 2018, 9:26:21 AM2/20/18
to Redis DB
If all you need is increment a value, use INCR instead of setting the new value.

Reply all
Reply to author
Forward
0 new messages