What is RabbitMQ?

RabbitMQ is a widely used open-source message broker that facilitates the exchange of messages between different systems and services. It implements the Advanced Message Queuing Protocol (AMQP), which provides robust, reliable, and scalable communication between distributed applications. RabbitMQ is commonly employed in microservices architectures, where it allows systems to communicate asynchronously, enabling scalability, decoupling, and fault tolerance.

lb

When to Use RabbitMQ?


Example of RabbitMQ

lb
  1. The user sends a PDF creation request to the web application.
  2. The web application (the producer) sends a message to RabbitMQ that includes data from the request such as name and email.
  3. An exchange accepts the messages from the producer and routes them to correct message queues for PDF creation.
  4. The PDF processing worker (the consumer) receives the task message and starts processing the PDF.
Online shopping example
Order Service:
  • When a user places an order, the order service sends a message to the "order queue".
  • The order is acknowledged immediately, ensuring a fast response time for the user.
Payment Service:
  • A consumer in the payment service listens to the "order queue" and processes the payment.
  • If payment fails, the message can be re-queued or moved to a dead-letter queue for further investigation.
Inventory Service:
  • Another consumer listens to the same "order queue" and updates the inventory once the order is processed.
  • The inventory service doesn’t need to wait for the payment to complete but can operate independently.
Email Notification Service:
  • The email service listens to a separate "notification queue" and sends an email to the user confirming the order.
  • Even if the email service is down, RabbitMQ will hold the message until it is ready to process it.

Exchange in RabbitMQ

Messages are not sent directly to a queue by the producer. Instead, they are first sent to an exchange, which is responsible for directing the messages to the appropriate queues. This routing process is guided by bindings and routing keys. A binding establishes the connection between a queue and an exchange, helping determine how messages are routed.

lb
Message Flow in RabbitMQ
  1. The producer sends a message to an exchange.
  2. When creating an exchange, its type must be specified. There are four types of exchanges: direct, topic, headers, and fanout.
  3. The exchange receives the message and becomes responsible for routing it. Depending on the type of exchange, various message attributes such as the routing key are taken into consideration.
  4. Bindings must be established between the exchange and queues. In this scenario, there are two bindings linking the exchange to two different queues.
  5. The exchange routes the message to the appropriate queues based on message attributes.
  6. Messages remain in the queue until they are processed by a consumer.
  7. The consumer retrieves and processes the message.

Exchange Type in RabbitMQ

lb
Exchange Type Routing Logic Use Case
Direct Exact match between routing key and queue’s binding key. Point-to-point communication, task distribution.
Fanout Broadcasts messages to all bound queues. Pub-sub patterns, sending the same message to multiple queues.
Topic Pattern matching with routing key and binding key (wildcards). Routing messages based on complex topic hierarchies.
Headers Message headers are used for routing, instead of routing keys. Content-based routing based on message metadata.

Key concepts in RabbitMQ


How to handle message expiration and dead-lettering in RabbitMQ?

Message Expiration: Set a expiration property on messages or queues to automatically remove messages after a certain time period. This is useful for handling time-sensitive messages.


    channel.basic_publish(
        exchange='',
        routing_key='my_queue',
        body='message',
        properties=pika.BasicProperties(expiration='60000')  # Message expires in 60 seconds
    )
            

Dead-Letter Exchanges (DLX): Configure a dead-letter exchange to route messages that cannot be delivered or are rejected by consumers. Set the x-dead-letter-exchange argument on the original queue to specify the DLX.


    channel.queue_declare(
        queue='original_queue',
        arguments={
            'x-dead-letter-exchange': 'dlx_exchange'
        }
    )
    
                

Install RabbitMQ using Docker Desktop

  1. Download Docker Desktop : https://docs.docker.com/desktop/install/windows-install/
  2. Open Docker Desktop and ensure it's running.
  3. Open a terminal or command prompt.
  4. Pull the RabbitMQ Docker image:
    docker pull rabbitmq:management
  5. Run the RabbitMQ container:
    docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:management
    • -d runs the container in detached mode.
    • --name rabbitmq assigns a name to the container.
    • -p 5672:5672 maps port 5672 from the container to port 5672 on your host machine.
    • -p 15672:15672 maps port 15672 from the container to port 15672 on your host machine.
  6. Access RabbitMQ Management Interface at http://localhost:15672 using default credentials:
    • Username: guest
    • Password: guest

Reference : https://cloudamqp.com/blog/part1-rabbitmq-for-beginners-what-is-rabbitmq.html