Skip to content
intermediate Phase 66 · Async Processing

RabbitMQ Integration — Magento 2 Message Broker

Integrating RabbitMQ with Magento 2: connection setup, exchanges, queues, bindings, and production configuration

45m
1 problems
Topic Progress 0%

RabbitMQ Connection Setup

env.php Configuration

// app/etc/env.php
return [
    'queue' => [
        'amqp' => [
            'host' => 'rabbitmq.example.com',
            'port' => '5672',
            'user' => 'magento',
            'password' => 'magento_password',
            'virtualhost' => '/'
        ]
    ]
];

Connection Parameters

Parameter Description Default
host RabbitMQ server hostname localhost
port AMQP port 5672
user Authentication username guest
password Authentication password guest
virtualhost AMQP virtual host /

Enable RabbitMQ

# Set queue configuration
php bin/magento setup:config:set \
    --queue-host=rabbitmq.example.com \
    --queue-port=5672 \
    --queue-user=magento \
    --queue-password=magento_password

Test Connection

# Check RabbitMQ status
rabbitmqctl status

# List queues
rabbitmqctl list_queues

# List connections
rabbitmqctl list_connections

Exchanges, Queues, and Bindings

Exchanges

Exchanges route messages to queues based on bindings:

Type Behavior
direct Routes to queue with matching routing key
fanout Routes to all bound queues
topic Routes based on routing key pattern
headers Routes based on message headers

Magento Default Exchange

<!-- queue_topology.xml -->
<exchange name="magento-exchange" type="topic">
    <binding id="product_sync"
             topic="vendor.product.sync"
             destinationType="queue"
             destination="product_sync_queue"/>
</exchange>

Custom Exchange

<exchange name="vendor-exchange" type="direct">
    <binding id="order_binding"
             topic="vendor.order.process"
             destinationType="queue"
             destination="order_queue"
             arguments>
                <argument name="routing_key">order.new</argument>
    </argument>
</exchange>

Binding Arguments

<binding id="priority_binding"
         topic="vendor.priority.task"
         destinationType="queue"
         destination="priority_queue"
         arguments>
            <argument name="x-priority">10</argument>
</binding>

Queue Arguments

<queue name="vendor_durable_queue"
       arguments>
    <argument name="x-queue-type">quorum</argument>
    <argument name="x-message-ttl">3600000</argument>
    <argument name="x-max-length">10000</argument>
</queue>

Magento Queue Configuration

Complete Queue Setup

<!-- queue_topology.xml -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/topology.xsd">
    <exchange name="magento-exchange" type="topic">
        <binding id="product_sync"
                 topic="vendor.product.sync"
                 destinationType="queue"
                 destination="product_sync_queue"/>
        <binding id="order_process"
                 topic="vendor.order.process"
                 destinationType="queue"
                 destination="order_process_queue"/>
    </exchange>
</config>
<!-- queue_consumer.xml -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/consumer.xsd">
    <consumer name="product_sync"
              queue="product_sync_queue"
              handler="Vendor\Module\Consumer\ProductSync::process"
              maxMessages="1000"/>
    <consumer name="order_process"
              queue="order_process_queue"
              handler="Vendor\Module\Consumer\OrderProcess::process"
              maxMessages="500"/>
</config>

Deployment Configuration

// app/etc/env.php production config
'queue' => [
    'amqp' => [
        'host' => env('RABBITMQ_HOST', 'rabbitmq'),
        'port' => env('RABBITMQ_PORT', '5672'),
        'user' => env('RABBITMQ_USER', 'magento'),
        'password' => env('RABBITMQ_PASS', ''),
        'virtualhost' => env('RABBITMQ_VHOST', '/')
    ]
]

Switching from DB to RabbitMQ

# Update env.php queue config
php bin/magento setup:config:set --queue-host=rabbitmq --queue-port=5672

# Re-deploy
php bin/magento setup:upgrade
php bin/magento setup:di:compile

RabbitMQ Monitoring and Management

RabbitMQ Management Plugin

# Enable management plugin
rabbitmq-plugins enable rabbitmq_management

# Access UI
http://rabbitmq:15672

CLI Monitoring

# Queue status
rabbitmqctl list_queues name messages consumers memory

# Connection status
rabbitmqctl list_connections

# Channel status
rabbitmqctl list_channels

# Exchange status
rabbitmqctl list_exchanges

# Binding status
rabbitmqctl list_bindings

Performance Tuning

# Increase file descriptor limit
ulimit -n 65536

# Memory watermark
rabbitmqctl set_vm_memory_high_watermark 0.6

# Disk free limit
rabbitmqctl set_disk_free_limit 2GB

Common Issues

Issue Cause Fix
Connection refused RabbitMQ not running Start RabbitMQ service
Authentication failed Wrong credentials Check env.php user/password
Queue full max-length reached Increase limit or add consumers
Memory high Too many messages Add consumers, increase memory
Messages stuck Consumer crashed Restart consumer, check DLX

Practice Problems

0 / 1 solved
RabbitMQ Connection Issues

Magento cannot connect to RabbitMQ. Messages are falling back to database. Diagnose and fix.

Quiz

1. What AMQP exchange type does Magento use by default?

Question 1 options

2. Where is RabbitMQ connection configured?

Question 2 options

3. What port does RabbitMQ use by default?

Question 3 options

4. What management UI port does RabbitMQ expose?

Question 4 options

Flashcards

Question

What is the default RabbitMQ port?

Answer

5672 for AMQP, 15672 for management UI

Question

Which exchange type does Magento use?

Answer

Topic exchange by default

Question

Where is RabbitMQ connection configured?

Answer

app/etc/env.php under queue.amqp

Question

What CLI checks queue status?

Answer

rabbitmqctl list_queues name messages consumers

Question

What is a dead letter exchange?

Answer

An exchange where failed messages are routed after max retries

Revision Notes

Key Takeaways

  • 1. RabbitMQ connection configured in app/etc/env.php under queue.amqp
  • 2. Magento uses topic exchanges by default for message routing
  • 3. queue_topology.xml defines exchanges, queues, and bindings
  • 4. Management UI available at port 15672
  • 5. Monitor with rabbitmqctl commands for queues, connections, and channels
  • 6. Switch from DB to RabbitMQ for production throughput

Interview Tips

  • Explain AMQP exchange types and when to use each
  • Describe how to configure RabbitMQ connection in Magento
  • Discuss RabbitMQ monitoring and performance tuning
  • Know common RabbitMQ issues and their fixes

Cheat Sheet

RabbitMQ Cheat Sheet

env.php:

'queue' => ['amqp' => [
    'host' => 'rabbitmq',
    'port' => '5672',
    'user' => 'magento',
    'password' => '...',
    'virtualhost' => '/'
]]

Ports: 5672 (AMQP), 15672 (management)

Monitoring:
rabbitmqctl list_queues
rabbitmqctl list_connections

Exchange types: direct, fanout, topic, headers

Common issues:

  • Connection refused: check service
  • Auth failed: check credentials
  • Queue full: add consumers