A report worker receives jobs through RabbitMQ and writes each completed result to a database. Sometimes it finishes the database write but crashes before RabbitMQ sees the acknowledgment. Walk through that gap before saying the system delivers anything “exactly once.” This example assumes AMQP 0-9-1, publisher confirms, and manual consumer acknowledgments.
Explain what each side knows
1. Does a publisher confirm mean a consumer finished?
No. Publisher confirms cover communication between the publisher and the broker; the consumer's acknowledgment is a separate signal. A publish that cannot be routed can still be confirmed. If the publisher needs to detect routing failures, use the mandatory flag and handle returned messages as well as confirms. Also decide what a confirm must cover for the workloads you are discussing: durability depends on the queue type and publish settings, not just enabling confirms.
2. When should the consumer acknowledge a job?
Acknowledge after the durable business effect succeeds. Acknowledging before committing can lose work if the consumer crashes. Acknowledging afterward leaves a different gap: the commit can succeed while the acknowledgment never arrives. In that case the job can be redelivered. Use a stable job ID and make the result write idempotent, for example by enforcing a unique ID where the result is committed. A check in one transaction followed by a write in another does not prevent concurrent duplicates.
- PublishSend a job with a stable ID and handle the broker's confirm or return.
- DeliverThe consumer receives the job and leaves it unacknowledged during processing.
- CommitSave the result using the job ID as a durable deduplication key.
- AcknowledgeTell the broker the delivery is complete. If the acknowledgment is lost, the result must still be safe to repeat.
Decide which failures deserve another attempt
3. Why is immediately requeueing every failure dangerous?
A consumer can receive the same failing message again as soon as it is requeued. If a dependency is down, many workers may spend all their time retrying. Give transient failures a delay and a finite budget. Route permanently malformed jobs to an appropriate dead-letter destination if configured, and investigate them instead of requeueing them indefinitely. Explicitly decide how to count attempts and how operators can retry a repaired job.
4. What does prefetch protect, and what does it not protect?
With manual acknowledgments, a positive prefetch value limits outstanding unacknowledged deliveries within its configured scope. RabbitMQ applies the usual non-global value per consumer. A small value can prevent one consumer from hoarding long jobs, but can reduce throughput for fast jobs when network latency matters. Prefetch limits deliveries, not the number of database calls that a single job can launch. Tune it with worker concurrency, downstream capacity, and processing-time variation in mind.
5. What can go wrong when the publisher retries?
On disconnect, a publisher may not know whether the broker accepted an unconfirmed publish. Retrying can create a duplicate if the original succeeded and its confirm was lost. Keep the same job ID when retrying the same operation. The consumer's deduplication still matters even if it never observed a previous delivery. Do not treat the broker's redelivery flag as your only mechanism for finding duplicate jobs: a duplicate publish may be a separate message.
Practice by moving the crash
- Put the crash before the database commit, after commit, and after acknowledgment. For each case, state what can repeat and what must not.
- Give one job a permanent format error and another a transient timeout. Explain how each leaves the main work queue.
- Compare one consumer holding many jobs with several consumers holding few. Pick a metric for queue delay and another for downstream saturation.
Read the outbox and partial-failure walkthrough for the broader cross-service contract. Then use Mentaro's free practice demo to rehearse the crash sequence without notes.