Is there anyway to atomically pop an item from a list and add it to a set?
My case scenario is that I have a "work queue" list of unique items, and I want to track what's being worked on in a "in progress" set. This would also allow the items in the "in progress" set to be re-queued if my worker process crashes while working on an item.
I'd prefer it to be atomic so that anything popped from the list will always be in the set. I just can't figure out how to do this with MULTI/EXEC, ie:
redis> MULTI
OK
redis> LPOP workqueue
"foobar"
redis> SADD inprog "foobar"
redis> EXEC
Is Redis LPOP / RPOP operation atomic? Yes, both LPOP and RPOP are atomic.
Removes and returns the first elements of the list stored at key . By default, the command pops a single element from the beginning of the list. When provided with the optional count argument, the reply will consist of up to count elements, depending on the list's length.
"You might not know it, but Redis is actually single-threaded, which is how every command is guaranteed to be atomic. While one command is executing, no other command will run."
Why do you want your "in progress" collection be a Set? You could simply use a List for the in progress items.
The command RPOPLPUSH "Right Pop, Left Push" was made exactly for this use case.
Atomically returns and removes the last element (tail) of the list stored at source, and pushes the element at the first element (head) of the list stored at destination
If you do want to use a Set for your in progress items, you will have to use a lua script and call it using eval.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With