Advertisement
Intermediate Redis, Lists in Redis

How do you implement a simple first in first out queue using a Redis list?

Short answer

You use RPUSH to add new items to the end of the list, representing new work being added to the queue, and LPOP to remove and retrieve the item from the beginning of the list, representing the oldest item being processed next, together implementing a straightforward first in…

Save Redis mock interview
Advertisement
AModel answer
You use RPUSH to add new items to the end of the list, representing new work being added to the queue, and LPOP to remove and retrieve the item from the beginning of the list, representing the oldest item being processed next, together implementing a straightforward first in first out queue.
Example

RPUSH task_queue 'task1' RPUSH task_queue 'task2' LPOP task_queue -- returns 'task1'

Real-world example
A background job processing system uses a Redis list as a simple task queue, adding new jobs with RPUSH and having worker processes continuously pull and process the oldest job with LPOP.
Likely follow-ups
  • What happens when multiple workers try to LPOP from the same queue at the same time?
  • What is the difference between LPOP and BLPOP?
Related topics