0
0
RabbitMQdevops~10 mins

Fair dispatch with prefetch in RabbitMQ - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Fair dispatch with prefetch
Consumer connects to queue
Set prefetch count = N
Receive up to N messages
Process message
Send ack to server
Server sends next message
Back to Receive
The consumer sets a limit (prefetch count) on how many messages it can handle at once. The server sends only that many messages before waiting for acknowledgments, ensuring fair load distribution.
Execution Sample
RabbitMQ
channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue='task_queue', on_message_callback=callback)
channel.start_consuming()
This code sets the prefetch count to 1, so the consumer gets one message at a time and acknowledges it before receiving the next.
Process Table
StepPrefetch CountMessages Sent by ServerMessages Processed by ConsumerAcknowledgments SentServer Waits?
11100No
21111No
31111Yes
41222No
51222Yes
61333No
71333Yes
81444No
91444Yes
101555No
Exit1555Yes - no more messages
💡 All messages processed and acknowledged; server has no more messages to send.
Status Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
Messages Sent by Server0112235
Messages Processed by Consumer0011235
Acknowledgments Sent0011235
Key Moments - 3 Insights
Why does the server send only one message at a time even if more are waiting?
Because the prefetch count is set to 1, the server waits for an acknowledgment before sending the next message, ensuring fair dispatch as shown in steps 1 and 2 of the execution_table.
What happens if the consumer does not acknowledge a message?
The server will not send more messages beyond the prefetch count, causing it to wait indefinitely as the acknowledgment is required to proceed, demonstrated by the 'Server Waits?' column.
Why is fair dispatch important in message processing?
It prevents one consumer from being overloaded by limiting unacknowledged messages, ensuring all consumers get a fair share of work, as seen by the controlled message flow in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, how many messages has the consumer processed?
A2
B1
C3
D0
💡 Hint
Check the 'Messages Processed by Consumer' column at step 4 in the execution_table.
At which step does the server stop sending messages because all are processed?
AStep 10
BExit
CStep 8
DStep 5
💡 Hint
Look at the 'Messages Sent by Server' column and the exit_note in the execution_table.
If prefetch_count was set to 2, how would the 'Messages Sent by Server' change at step 1?
AIt would be 1
BIt would be 0
CIt would be 2
DIt would be 3
💡 Hint
Prefetch count controls how many messages the server sends before waiting for ack; see concept_flow.
Concept Snapshot
Fair dispatch with prefetch:
- Set prefetch_count to limit unacknowledged messages.
- Server sends only up to prefetch_count messages.
- Consumer processes and acknowledges each message.
- Server waits for ack before sending more.
- Ensures balanced load among consumers.
Full Transcript
Fair dispatch with prefetch in RabbitMQ means the consumer tells the server how many messages it can handle at once by setting prefetch_count. The server then sends only that many messages before waiting for acknowledgments. This prevents one consumer from getting overloaded and ensures messages are fairly distributed. The example code sets prefetch_count to 1, so the consumer gets one message at a time. The execution table shows the server sending one message, the consumer processing and acknowledging it, then the server sending the next. This cycle repeats until all messages are processed. If the consumer does not acknowledge, the server stops sending new messages. This method balances work fairly among consumers.