PHP RabbitMQ Work Mode

The Work mode in RabbitMQ involves configuring multiple consumers to process messages from the same queue, which can increase the concurrent processing speed of messages. The architecture is shown in the diagram below:

RabbitMQ Work Mode

Note: Regardless of the RabbitMQ working mode used, each queue supports multiple consumers, and a message in the same queue will only be processed by one of the consumers.

1. Prerequisite Tutorial

Please read the following sections to understand the relevant knowledge:

2. Implementing Multiple Consumers in PHP

PHP itself does not support concurrent technologies such as multi-threading and coroutines, so it typically uses multi-processes to achieve concurrent processing. Here, we use multi-process mode to implement concurrent consumers processing messages in the queue.

2.1. Manually Starting Multiple Processes

To implement multiple processes, the simplest way is to manually run the PHP command multiple times.

For example: Assuming the consumer script from the previous section is recv.php, we can open multiple shell windows and repeatedly execute the consumer script like this:

php recv.php

php recv.php

Or in the same shell window, put the script in the background to run like this:

php recv.php &
php recv.php &

Explanation: The disadvantage of implementing multiple consumers in this manual way is that the processes are not well maintained, with no process monitoring. If a process crashes, it will not restart automatically.

2.2. Using Supervisor to Implement Multiple Processes

Supervisor is a process monitor in the Linux operating system, which can monitor PHP processes. If a PHP process crashes, it will be automatically restarted. It can also configure the concurrency of processes, making it easy to achieve concurrent processing of multiple consumers.

Here's an example using Ubuntu, similar for other Linux distributions.

Installing Supervisor

sudo apt-get install supervisor

Configuring Supervisor

The configuration file for Supervisor is usually located in the /etc/supervisor/conf.d directory. In this directory, you can create any number of configuration files to tell Supervisor how to monitor our processes. For example, create a file rabbitmq-worker.conf to monitor our consumer processes.

Example: File: rabbitmq-worker.conf

[program:rabbitmq-worker]
process_name=%(program_name)s_%(process_num)02d
command=php recv.php
autostart=true
autorestart=true
user=root
numprocs=10
redirect_stderr=true
stdout_logfile=/var/log/worker.log

Parameters explanation:

  • process_name: Definition of the process name, can be named arbitrarily. Here, two variables are used: program_name (process name) and process_num (process number).
  • command: The command we need to run.
  • autostart: Whether to start automatically on boot.
  • autorestart: Whether to restart automatically.
  • user: Which system account to use to run the command.
  • numprocs: Number of concurrent processes, indicating how many processes to start.
  • stdout_logfile: Where the running log file is saved.