Is pipelining atomic?

932 views
Skip to first unread message

Joon Nick

unread,
Nov 15, 2011, 7:30:39 AM11/15/11
to Redis DB
Hi List,

we need a FIFO-queue, who will block duplicates.

Our solution:
- sorted Set with timestamp as score
- push member if not exists
- pop member with lowest score


PUSH:
ZSCORE queueKey member
if( nil ) ZADD queueKey time() member

POP
openPipeline
ZRANGE queueKey 0 0
ZREMRANGEBYRANK queueKey 0 0
executePipeline

or we need ?:

openPipeline
MULTI
ZRANGE queueKey 0 0
ZREMRANGEBYRANK queueKey 0 0
EXEC
executePipeline


Questions:
1. is this the right way or can we do it simpler, faster, better?
2. is pipelining atomic?


bugant

unread,
Nov 15, 2011, 7:39:59 AM11/15/11
to redi...@googlegroups.com
On Tue, Nov 15, 2011 at 1:30 PM, Joon Nick <joo...@googlemail.com> wrote:
> PUSH:
> ZSCORE queueKey member
> if( nil ) ZADD queueKey time() member

This is not necessary since member in a set are always unique.
From the ZADD documentation (http://redis.io/commands/zadd):
"If a specified member is already a member of the sorted set, the
score is updated and the element reinserted at the right position to
ensure the correct ordering"

ciao ciao,
matteo.

Dvir Volk

unread,
Nov 15, 2011, 8:03:44 AM11/15/11
to redi...@googlegroups.com
pipelining by itself without MULTI...EXEC is not atomic, so you need those.



--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
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.




--
Dvir Volk
System Architect, DoAT, http://doat.com

ivan babrou

unread,
Nov 15, 2011, 8:00:06 AM11/15/11
to redi...@googlegroups.com
No, use multi/exec for atomic operations

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
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.




--
Regards, Ian Babrou
http://bobrik.name http://twitter.com/ibobrik skype:i.babrou

Joon Nick

unread,
Nov 15, 2011, 9:23:58 AM11/15/11
to Redis DB
On 15 Nov., 13:39, bugant <bug...@gmail.com> wrote:
> > PUSH:
> > ZSCORE queueKey member
> > if( nil ) ZADD queueKey time() member
>
> This is not necessary since member in a set are always unique.

correct, but an duplicate ZADD will update the score(timestamp) and
break the FIFO-order!


--
so long Joon
www.schottenland.de

Joon Nick

unread,
Nov 15, 2011, 9:37:13 AM11/15/11
to Redis DB
(Thx for responses)

On 15 Nov., 14:03, Dvir Volk <d...@doat.com> wrote:

> pipelining by itself without MULTI...EXEC is not atomic, so you need those.
> --
> Dvir Volk
> System Architect, DoAT,http://doat.com

OK, per definition is it not atomic, i agree!
BUT, what will the single thread do, i suppose he will process the
input until the buffer is empty, meens all commands in the pipeline,
then he will look for new data from any client.
Wrong?
Maybe if the pipeline-data is big and the process is processing the
incomming data faster as new packets arrive, he will switch to other
client-connections?!
(and i suppose my two commands fit in a single Datapacket.)

I know, this is not an API-Question, its only for practical
understandigs... ;-)

Salvatore Sanfilippo

unread,
Nov 15, 2011, 10:34:02 AM11/15/11
to redi...@googlegroups.com
Hello,

yes it is possible that if your two queries are inside the same TCP
frame Redis will process it in an atomic fashion, but this is just a
side effect of the implementation and is not guaranteed.

Cheers,
Salvatore

> --
> You received this message because you are subscribed to the Google Groups "Redis DB" group.
> 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.
>
>

--
Salvatore 'antirez' Sanfilippo
open source developer - VMware

http://invece.org
"We are what we repeatedly do. Excellence, therefore, is not an act,
but a habit." -- Aristotele

Josiah Carlson

unread,
Nov 15, 2011, 12:04:55 PM11/15/11
to redi...@googlegroups.com
Currently you have nothing really stopping the FIFO order or duplicate
violation, except that a pair of racing queue entries will have almost
the same time, and it is unlikely that someone will consume the first
entered item before the second.

Generally, to do it properly requires one of 2 methods. Either using
WATCH/MULTI/EXEC
WATCH queue
score = ZSCORE queue member
if !score:
MULTI
ZADD queue time() member
EXEC
else:
UNWATCH queue

Or using the scripting branch.

- Josiah

Joon Nick

unread,
Nov 15, 2011, 12:25:31 PM11/15/11
to Redis DB
On 15 Nov., 16:34, Salvatore Sanfilippo <anti...@gmail.com> wrote:
> yes it is possible that if your two queries are inside the same TCP
> frame Redis will process it in an atomic fashion, but this is just a
> side effect of the implementation and is not guaranteed.

Salvatore,
thanks for the confirmation of my idea of the system.

Joon Nick

unread,
Nov 15, 2011, 12:30:45 PM11/15/11
to Redis DB
On 15 Nov., 18:04, Josiah Carlson <josiah.carl...@gmail.com> wrote:

> Generally, to do it properly requires one of 2 methods. Either using
> WATCH/MULTI/EXEC
> WATCH queue
> score = ZSCORE queue member
> if !score:
>   MULTI
>   ZADD queue time() member
>   EXEC
> else:
>   UNWATCH queue

Thx Joshiah, this makes the Push atomic too!

> Or using the scripting branch.

Yes, i thought about it, as Two commands Push and Pop, usefull
reciept.
Reply all
Reply to author
Forward
0 new messages