How do you build a simple work queue using a Redis list where multiple workers pull tasks to process?
Short answer
You have producers add new tasks to the queue using RPUSH, and have multiple worker processes each call BRPOP on the same queue key, with Redis guaranteeing that each individual task is only delivered to exactly one of the waiting workers, naturally distributing the workload…
Advertisement
AModel answer
You have producers add new tasks to the queue using RPUSH, and have multiple worker processes each call BRPOP on the same queue key, with Redis guaranteeing that each individual task is only delivered to exactly one of the waiting workers, naturally distributing the workload across however many workers are currently running.
Example
-- Producer RPUSH tasks 'send_email_to_user_42' -- Each worker BRPOP tasks 0
Real-world example
An email sending service runs five worker processes all calling BRPOP on the same task queue, with Redis automatically distributing incoming email jobs across whichever workers happen to be available at that moment.
Likely follow-ups
- How do you scale the number of workers up or down based on queue length?
- What happens if a worker crashes while holding a task it pulled from the queue?
Related topics