Fair dispatch with prefetch in RabbitMQ - Time & Space Complexity
We want to understand how the work done by RabbitMQ changes when using fair dispatch with prefetch.
Specifically, how does the number of messages affect the operations performed?
Analyze the time complexity of the following RabbitMQ consumer setup with prefetch.
channel.basicQos(prefetchCount=1)
channel.basicConsume(queue, autoAck=false, consumer)
consumer.handleDelivery = (msg) => {
processMessage(msg)
channel.basicAck(msg.deliveryTag)
}
This code sets a prefetch count of 1 to ensure fair dispatch, processes each message, then acknowledges it.
Look at what repeats as messages arrive.
- Primary operation: Processing each message one by one.
- How many times: Once per message received from the queue.
As the number of messages increases, the consumer processes each message individually.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 message processings and acknowledgments |
| 100 | 100 message processings and acknowledgments |
| 1000 | 1000 message processings and acknowledgments |
Pattern observation: The work grows directly with the number of messages.
Time Complexity: O(n)
This means the time to process messages grows linearly with how many messages arrive.
[X] Wrong: "Setting prefetch to 1 makes processing faster overall."
[OK] Correct: Prefetch controls how many messages are sent before ack, but processing each message still takes time. It just spreads work evenly among consumers.
Understanding how message processing scales helps you explain system behavior and resource use clearly in real projects.
"What if we increased prefetchCount to 10? How would the time complexity change?"