Magento 2 Rabbitmq configuration

Introduction

In November 2018, Magento 2.3.0 update quietly introduced integration with the message queue management tool RabbitMQ. The new feature opened powerful optimization options for both Magento 2 Commerce and Open Source editions.

Recently, We’ve talked about RabbitMQ installation and configuration. Now, We will show you how to configure Rabbitmq and use it on the installed Magento 2 instance.

Benefits

The main benefit of using RabbitMQ is the ability to decrease the load on the server during peak hours by storing data on the message broker. Before 2.3.0 only MySQL and cron were used for those purposes. But now We can use Rabbitmq which can speed up your current store by decreasing the load on MySQL database.

Rabbitmq installation

Rabbitmq installation is described in our article RabbitMQ installation and configuration. Which can be used to install RabbitMQ on your Ubuntu or CentOS server.

Rabbitmq configuration

1. First of all need to remove guest user:

# rabbitmqctl delete_user guest

2. After that we will need to create user with needed permissions.

# rabbitmqctl add_user rabbitmq rabbitmq_pwd
# rabbitmqctl set_user_tags rabbitmq administrator
# rabbitmqctl set_permissions -p / rabbitmq ".*" ".*" ".*"

3. Also, We need to create queue:

# rabbitmqadmin --username=rabbitmq --password=rabbitmq_pwd declare queue --vhost=/ name=async.operations.all durable=true

That’s all, rabbitmq is ready to use. From now, We can start to configure Magento to use it.

Magento configuration

First of all, We need to add rabbitmq configuration. It can be done in 2 ways:

Via bin/magento

$ php bin/magento setup:config:set --amqp-host="127.0.0.1" --amqp-port="5672" --amqp-user="rabbitmq" --amqp-password="rabbitmq_pwd" --amqp-virtualhost="/"

By editing app/etc/env.php with following lines

'queue' =>
  array (
    'amqp' =>
    array (
      'host' => '127.0.0.1',
      'port' => '5672',
      'user' => 'rabbitmq',
      'password' => 'rabbitmq_pwd',
      'virtualhost' => '/'
     ),
  ),

Also, you’ll need to add cron runner to app/etc/env.php:

'cron_consumers_runner' => [
  'cron_run' => true,
  'max_messages' => 0,
  'consumers' => [
    'async.operations.all',
    'codegeneratorProcessor'
  ]
]

That’s all. We successfully configured rabbitmq consumer in Magento.

Testing rabbitmq consumer on Magento

To list all consumers you can run command:

# php bin/magento queue:consumers:list

To run consumer you need to run following command:

# php bin/magento  queue:consumers:start  async.operations.all

That’s all. The consumer should work now!

Conclusion

RabbitMQ is a powerful message broker which can help to improve your store speed. Also, it will be helpful to decrease the load on your MySQL server which can be helpful during load spikes.

Related Post