Task Queues for PHP: RabbitMQ, Gearman and Redis
In modern PHP web applications, there is often a need to perform long-running or resource-intensive operations without blocking the main user thread. Sending email newsletters, generating reports, processing images, working with video — all these tasks are better performed asynchronously, in the background. For this purpose, there are job queues — mechanisms that allow you to defer task execution and process it later using workers.
In this article, we will look at three popular tools for organizing task queues in PHP: RabbitMQ, Gearman and Redis. You will learn when to use them and see code examples.
Why are task queues needed?
The main advantages of using queues:
- Asynchrony — the user does not wait for the completion of a heavy operation.
- Scalability — you can run multiple workers for parallel processing.
- Fault tolerance — if a worker fails, the task remains in the queue and will be processed later.
- Separation of concerns — the code for sending a task and the code for processing it are loosely coupled.
1. RabbitMQ — a powerful message broker
RabbitMQ is a full-fledged message broker supporting the AMQP protocol. It provides guaranteed delivery, routing, acknowledgments, and much more. Suitable for complex systems with high load.
To work with RabbitMQ in PHP, the php-amqplib extension is used.
Installation
composer require php-amqplib/php-amqplibExample: sending a task
<?phprequire_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\\Connection\\AMQPStreamConnection;use PhpAmqpLib\\Message\\AMQPMessage;
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');$channel = $connection->channel();
$channel->queue_declare('email_queue', false, true, false, false);
$data = json_encode(['email' => 'user@example.com', 'subject' => 'Hello!']);$msg = new AMQPMessage($data, ['delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT]);
$channel->basic_publish($msg, '', 'email_queue');echo "Task sent to queue";
$channel->close();$connection->close();Example: worker (handler)
<?phprequire_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\\Connection\\AMQPStreamConnection;
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');$channel = $connection->channel();
$channel->queue_declare('email_queue', false, true, false, false);
$callback = function ($msg) { $data = json_decode($msg->body, true); echo "Sending email to {$data['email']} with subject '{$data['subject']}'"; // Actual email sending here $msg->ack(); // Acknowledge processing};
$channel->basic_qos(null, 1, null);$channel->basic_consume('email_queue', '', false, false, false, false, $callback);
while ($channel->is_consuming()) { $channel->wait();}2. Gearman — a distributed job system
Gearman specializes in distributed task processing. It allows you to call functions on remote servers or in the background. It is well suited for microservice architecture.
For PHP, there is the gearman extension (PECL).
Installation
sudo apt install gearman-job-serverpecl install gearmanExample: client (sending a task)
<?php$client = new GearmanClient();$client->addServer('127.0.0.1', 4730);
$result = $client->doBackground('send_email', json_encode([ 'email' => 'user@example.com', 'subject' => 'Hello!']));
echo "Task sent