Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Job queue with redis using BLPOP

Tags:

redis

Im trying to create infinite job queue using redis and ruby eventmachine. To achieve that Im using redis BLPOP command with 0 timeout. After successful BLPOP I run it again.

Am I on the right way or there is a better way to create job queue with redis?

like image 262
Alexey Zakharov Avatar asked Aug 30 '11 09:08

Alexey Zakharov


People also ask

Can you use Redis as a queue?

Using Redis with Redis Queue allows you to enter those complex tasks into a queue, so the Redis worker executes these tasks outside of your application's HTTP server. In this article, we will build an app that enqueues jobs with Redis queue, performs a function on those jobs and returns the result of the function.

What is Blpop in Redis?

BLPOP is a blocking list pop primitive. It is the blocking version of LPOP because it blocks the connection when there are no elements to pop from any of the given lists. An element is popped from the head of the first list that is non-empty, with the given keys being checked in the order that they are given.


1 Answers

If you use BLPOP alone to remove a message from the queue, and your message consumer fails to process it, the message will have to be re-queued, lest it disappear forever along with the failed consumer.

For more durable message processing, a list of messages being processed must be maintained so they can be re-queued in the event of failure.

[B]RPOPLPUSH is perfect for this scenario; it can atomically pop a message from the message queue and pushes it onto a processing queue so that the application can respond in the case of a failure on the consumer's end.

http://redis.io/commands/rpoplpush

Actual re-queueing is left to the application, but this redis command provides the foundations to do so.

There are also some drop-in-place implementations of queues using redis floating around the web, such as RestMQ [ http://www.restmq.com/ ]

like image 171
Daniel Farrell Avatar answered Sep 28 '22 08:09

Daniel Farrell