Task Queues for PHP: RabbitMQ, Gearman and PhpAmqpLib
In modern web applications, response speed to the user is critically important. Long-running operations (sending email, PDF generation, image processing, data parsing) should not block the main execution thread. The solution to this problem is job queues. They allow delegating background tasks to workers running asynchronously.
In the PHP ecosystem, there are several mature solutions for organizing queues. Let's look at three key tools: PhpAmqpLib (client for RabbitMQ), Gearman, and native RabbitMQ via its AMQP protocol.
Why are task queues needed in PHP?
Typical scenario: a user registers on a website. The system must:
- Save data to the database
- Send a welcome email
- Create a PDF contract
- Run a spam check
Without a queue, the user waits for all operations to complete. With a queue:
- The main process saves the data and sends the task to the queue
- The user instantly receives a "Success" response
- Background workers process tasks one by one
This improves UX and system stability.
1. RabbitMQ and PhpAmqpLib
RabbitMQ is a powerful message broker supporting the AMQP 0-9-1 protocol. PhpAmqpLib is a pure PHP client implementation for working with RabbitMQ without external extensions.
Installation
composer require php-amqplib/php-amqplibExample: Sending a task (Producer)
<?phprequire_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\\Connection\\AMQPStreamConnection;use PhpAmqpLib\\Message\\AMQPMessage;
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');$channel = $connection->channel();
// Declare the queue (will be created if it doesn't exist)$channel->queue_declare('email_queue', false, true, false, false);
$data = json_encode([ 'to' => 'user@example.com', 'subject' => 'Welcome', 'body' => 'Welcome!']);
$msg = new AMQPMessage($data, ['delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT]);$channel->basic_publish($msg, '', 'email_queue');
echo " [x] Sent: $data";
$channel->close();$connection->close();Example: Worker (Consumer)
<?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);
echo " [*] Waiting for tasks. To exit press CTRL+C";
$callback = function ($msg) { $data = json_decode($msg->body, true); // Simulating email sending echo " [x] Sending email to {$data['to']}: {$data['subject']}"; sleep(2); // Simulating work echo " [x] Done"; $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();}Advantages: reliability, support for complex routing (topics, headers), message persistence, clustering.
Disadvantages: requires installation and configuration of a RabbitMQ server (Erlang, management via web interface).
2. Gearman
Gearman is a distributed task system. It consists of three components: client (sends tasks), worker (executes), server (dispatcher). Works via a binary protocol.
Installation
First, install the Gearman server and PHP extension:
sudo apt-get install gearman-job-s Also in library
Task Queues in C# (.NET): System.Threading.Channels, Hangfire, and RabbitMQ
Authorization and Authentication in Rust: Libraries and Tools
Java Application Configuration: Libraries and Tools
Prisma for JavaScript (Node.js): Complete Guide with Examples
Working with CSV in Node.js: csv-parser and csv-writer libraries