Message Queues & Redelivery
Message queues retain work until consumers acknowledge completion, but redelivery can repeat work that already succeeded. If the business write commits and its ACK never reaches the queue, the same message may arrive again. Atomic deduplication lets the consumer recognize that message without applying its effect twice.
The awkward part is what the queue cannot see. A worker that crashed before writing and a worker that wrote successfully but lost its ACK leave the same silence. Retrying rescues the first case. Without a guard, it repeats the write in the second.
How Message Queues Handle Unacknowledged Work
This lesson uses a lease-based work queue. A producer publishes M1, carrying increment by 1.
The queue retains a record in Ready. When W1 receives it, that record moves to In flight: the
queue still owns pending work, while W1 has one delivery attempt. A lease makes the message
unavailable for the model's other receiver until completion or expiry.
The receive has its own receipt, r1. The receipt identifies this attempt; M1 identifies the
logical message. After processing, W1 sends a completion ACK. An accepted ACK removes the
retained record. If none arrives before the lease expires, M1 becomes Ready again. A later receive
gets receipt r2, possibly at another worker.
That is the distinction to keep: receiving, committing a business effect, and acknowledging
completion are separate events. The queue cannot infer the database result from a missing ACK.
In Amazon SQS, the comparable completion operation is DeleteMessage, rather than an API named
ACK. Its visibility timeout documentation
explains why receiving a message does not delete it and why an uncompleted receive can become
visible again.
Message Queues & Redelivery Step by Step
The default selects Apply every delivery and ACK lost. The counter starts at zero. One playback includes the protocol trace, a controlled replay, and a split-commit counterexample. Comparison panels are separate experiments; their counters do not accumulate across chapters.
- P sends M1. The queue accepts it into Ready: one published message, no deliveries yet.
- The queue reserves M1 for W1 with receipt r1. Its retained record moves to In flight.
- W1 receives delivery a1. The delivery count becomes one; the business counter is still zero.
- W1 inspects the store without a deduplication guard, then commits the increment. The counter
becomes one and the ledger records
M1 / a1 / W1. - W1 sends ACK r1. It is lost before reaching the queue. The committed increment remains.
- The remaining lease expires. Two panels compare a crash before writing (counter zero) with a lost ACK after writing (counter one). Both leave the queue holding M1 without an ACK. Back in the original trace, the same M1 returns to Ready; no second delivery has happened yet.
- W2 receives M1 under a new receipt, r2. There are now two deliveries of one published message.
- W2 also applies every delivery. It commits another increment: the counter becomes two, and
the ledger gains
M1 / a2 / W2. - ACK r2 reaches the queue, which removes its retained record. The final totals are one publication, two deliveries, and two business effects.
- Two fresh stores replay the same M1, workers and lost ACK. Only the consumer rule differs. Both commit once, lose the ACK, and receive M1 again. The unguarded handler writes twice; the atomic handler finds M1 and skips. Their publication/delivery/effect totals are 1/2/2 and 1/2/1.
- A fresh counterexample splits the business write and ID record into two commits. W1 commits the increment, then crashes before recording M1. Counter one survives beside an empty ID set. After expiry, W2 receives M1, finds no ID, and commits another increment. Counter two is visible before W2's later ID write and ACK; the demonstration stops at that failure.
- The adjacent atomic experiment commits the increment and unique M1 together. Its ACK is lost, W2 receives M1 again, and the durable ID prevents a second write. The counter stays one. W2 must still ACK the handled duplicate to finish the queue work.
The retained M1 card slides between Ready and In flight while separate envelopes cross to the workers. The shared-store ledger preserves each committed effect. Those two surfaces keep queue custody and business completion visible at the same time. In the comparisons, a transaction bracket encloses the counter and processed ID; the split-commit example exposes the durable half-state that this bracket prevents.
Why Atomic Deduplication Changes the Result
The default playback already compares both rules. To inspect the guarded protocol by itself, select Deduplicate atomically while keeping ACK lost. W1 now commits the increment and a unique processed-ID record for M1 in the same database transaction. When W2 handles the retry, it finds that durable record and skips the write. W2 still acknowledges the handled duplicate. The queue can finish, and the counter remains one.
| Same lost ACK | Published | Delivered | Business effects |
|---|---|---|---|
| Apply every delivery | 1 | 2 | 2 |
| Deduplicate atomically | 1 | 2 | 1 |
The ID must survive worker changes and crashes. W1's private memory is insufficient. The transaction boundary also matters: commit the effect first and crash before storing the ID, and a retry can apply the effect again. Commit the ID first and crash before the effect, and a retry can skip work that never happened. A unique ID constraint and the business write belong in the same transaction, as in the Idempotent Consumer pattern.
For d delivery attempts, this example invokes the handler d times. Deduplication suppresses repeated committed effects; it does not eliminate receive traffic or the work of checking IDs. One effect in this trace does not establish exactly-once delivery. Standard SQS queues explicitly require applications to tolerate at-least-once delivery.
Edge Cases
- No failure: either consumer rule produces one delivery and one effect. The ACK clears M1.
- ACK lost after commit: the default exposes a duplicate effect. Atomic deduplication keeps the second delivery from changing the counter.
- Crash before commit: W1 writes nothing and sends no ACK. After expiry, W2 completes the pending increment. Both consumer rules end with one effect.
- Different retry worker: W2 reads the shared durable store. It does not inherit W1's memory.
Common Mistakes
- Acknowledge before committing. If the worker then crashes, the queue has already removed the work and the business effect may never happen.
- Generate a new message ID on each retry. Deduplication cannot recognize the same logical work if its identity changes.
- Treat an ID lookup and a later write as an atomic guard. Separate operations leave a crash window and can race with another consumer. Use a transaction with a uniqueness constraint.
- Assume the queue makes an external API call exactly once. A local database transaction cannot atomically include an unrelated email or payment API. The receiving service needs an appropriate idempotency or coordination contract.
A Note on Simplification
Each experiment fixes one message, two workers, and at most one scheduled fault. The six configurable protocol traces assume W2 is available when needed and the second ACK succeeds. The added comparisons stop at the decision or write under discussion; they do not show another completed ACK. The atomicity chapter contrasts a crash between separate commits with ACK loss after one atomic commit. These are distinct failure histories, not a one-variable comparison. Real repeated failures do not promise eventual success. The lease meter is schematic, not a timeout value measured in animation seconds.
The model omits replication, publisher confirms and retries, concurrent leases, ordering, receipt races, lease extension, retention limits, retry backoff, poison messages, and dead-letter queues. Real standard queues may also duplicate messages during a visibility timeout. The guarded counter and processed-ID record share one transactional store; arbitrary external effects do not.
Keep exploring
A few lessons that share the same technique or lead into the next idea.
Premium players need paid access. Every Explain doc is free.
Found a mistake or something unclear? Leave feedback.