Manual Acknowledgments & Consumer Prefetch (BasicQos)
Quick Answer: Turn off autoAck so RabbitMQ keeps every message safe until your C# worker explicitly calls BasicAck. Pair it with BasicQos prefetch limits so a crashing worker never hoards or drops in-flight tasks.
Imagine Karthik runs an electricity bill payment portal in Chennai. On the last day of the month, Lakshmi pays her Rs. 2,400 bill (BILL-8812).
Her payment enters a RabbitMQ queue for background processing. One second later, the C# worker reading that queue runs out of memory and crashes.
How do we guarantee Lakshmi's payment is never lost, even if a worker dies mid-task?
1. Why Do We Need Manual Acks and BasicQos?
The problem with auto-acknowledgment
By default, many beginner examples set autoAck: true and leave prefetch unlimited. RabbitMQ deletes a message the instant it pushes it over the network.
Example 1: Unsafe C# consumer with autoAck enabled
This works on a quiet test machine, but loses real money when a worker crashes.
// DANGER: No BasicQos limit and autoAck is true
await channel.BasicConsumeAsync(
queue: "q.bill.payments",
autoAck: true,
consumer: consumer);
What happens when Worker 1 crashes
RabbitMQ pushed 500 bills to Worker 1 -> deleted all 500 from queue. Worker 1 crashed while saving BILL-8812! Result: Lakshmi's bill and 499 other payments vanished forever.
This setup works when nothing ever fails. However, it breaks in production.
- RabbitMQ deletes
BILL-8812before Worker 1 even finishes writing to SQL Server. - Without a prefetch limit, Worker 1 grabs hundreds of bills into RAM at once.
- A fast Worker 2 sits idle while a slow Worker 1 hoards the entire queue.
Now think about 20,000 families paying bills before midnight. We need RabbitMQ to hold each message until a worker proves the job is done.
The solution: Manual Ack + BasicQos Prefetch
Manual Acknowledgment = a rule where RabbitMQ keeps a message locked until the worker sends back a receipt.
Consumer Prefetch (BasicQos) = a limit on how many unacknowledged messages one worker can hold at the same time.
When Karthik sets prefetchCount: 1 and autoAck: false, RabbitMQ gives each worker just one bill. If Worker 1 crashes, RabbitMQ instantly hands BILL-8812 to Worker 2.
Live Recovery: BasicQos (Prefetch = 1) & Crash Redelivery
Watch how RabbitMQ protects Lakshmi's BILL-8812 when Worker 1 crashes before calling BasicAck.
Notice that BILL-8814 stays safely in the queue until a worker frees up its prefetch slot. When Worker 1 dies, RabbitMQ detects the closed TCP connection and redelivers BILL-8812 to Worker 2.
2. How BasicQos, BasicAck, and BasicNack Work in C#
BasicAck = tells RabbitMQ "I finished this message, you can delete it now."
BasicNack = tells RabbitMQ "I failed to process this message," with a flag to either requeue it or drop it.
Example 2: Safe C# consumer with BasicQos and manual acks (RabbitMQ.Client v7)
Set prefetch BEFORE starting the consumer, then ack only after the database save succeeds.
// 1. Allow only 1 unacked message at a time on this channel
await channel.BasicQosAsync(prefetchSize: 0, prefetchCount: 1, global: false);
var consumer = new AsyncEventingBasicConsumer(channel);
consumer.ReceivedAsync += async (sender, ea) =>
{
try
{
await SaveBillPaymentAsync(ea.Body);
// 2. Success: confirm and remove from queue
await channel.BasicAckAsync(ea.DeliveryTag, multiple: false);
}
catch (Exception)
{
// 3. Failure: reject without requeuing endlessly
await channel.BasicNackAsync(ea.DeliveryTag, multiple: false, requeue: false);
}
};
// 4. Turn OFF autoAck so RabbitMQ waits for our receipt
await channel.BasicConsumeAsync("q.bill.payments", autoAck: false, consumer: consumer);
Expected output
[Worker 2] Saved BILL-8813 -> BasicAck sent (tag 1) [Worker 1] CRASHED on BILL-8812 -> Connection closed! [Worker 2] Received redelivered BILL-8812 -> BasicAck sent (tag 2)
The DeliveryTag is a unique number per channel that identifies which message you are acknowledging. Passing multiple: false ensures you only confirm that single bill.
3. Auto-Ack vs. Manual Ack with BasicQos
| Auto-Ack (Unsafe) | Manual Ack + BasicQos (Safe) |
|---|---|
| Messages are deleted as soon as they leave the broker. | Messages stay locked until BasicAck arrives. |
| A crashing worker drops all in-flight tasks. | RabbitMQ automatically redelivers unacked tasks. |
| One worker can hoard hundreds of messages. | Work spreads fairly across all healthy workers. |
The important terms to remember
- DeliveryTag = a channel-level sequence number used to ack or nack a specific message.
- Unacked State = a message delivered to a worker but not yet confirmed or rejected.
- BasicQos = Quality of Service setting that caps how many unacked messages a consumer can hold.
Design tip: Use prefetchCount: 1 for slow, heavy jobs like PDF generation or bank calls. For fast jobs under 10 ms, raise prefetch to 10–50 so network round trips do not bottleneck throughput.
4. Common Mistakes to Avoid
1. Forgetting to call BasicAck or BasicNack
If an unhandled exception skips your ack call, the message stays Unacked forever. Once your worker hits its prefetchCount limit, it freezes and stops receiving new messages.
Better approach: Always wrap your consumer handler in a try/catch block so every code path ends with BasicAckAsync or BasicNackAsync.
2. Setting requeue: true on a broken message
If a bill has corrupt JSON and you call BasicNackAsync(..., requeue: true), RabbitMQ puts it right back at the front of the queue. Your worker will crash on it in an endless loop.
Better approach: Set requeue: false and route rejected messages to a Dead-Letter Exchange (DLX) for inspection.
5. Interview Questions & Practice
Question 1: Why does BasicQos matter when you run multiple C# workers?
What the interviewer expects: Understanding of fair dispatch and memory protection.
Strong answer: "Without BasicQos, RabbitMQ uses round-robin and pushes messages as fast as possible into each worker's memory. In Karthik's bill portal, if Worker 1 gets stuck on a slow bank call, it still receives new bills while Worker 2 sits idle. Setting prefetchCount: 1 forces RabbitMQ to send the next bill only to a worker that is actually free."
Practice: UPI refund worker
Write the BasicQosAsync and BasicConsumeAsync calls for a q.upi.refunds queue where each C# worker should hold at most 5 refunds in flight and use manual acknowledgments.
Expected result: BasicQosAsync(0, 5, false) followed by BasicConsumeAsync("q.upi.refunds", autoAck: false, consumer).
6. Next Steps
- Previous Article: Message Durability, Persistence & Publisher Confirms
- Next Article: Dead-Letter Exchanges (DLX) & Poison Message Handling
Discussion & Comments
0No comments yet
Start the discussion above — ask a question, share a tip, or reply to a comment.