SkillSprout
Chapter 29 · Lesson 2
Advanced
3 min read

Delivery Guarantees, Retries and Dead-Letter Queues

Overview

Networks and servers fail, so a broker must decide what happens when a message might not have been processed. This lesson explains the delivery guarantees, why duplicates happen, and how to handle bad messages safely.

Detailed Notes

What is a delivery guarantee

A delivery guarantee describes how many times a message may be processed. It depends on acknowledgements (acks): after a consumer finishes a message, it tells the broker "done". Until then the broker keeps the message.

The problem it solves

Suppose a consumer receives a message, charges a card, and crashes before sending the ack. The broker cannot know whether the work happened. It must choose: risk losing the message, or send it again and risk a duplicate.

How it works

  • At-most-once: the broker delivers once and never retries. No duplicates, but messages can be lost. Fine for low-value data like a metrics sample.
  • At-least-once: the broker retries until acked. Nothing is lost, but duplicates can occur. This is the common default.
  • Exactly-once: each message has an effect exactly once. This is hard and usually works only inside one system or with extra effort. In practice, teams get the same result by combining at-least-once delivery with idempotent processing.

Idempotent means doing it twice has the same effect as doing it once. For example, "set status to PAID for order 42" is idempotent; "add 10 to the balance" is not. A common technique is to store the IDs of processed messages and skip repeats, or use a unique key in the database.

Retries and dead-letter queues (DLQ): some messages can never succeed, for example malformed data. Retrying forever blocks the line. After a set number of failed attempts (often with growing waits between them), the broker moves the message to a dead-letter queue, a side queue where engineers can inspect and replay it later.

Ordering: queues generally do not guarantee strict global order. If order matters only per customer, send that customer's messages through the same lane (Kafka does this with partitions, next lesson).

A simple example

A courier delivers a parcel but the "delivered" confirmation gets lost, so the shop sends a second parcel. That is at-least-once. If you notice you already have order 42 and refuse the second, you are idempotent.

System design example

A payment consumer reads "charge order 42 for $30". It first checks a table of processed order IDs. If 42 is there, it just acks. Otherwise it charges the card and records 42 in the same database transaction, then acks. A crash before the ack causes a redelivery, which is safely ignored. A message that fails five times goes to the payments DLQ. Queue depth (how many messages are waiting) shows when consumers are falling behind.

Summary

  • Acks tell the broker a message is finished; no ack means redelivery.
  • At-most-once may lose messages; at-least-once may duplicate them.
  • Build idempotent consumers; this gives the effect of exactly-once.
  • Use retries with backoff, then a dead-letter queue for poison messages.
  • Do not rely on global ordering; order per key instead.

Architecture Diagram

Retries and dead-letter queue
Architecture
Producer
Queue
Consumeridempotent
Dead-letter queue

Failed messages are retried, then parked for inspection.

  1. 1The consumer receives a message and processes it.
  2. 2On success it acknowledges; on failure the broker redelivers.
  3. 3After too many failures the message moves to the DLQ.
  4. 4Engineers inspect and replay DLQ messages.

Common Interview Questions

Finished this lesson?

0 of 91 lessons done · 0% of the course