Database Reference
In-Depth Information
Implementing a Reliable Queue
Redis provides a fairly simple mechanism for implementing a reliable
queue using the RPOPLPUSH (or its blocking equivalent) and the LREM
commands. As the name suggests, the RPOPLPUSH command pops an
element off the tail of a list, like the RPOP command, and then pushes it
onto the head of a second list, like the LPUSH command, in a single
atomic action. Unlike separate RPOP and LPUSH commands, this
ensures that the data cannot be lost in transit between the clients.
Instead, the item to be processed remains on an “in-flight” list of
elements currently being processed.
The command returns the element moved between the two lists for
processing. When processing is complete, the LREM is executed against
the processing list to remove the element from the processing list. This
simple example shows the basic process for implementing the queue:
redis 127.0.0.1:6379> LPUSH queue item1 item2 item3
item4 item5
(integer) 5
redis 127.0.0.1:6379> RPOPLPUSH queue processing
"item1"
redis 127.0.0.1:6379> LRANGE queue 0 100
1) "item5"
2) "item4"
3) "item3"
4) "item2"
redis 127.0.0.1:6379> LRANGE processing 0 100
1) "item1"
redis 127.0.0.1:6379> LREM processing 0 item1
(integer) 1
redis 127.0.0.1:6379> LRANGE processing 0 100
(empty list or set)
redis 127.0.0.1:6379>
Search WWH ::




Custom Search