How to Use Round Robin Dispatch in RabbitMQ
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.
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.
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()
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.
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=1for fair dispatch. - Use manual acknowledgments to avoid message loss.
- RabbitMQ automatically distributes messages round robin among consumers.