Re: Atomic Compare and Swap

1,181 views
Skip to first unread message

Tim Lossen

unread,
Nov 22, 2012, 2:59:23 AM11/22/12
to redi...@googlegroups.com
no, but you can easily implement it in lua:

http://redis.io/commands/eval

(each lua script runs atomically, like other redis commands)


On 2012-11-22, at 04:10 , Ritesh Tijoriwala wrote:

> Is there a way to do Atomic Compare and Swap in Redis? Without this, I am not sure how to avoid the extra roundtrip to server which GETs the value and then client does the check.
>
> --
> You received this message because you are subscribed to the Google Groups "Redis DB" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/redis-db/-/JtqzpgOL5vcJ.
> To post to this group, send email to redi...@googlegroups.com.
> To unsubscribe from this group, send email to redis-db+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/redis-db?hl=en.

--
http://tim.lossen.de



Salvatore Sanfilippo

unread,
Nov 22, 2012, 3:00:30 AM11/22/12
to Redis DB
Well actually WATCH is Redis's (more generic) CAS.

Cheers,
Salvatore
Salvatore 'antirez' Sanfilippo
open source developer - VMware
http://invece.org

Beauty is more important in computing than anywhere else in technology
because software is so complicated. Beauty is the ultimate defence
against complexity.
— David Gelernter

Ritesh Tijoriwala

unread,
Nov 22, 2012, 4:02:26 AM11/22/12
to redi...@googlegroups.com
Hi Salvatore,
I am currently using WATCH. However, I have to do an extra GET to compare the values and this forces extra deserialization on client side and one complete roundtrip. For complex objects/values, this can be quite expensive. Is there no way to avoid this?


Thanks for help.
Regards,
Ritesh

Salvatore Sanfilippo

unread,
Nov 22, 2012, 4:26:51 AM11/22/12
to Redis DB
Hey, I'm not sure why another GET is required.

Memcached's CAS:

GET key ....
CAS key ....

Redis's WATCH:

WATCH key
GET key
MULTI
SET key ...
EXEC

There is just one get operation in both cases.

However you can use Lua script if you want to improve in the side of
number of commands to send to the server, but note that the first two
and the last three commands can be sent in two separate pipelines so
you pay latency only two times, like in the GET/CAS example.

Cheers,
Salvatore
> https://groups.google.com/d/msg/redis-db/-/b4cthm6KPbIJ.
Reply all
Reply to author
Forward
0 new messages