How to Build a High‑Performance Message Queue with Redis List and Redisson
This article explains how Redis List can be used as a FIFO message queue, covering core concepts, command usage, reliability techniques, and Java integration with Redisson for building a practical backend messaging solution.
1 Introduction
In distributed systems, a message middleware is essential for decoupling, ordering, routing, asynchronous processing, and traffic shaping. Besides mainstream MQs like RabbitMQ, RocketMQ, and Kafka, Redis can also serve as a message queue.
2 About Message Queues
2.1 What is a Message Queue
Message middleware (or MQ) provides reliable, platform‑independent message transmission, enabling integration of distributed components. It is widely used for routing, publish/subscribe, and async processing to relieve system pressure.
Broker : the server that hosts one or more queues.
Producer : the sender that pushes messages to the broker.
Consumer : the receiver that pulls messages from the broker.
2.2 Problems Solved by MQ
1. Decoupling – producers and consumers interact via the queue, reducing direct dependencies.
Example: an order system delegates inventory reduction to an inventory system through a queue.
2. Ordering – FIFO ensures that messages are processed in the order they were produced.
Critical for scenarios like banking transactions where order matters.
3. Routing – messages can be directed to different queues based on rules.
4. Asynchronous processing – long‑running tasks can be split into independent steps, improving throughput.
Login flow: authentication, logging, and token caching can be handled asynchronously.
5. Traffic shaping – non‑critical steps can be off‑loaded to a queue to smooth spikes.
2.3 Business Characteristics of MQ
2.3.1 Message ordering
Ensures consumption follows production order.
2.3.2 Message deduplication
Guarantees idempotent processing to avoid duplicate execution.
2.3.3 Reliable transmission
Supports retries, persistence, dead‑letter queues, and recovery after failures.
3 Implementing a Queue with Redis List
Redis List is a linear ordered structure that naturally follows FIFO, making it suitable for queue semantics.
3.1 Enqueue with LPUSH
LPUSH key element [element...]LPUSH inserts elements at the head of the list; if the key does not exist, Redis creates it.
> LPUSH msg_queue msg1 msg2 msg3
(integer) 33.2 Dequeue with RPOP
> RPOP msg_queue
"msg1"
> RPOP msg_queue
"msg2"
> RPOP msg_queue
"msg3"
> RPOP msg_queue
(nil)3.3 Timely Consumption
Unlike traditional MQs, List does not push notifications, so consumers must poll (e.g., every second) using RPOP, which wastes I/O and CPU. Blocking commands BLPOP/BRPOP solve this by waiting for new items.
# BRPOP key timeout
BRPOP msg_queue 0The timeout of 0 makes the call block indefinitely until a message arrives.
3.4 Duplicate Consumption
List lacks built‑in idempotency; solutions include assigning a global ID to each message and checking it before processing.
3.5 Reliable Transmission
Redis lacks an ACK mechanism. The RPOPLPUSH command atomically moves a message to a backup list, enabling recovery after consumer crashes.
# Produce
> LPUSH list_queue msg1 msg2
(integer) 2
# Consume with backup
> RPOPLPUSH list_queue list_queue_bak
"msg1"
# After failure, read from backup
> RPOP list_queue_bak
"msg1"4 Using Redisson for Queue Operations
Redisson provides high‑level Java APIs for Redis queues. Example with Spring Boot:
4.1 Maven Dependency and Configuration
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.16.8</version>
</dependency>
spring:
application:
name: redisson_test
redis:
host: x.x.x.x
port: 6379
ssl: false
password: xxxx.xxxx4.2 Java Service Example
@Slf4j
@Service
public class RedisQueueService {
@Autowired
private RedissonClient redissonClient;
private static final String REDIS_QUEUE = "listQueue";
/** Message production */
public void msgProduce(String msg) {
RBlockingDeque<String> blockDeque = redissonClient.getBlockingDeque(REDIS_QUEUE);
try {
blockDeque.putFirst(msg);
} catch (InterruptedException e) {
log.error(e.printStackTrace());
}
}
/** Message consumption (blocking) */
public void msgConsume() {
RBlockingDeque<String> blockDeque = redissonClient.getBlockingDeque(REDIS_QUEUE);
boolean isCheck = true;
while (isCheck) {
try {
String msg = blockDeque.takeLast(); // consume
} catch (InterruptedException e) {
log.error(e.printStackTrace());
}
}
}
}5 Summary
Redis List implements a FIFO queue using LPUSH/RPOP.
BRPOP solves timely consumption by blocking until data arrives.
RPOPLPUSH provides a backup for reliable transmission.
For low‑to‑moderate traffic, Redis can be a practical queue alternative, though it cannot match the throughput of dedicated MQs like Kafka or RocketMQ.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architecture & Thinking
🍭 Frontline tech director and chief architect at top-tier companies 🥝 Years of deep experience in internet, e‑commerce, social, and finance sectors 🌾 Committed to publishing high‑quality articles covering core technologies of leading internet firms, application architecture, and AI breakthroughs.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
