How to Build a Delayed Queue with Redis Zset and Beanstalkd – Practical Guide

This article explains common delayed‑queue scenarios, demonstrates how to implement a delay queue using Redis Zset with atomic Lua scripts, and shows how to deploy and use Beanstalkd—including Java client setup and sample producer/consumer code—while offering extensions and best‑practice tips.

Java Architecture Diary
Java Architecture Diary
Java Architecture Diary
How to Build a Delayed Queue with Redis Zset and Beanstalkd – Practical Guide

Application Scenarios for Delayed Queues

Automatically cancel unpaid orders after 30 minutes, give an automatic positive review after 24 hours without user comment, and many other real‑world cases.

Implementation Based on Redis Zset

Principle

Redis provides a sorted‑set (Zset) structure that can be used for delayed operations. Each element has a score which can be set to a timestamp; the set is automatically ordered by this score.

If the score represents the desired execution time (a Unix timestamp), inserting the element into the Zset sorts it by time, effectively ordering tasks chronologically.

Continuously fetch the first element; when the current timestamp is greater than or equal to its score, consume and delete it. This achieves delayed execution without scanning the entire set.

ZADD delay_queue 1581309229 taskId_1
(integer) 1
ZADD delay_queue 1581309129 taskId_2
(integer) 1
ZADD delay_queue 1581309329 taskId_3
(integer) 1
ZRANGE delay_queue 0 -1 withscores
1) "taskId_2"
2) "1581309129"
3) "taskId_1"
4) "1581309229"
5) "taskId_3"
6) "1581309329"

Usage Notes

Wrap the retrieval and deletion logic in Redis Lua scripts to ensure atomic operations and be aware of Redis Cluster’s pseudo‑cluster issues.

For Java, use Redisson’s DelayedQueue implementation, which provides a ready‑made delayed‑queue wrapper.

Source code location:

org/redisson/RedissonDelayedQueue.java
Redis delayed queue diagram
Redis delayed queue diagram

Beanstalkd Message Queue

Beanstalkd is a high‑performance, lightweight distributed in‑memory queue system, originally used by Facebook’s Causes application and now open‑source, handling millions of tasks daily.

Deployment

Linux installation or Docker deployment.

yum install beanstalkd
||
docker run -d -p 11300:11300 pig4cloud/beanstalkd

Client usage – Maven dependency.

<!-- Encapsulates the official Java SDK, supports Spring Boot 2.x -->
<dependency>
    <groupId>com.pig4cloud.beanstalk</groupId>
    <artifactId>beanstalkd-client-spring-boot-starter</artifactId>
    <version>0.0.1</version>
</dependency>

Default configuration (see image).

Beanstalkd default configuration
Beanstalkd default configuration

Code usage.

@Autowired
private JobProducer producer;

/**
 * @param delay   Number of seconds to wait before the job becomes ready.
 * @param ttr    Time‑to‑run; seconds a worker has to process the job.
 * @param priority 0~2^32, lower values mean higher priority.
 */
@Test
public void testSend() {
    String taskId = "1"; // business object identifier
    producer.putJob(0, 10, 10, taskId.getBytes());
}
@Component
public class DemoJobConsumer extends AbstractTubeConsumerListener {
    @Override
    public void work(JobConsumer consumer) {
        // Block up to 1000 ms to reserve a job
        Job job = consumer.reserveJob(1000L);
        // Delete the job after processing
        consumer.deleteJob(job.getId());
        // Business logic for the delayed job
        String biz = new String(job.getData());
    }
}

Extensions

Database‑based polling can become a performance bottleneck under high load.

Other delayed‑queue implementations, such as RabbitMQ using TTL and dead‑letter exchanges, require careful configuration.

Custom implementations can be built using a timing‑wheel algorithm.

All approaches need compensation logic, whether triggered manually or automatically.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaredisMessage Queuedelayed queueredissonBeanstalkd
Java Architecture Diary
Written by

Java Architecture Diary

Committed to sharing original, high‑quality technical articles; no fluff or promotional content.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.