database distributedsystem systemdesign
Queue
A Queue is a Collection of entities that are maintained in a sequence and can be modified by the addition of entities at one end of the sequence and the removal from the other end. This characteristic is known as FIFO (First In, First Out), meaning that the first element that gets added to the queue is the first one to be removed.
π Queue
Transclude of message-broker#intro
Redis Queue
Redis Lists are one of the fundamental data structures in Redis. They can be used to implement queues, making them an alternative to message queues like RabbitMQ and Apache Kafka.
π Redis Streams, Redis PubSub and Kafka
Redis List
- Redis lists are linked lists of string.
- It supports operations at both ends (left and right).
- Elements are stored in insertion order.
How it Works ?
Redis list provide two queue models:
- FIFO Queue β>
LPUSH+BRPOP(Message Queue Alternative) - LIFO Stack β>
LPUSH+LPOP
Commands (redis-cli)
| Command | Description |
|---|---|
LPUSH queue value | Pushes a value to the left (Head) of the list. |
RPUSH queue value | Pushes a value to the right (Tail) of the list. |
LPOP queue | Removes and returns the first element (left). |
RPOP queue | Removes and returns the last element (right). |
BLPOP queue timeout | Blocking LPOP (waits if no element). |
BRPOP queue timeout | Blocking RPOP (waits if no element). |
LRANGE queue start stop | Retrieves multiple elements in the list. |
LLEN queue | Gets the list length. |
![]() |
Using RPUSH, add messages to the queue:
RPUSH task_queue "task1"
RPUSH task_queue "task2"
RPUSH task_queue "task3"RPUSH task_queue "task1"β Adds"task1"to the right of the queue..
Use LRANGE to view all elements in the queue:
LRANGE task_queue 0 -1Use BLPOP to remove and retrieve the first message (FIFO).
BLPOP task_queue 0-
BLPOPis a blocking pop operation. (LPOPis non-blocking operations.) -
The timeout specifies how long Redis should wait if the queue is empty.
-
A timeout of
0means βwait indefinitelyβ until a new element is added. -
If timeout of 5, the queue is empty, Redis will wait for 5 seconds.
-
If no message appears within 5 seconds, it returns nil.
Run LRANGE to see the remaining messages:
LRANGE task_queue 0 -1If you run BLPOP when the queue is empty, Redis waits until a new message arrives.
BLPOP task_queue 0
(Waiting for a message...)Use LLEN to check the number of messages in the queue:
LLEN task_queueUse DEL to delete the queue completely:
DEL task_queueImplementing Redis Queue in Go
publisher.go
var ctx = context.Background()
func main() {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer client.Close()
queue := "task_queue"
// Push tasks to queue
tasks := []string{"task1", "task2", "task3", "task4", "task5", "task6"}
for _, task := range tasks {
err := client.RPush(ctx, queue, task).Err()
if err != nil {
log.Fatalf("Failed to push task: %v", err)
}
fmt.Println("Added:", task)
// Sleep for a random duration (0-5 seconds)
delay := time.Duration(rand.Intn(5)) * time.Second
time.Sleep(delay)
}
}consumer.go
var ctx = context.Background()
func main() {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer client.Close()
queue := "task_queue"
for {
// Blocking pop (waits for new task)
task, err := client.BLPop(ctx, 0, queue).Result()
if err != nil {
log.Fatalf("Error consuming task: %v", err)
}
fmt.Println("Consumed:", task[1])
// Sleep for 1 second before consuming the next task
time.Sleep(1 * time.Second)
}
}
Comparison
| Feature | Redis Lists (Queue) | Redis Streams | RabbitMQ |
|---|---|---|---|
| Persistence | β No | β Yes | β Yes |
| Message Acknowledgment | β No | β Yes | β Yes |
| Multiple Consumers | β No | β Yes (Consumer Groups) | β Yes |
| Blocking Read | β Yes | β Yes | β Yes |
| Best Use Case | Simple Task Queues | Event-driven Processing | Enterprise Messaging |
When to Use Redis Queue ?
- Short-lived jobs (background tasks, worker queues).
- Simple task processing (e.g., webhooks, notifications).
- When you donβt need message persistence.
When Not to Use ?
- If message loss is unacceptable β Use Redis Streams.
- **If multiple consumers must process the same message** β> Use RabbitMQ/Kafka
