Job Queues for PHP: RabbitMQ, Beanstalkd, and Gearman
In modern web applications, there is often a need to perform long-running or resource-intensive operations asynchronously, without making the user wait for a response. Typical examples include sending email newsletters, generating reports, processing images, and interacting with external APIs. To solve these tasks in PHP, job queues are used. They allow separating request logic from background processing, improving interface responsiveness and system scalability.
In this article, we will take a detailed look at three popular tools for organizing queues in PHP: RabbitMQ, Beanstalkd, and Gearman. You will learn their features, architecture, usage examples, and how to choose the right solution for your project.
1. RabbitMQ
RabbitMQ is a powerful message broker implementing the AMQP (Advanced Message Queuing Protocol). It supports complex routing scenarios, exchanges, priority queues, delivery acknowledgments, and much more. RabbitMQ is ideal for microservice architectures and systems requiring reliable message delivery.
Installation and Connection
To work with RabbitMQ in PHP, the php-amqplib library is used. Installation via Composer:
composer require php-amqplib/php-amqplibExample: Sending an Email Notification
Let's create a producer (sender) and a consumer (handler) for background email sending.
File producer.php:
<?phprequire_once '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' => 'Welcome!', 'body' => 'Thank you for registering.']);
$msg = new AMQPMessage($data, [ 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT]);
$channel->basic_publish($msg, '', 'email_queue');
echo " [x] Sent message";
$channel->close();$connection->close();File consumer.php:
<?phprequire_once '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 messages. To exit press CTRL+C";
$callback = function ($msg) { $data = json_decode($msg->body, true); // Simulating email sending sleep(2); echo " [x] Sent email to {$data['email']}"; $msg->ack();};
$channel->basic_qos(null, 1, null);$channel->basic_consume('email_queue', '', false, false, false, false, $callback);
while ($channel->is_consuming()) { $channel->wait();}
$channel->close();$connection->close();2. Beanstalkd
Beanstalkd is a lightweight, fast, and easy-to-use queue manager. It operates on a protocol similar to memcached and is well-suited for tasks that do not require complex routing. Beanstalkd supports priorities, delays, and time-to-run (TTR).
Installation
For PHP, there is the pda/pheanstalk client:
composer require pda/pheanstalkExample: Image Processing
Suppose we need to create thumbnails for uploaded images.
File producer.php:
<?phprequire_once 'v Also in library
Elasticsearch C++ Client: Complete Guide to Working with the Search Engine
CLI Frameworks for PHP: Creating Console Applications with Symfony Console and Laravel Zero