0
0
RabbitMQdevops~30 mins

Fair dispatch with prefetch in RabbitMQ - Mini Project: Build & Apply

Choose your learning style9 modes available
Fair dispatch with prefetch in RabbitMQ
📖 Scenario: You are setting up a RabbitMQ message queue system where multiple workers consume tasks. To avoid one worker getting overloaded with many messages while others are idle, you want to use fair dispatch with prefetch.This means each worker will only get one message at a time until it acknowledges the previous one, ensuring balanced work distribution.
🎯 Goal: Configure a RabbitMQ consumer to use fair dispatch by setting the prefetch count to 1, so that the server sends only one unacknowledged message at a time to the worker.
📋 What You'll Learn
Create a queue named task_queue with durability enabled
Set the prefetch count to 1 for fair dispatch
Consume messages from task_queue with manual acknowledgments
Print the received message body in the consumer
💡 Why This Matters
🌍 Real World
In real systems, fair dispatch prevents one worker from being overwhelmed while others are idle, improving resource use and responsiveness.
💼 Career
Understanding prefetch and manual acknowledgments is essential for roles involving message queue management, backend development, and system reliability.
Progress0 / 4 steps
1
Create a durable queue named task_queue
Write code to declare a queue named task_queue with durability enabled using channel.queue_declare.
RabbitMQ
Need a hint?

Use channel.queue_declare with queue='task_queue' and durable=True.

2
Set the prefetch count to 1 for fair dispatch
Add code to set the prefetch count to 1 using channel.basic_qos to enable fair dispatch.
RabbitMQ
Need a hint?

Use channel.basic_qos(prefetch_count=1) to limit unacknowledged messages to one.

3
Consume messages with manual acknowledgments
Write a callback function named callback that takes ch, method, properties, and body as parameters. Inside it, print the message body decoded as UTF-8. Then acknowledge the message using ch.basic_ack with delivery_tag=method.delivery_tag. Finally, start consuming from task_queue with auto_ack=False and the callback function.
RabbitMQ
Need a hint?

Define callback to print the decoded message and acknowledge it. Use channel.basic_consume with auto_ack=False.

4
Start consuming and print output
Add code to start consuming messages using channel.start_consuming().
RabbitMQ
Need a hint?

Call channel.start_consuming() to begin receiving messages.