Delivery guarantees

Topic 7 of 9 in Week 4: Caching, queues and content delivery9 min readIntermediateFree course

Overview

Networks drop packets and processes crash at the worst possible moment. When a consumer takes a message from a queue, does work and acknowledges it, a failure can happen between any of those steps. Delivery guarantees describe what can happen to a message as a result: lost, duplicated, or neither. Choosing the right one, and handling its consequences, is central to reliable asynchronous systems.

It depends on when you acknowledge

Delivery guarantees. At-most-once: the consumer receives a message, acknowledges it, then processes it; if it crashes while processing, the message was already acknowledged and is lost, which is fine for metrics, logs and typing indicators. At-least-once: the consumer receives, processes and then acknowledges; if it crashes before acknowledging, the message is redelivered and processed twice, which is the default, so consumers must be idempotent. Exactly-once in practice: at-least-once delivery plus idempotent processing gives an exactly-once effect; or store the result and the consumer offset in the same transaction; Kafka transactions give exactly-once within Kafka, but side effects outside still need idempotency.

At-most-once

Acknowledge (or commit the offset) before processing. If the consumer crashes mid-work, the message is gone.

  • Never processed twice; may be lost.
  • Fine for data where loss is acceptable and duplicates are annoying: metrics samples, logs, live typing indicators, real-time positions that are replaced a second later.

At-least-once

Acknowledge after processing. If the consumer crashes after doing the work but before acknowledging, the message is delivered again and processed a second time.

  • Never lost (as long as the broker keeps it); may be duplicated.
  • The practical default for anything that matters: SQS, RabbitMQ, Google Pub/Sub and typical Kafka consumers all work this way.

Exactly-once

The ideal: every message processed once, no matter what fails. Across independent systems connected by an unreliable network, true exactly-once delivery can’t be guaranteed: there’s always a moment where a sender can’t know whether the receiver got the message. What you can build is an exactly-once effect.

Getting an exactly-once effect

Idempotent processing

Make processing the same message twice have the same effect as once:

  • Natural idempotency: “set status to shipped”, “write document 981 to the search index” and “delete item 7” are already safe to repeat.
  • Deduplication by ID: record each processed message ID (in a table with a unique constraint, or Redis with an expiry) and skip IDs you’ve seen. Do the record and the work in one database transaction where possible.
  • Idempotency keys for external calls: pass the message ID to the payment provider so a retried charge is recognised and ignored.
  • Conditional updates: apply an update only if the version is newer than the stored one, which also handles out-of-order delivery.

Transactional progress

If the consumer’s output goes to a database, store the result and the consumed offset (or message ID) in the same transaction. After a crash, the consumer resumes from the stored offset, and work either happened together with the offset or not at all.

Within Kafka

Kafka supports idempotent producers and transactions that atomically write output messages and commit input offsets, giving exactly-once processing for read-from-Kafka, write-to-Kafka pipelines (Kafka Streams uses this). Side effects outside Kafka, like sending an email or charging a card, still need idempotency.

The producer side

Duplicates also start at the producer. If a producer sends a message, the broker stores it, and the acknowledgement is lost, the producer retries and the message is stored twice. Remedies:

  • idempotent producer settings (Kafka assigns sequence numbers and drops duplicates),
  • deduplication windows (SQS FIFO deduplicates by ID for five minutes),
  • a unique message ID generated once by the producer, so consumers can deduplicate anyway.

Ordering and duplicates together

Retries can deliver an old message after a newer one (“picked up” after “delivered”). Version numbers or timestamps on messages let consumers ignore stale updates, which solves both duplicates and reordering for state-style events.

In an interview

Say the guarantee and how you handle its consequence: “The queue is at-least-once, so the payment consumer is idempotent: it uses the order ID as the idempotency key with the payment provider and records processed message IDs in the same transaction as the payment record.” This answers the most common follow-up before it’s asked.

Recap

Acknowledging before processing gives at-most-once (possible loss); acknowledging after gives at-least-once (possible duplicates). Exactly-once delivery isn’t achievable in general, but an exactly-once effect is: use at-least-once delivery with idempotent consumers, deduplication by message ID, idempotency keys for external calls, or transactions that store results together with progress.

Exercise

Pick the guarantee

For each consumer, choose at-most-once or at-least-once delivery and say how you'd handle duplicates if any: (a) a service that records page-view counts for a dashboard; (b) a service that charges a customer's card when a SubscriptionRenewed event arrives; (c) a service that sends a 'Your driver has arrived' push notification; (d) a service that writes every chat message into the search index.

Text
(a)
(b)
(c)
(d)

Summary

  • At-most-once: a message is delivered zero or one times; acknowledge before processing; messages can be lost.
  • At-least-once: a message is delivered one or more times; acknowledge after processing; duplicates are possible.
  • Exactly-once delivery across networks isn't achievable in general; aim for an exactly-once effect.
  • Exactly-once effect = at-least-once delivery + idempotent processing, or storing results and progress in one transaction.
  • Producers have the same problem: a retry after a lost acknowledgement can publish a message twice.