0
0
RabbitmqHow-ToBeginner ยท 4 min read

How to Use Round Robin Dispatch in RabbitMQ

In RabbitMQ, round robin dispatch happens automatically when multiple consumers subscribe to the same queue. RabbitMQ distributes messages evenly, sending each new message to the next consumer in line, ensuring balanced processing.
๐Ÿ“

Syntax

To use round robin dispatch, you declare a queue and have multiple consumers subscribe to it. RabbitMQ then distributes messages in a round robin manner to these consumers.

Key parts:

  • queue: Holds messages for consumers.
  • consumer: A client that receives messages from the queue.
  • Multiple consumers on one queue enable round robin dispatch.
python
channel.queue_declare(queue='task_queue', durable=True)

channel.basic_consume(queue='task_queue', on_message_callback=callback, auto_ack=False)

channel.start_consuming()
๐Ÿ’ป

Example

This example shows two consumers subscribing to the same queue. RabbitMQ sends messages alternately to each consumer, demonstrating round robin dispatch.

python
import pika
import time

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

channel.queue_declare(queue='task_queue', durable=True)

def callback(ch, method, properties, body):
    print(f"Received {body.decode()}")
    time.sleep(1)  # Simulate work
    print("Done")
    ch.basic_ack(delivery_tag=method.delivery_tag)

channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue='task_queue', on_message_callback=callback, auto_ack=False)

print('Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
Output
Waiting for messages. To exit press CTRL+C Received Task 1 Done Received Task 3 Done Received Task 5 Done ...
โš ๏ธ

Common Pitfalls

1. Using multiple queues instead of multiple consumers on one queue: Round robin only works when multiple consumers listen to the same queue. Using separate queues sends messages independently.

2. Not setting prefetch_count=1: Without this, a consumer may get multiple messages at once, breaking fair dispatch.

3. Auto-acknowledgment: Using auto_ack=True can cause message loss if a consumer crashes before processing. Use manual ack for reliability.

python
channel.basic_consume(queue='task_queue', on_message_callback=callback, auto_ack=True)  # Risky

# Better:
channel.basic_consume(queue='task_queue', on_message_callback=callback, auto_ack=False)

# And call ch.basic_ack() after processing
๐Ÿ“Š

Quick Reference

  • Declare a single queue for tasks.
  • Start multiple consumers on that queue.
  • Set prefetch_count=1 for fair dispatch.
  • Use manual acknowledgments to avoid message loss.
  • RabbitMQ automatically distributes messages round robin among consumers.
โœ…

Key Takeaways

Round robin dispatch happens automatically when multiple consumers subscribe to the same RabbitMQ queue.
Set prefetch_count=1 to ensure fair message distribution among consumers.
Use manual message acknowledgments to prevent message loss during consumer failures.
Do not create multiple queues for round robin; use multiple consumers on one queue instead.
RabbitMQ balances load by sending each new message to the next consumer in line.