Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis and Optimistic concurrency control: is it possible?

Tags:

redis

Working in an application that stores entities in redis as a serialized binary blob. I have multiple clients working on the same data set and I wish to use optimistic concurrency.

My requirements are these:

  1. Read the serialized entity for a specific key in one roundtrip
  2. Write the modifiend entity back to redis. If any other client modified the entity between the read and the write the operation will fail

Is this possible to do in redis? And if so: what redis commands should be executed to do this?

like image 516
Tim Skauge Avatar asked Nov 14 '11 12:11

Tim Skauge


1 Answers

WATCH key, GET key, MULTI, SET key, then EXEC. The EXEC will fail if the key's value has changed since you executed the WATCH.

http://redis.io/topics/transactions#cas

like image 198
Carl Zulauf Avatar answered Nov 10 '22 00:11

Carl Zulauf