Databases 6 min read

Implementing a Redis Message Queue with RPUSH and BLPOP for Order Cancellation

This article explains how to build a Redis‑based message queue using RPUSH to enqueue tasks and BLPOP to consume them in a PHP backend, illustrating the configuration, execution flow, and sample code for processing cancel‑order jobs in a high‑concurrency environment.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
Implementing a Redis Message Queue with RPUSH and BLPOP for Order Cancellation

Background In high‑concurrency business scenarios, a message queue is often used to handle asynchronous processing. This guide demonstrates one Redis‑based queue solution using RPUSH and BLPOP, with a cancel‑order use case (the business logic is omitted to focus on the queue implementation).

Solution Overview Data is pushed to the tail of a Redis list via the RPUSH command. A consumer repeatedly calls BLPOP to block‑wait for items from the head of the list (FIFO). The consumer function consume() in app/WorkQueue.php is triggered by a cron job every five minutes; the script terminates after 290 seconds to allow the next scheduled run.

Configuration The file app/config/config.php defines queue drivers. For the cancel_order driver, the class points to app/logic/Order.php, and method specifies the business method to invoke. The open flag enables or disables the queue.

Redis Details Queue keys use the prefix QUEUE_. Queue items are stored as JSON strings and decoded to arrays in the consumer. An example of an enqueued JSON payload is shown in the diagram.

Executing the Consumer The cron expression */5 * * * * php /app/WorkQueue.php cancel_order runs the consumer. The core PHP script is:

<?php
require_once './config/config.php';

class WorkQueue {
    /**
     * Execute message queue consumption
     */
    public function consume() {
        $startTime = time(); // program start time
        $allowAllExcuteTime = 290; // max execution seconds
        $configArr = include_once './config/config.php';
        $quueDriverConfig = $configArr['quueDriver'];
        $driverName = $argv[1]; // e.g., cancel_order
        $redisQueueKey = "QUEUE_{$driverName}";
        $logicName = $quueDriverConfig[$driverName]['class'];
        $method = $quueDriverConfig[$driverName]['method'];
        if ($quueDriverConfig[$driverName]['open'] !== true) {
            return;
        }
        require_once "./logic/queue/{$logicName}.php";
        $logicQueueObj = new $logicName();
        $redis = new redis();
        $redis->connect('127.0.0.1', 6379);
        while (true) {
            $specialTime = $allowAllExcuteTime - (time() - $startTime);
            if ($specialTime <= 0) {
                break;
            }
            $content = $redis->BLPOP($redisQueueKey, $specialTime);
            $contentArr = json_decode($content, true);
            if (is_array($content)) {
                $logicQueueObj->$method($contentArr);
            }
        }
    }
}

$workQueueObj = new WorkQueue();
$workQueueObj->consume();
?>

The BLPOP call blocks for $specialTime seconds waiting for a task; if none arrives, it returns after the timeout. Once a task is retrieved, the specified method on the logic class processes the JSON payload.

Summary There are many ways to implement queues; choose the one that fits your business scenario. In real deployments you may also need to handle re‑queueing failed tasks, which is beyond the scope of this brief guide.

RedisMessage QueuePHPqueueBLPOPRPUSH
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

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.