Distributed systems · L1Queues vs logs and delivery guarantees

Queues vs logs and delivery guarantees

A queue forgets a message once it is done. A log keeps it. Know which one you have, and what "delivered once" really means.

L1 Must-knowDistributed systems18 min read
Fact-checked by AI against cited sources · Human review pendingLast reviewed 21 Oct 2026 · How we check content
Senior at work · lesson 14 of 26
Section 1 of 8

What broke

A made-up story to show a common failure. Not about a real company.

A billing service puts one message on an Amazon SQS queue for each invoice. A worker takes the message, builds a PDF, emails it to the customer, and deletes the message.

For most customers this takes 5 seconds. For a few large customers, the PDF has thousands of lines and takes 50 seconds. The queue uses the default visibility timeout of 30 seconds.

At second 30, the message for a large customer becomes visible again. A second worker takes it and starts the same invoice. At second 50, the first worker sends the email and deletes the message. At second 80, the second worker sends the same email again.

The biggest customers, the ones the company cares about most, get every invoice two or three times. Their finance teams start asking if they were charged twice. Nothing in the logs shows an error. Each worker did exactly what it was told.

Section 2 of 8

The idea in simple words

Both queues and logs let one service hand work to another without waiting. The sender is called the producer. The service that does the work is the consumer. A server in the middle, the broker, holds the messages.

A queue, like SQS or RabbitMQ, hands each message to one consumer. When the consumer says "done", the broker deletes the message. Many workers can share the load. 1 8

A log, like Kafka, is a file you only add to. Each message gets a number, its offset. Consumers do not delete anything. Each consumer just remembers how far it has read. Messages stay until a time limit you set, so you can read them again later. 3 2 You can also set a size limit for each partition. 9

The key idea. In a queue, "done" deletes the message. In a log, "done" moves a bookmark. Either way, a crash between doing the work and saying "done" means the work runs again.

Section 3 of 8

How it works

Step through a worker crash in both systems. Use Next and Back, or the ← and → keys.

A worker crashes mid-batch: what a queue and a log do nextA · queue (like SQS) m1 m2 m3 m4 Worker 1 hidden for 30 s, not deleted crash: m3 done,not deleted Worker 2m3, m4 againm3 runs twice B · log (like a Kafka partition) 0 1 2 3 4 5 commit: 0 Worker Aread 0–3 did 0, 1, 2crash before commit Worker Bfrom 00, 1, 2 run againlog still has 0–5 ✓ Both: at-least-once. Make it idempotent. SeniorPath
  1. A queue, like Amazon SQS. Worker 1 receives 4 messages. They stay in the queue, but other workers cannot see them for the visibility timeout: 30 seconds by default.
  2. Worker 1 handles m1 and m2 and deletes each one. Then it handles m3, but it crashes before it can delete it.
  3. After 30 seconds, m3 and m4 are visible again. Worker 2 receives them, so m3 runs twice. m1 and m2 are deleted, so they are gone for good.
  4. A log, like one Kafka partition. Each message has an offset: 0, 1, 2 and so on. The group's committed offset is 0, so Worker A reads from offset 0.
  5. Worker A handles offsets 0, 1 and 2. It plans to commit after the batch, but it crashes first. The committed offset is still 0.
  6. Worker B, in the same consumer group, takes over the partition. It starts at the committed offset, so 0, 1 and 2 run again. Nothing was deleted. The log keeps every message until retention ends.
  7. Both systems lost nothing, and both ran some work twice. That is at-least-once delivery. Your handler must be idempotent.
Step 1 of 7

The queue: acks and visibility timeout

In SQS, a received message is not removed. It is hidden from other consumers for the visibility timeout, 30 seconds by default. If you do not delete it in time, it becomes visible again, and any worker can receive it. 4

That is the invoice bug. The fix is to set the timeout above your slowest normal job. For long jobs, call ChangeMessageVisibility from time to time, like a heartbeat, to keep the message hidden. The total is capped at 12 hours from the first receive. 4

RabbitMQ works in a similar way with acks. With manual acks, if a consumer's channel or connection closes before it acks, RabbitMQ puts the message back in the queue. It marks it as redelivered. 8 With automatic acks, RabbitMQ counts a message as delivered as soon as it sends it. The docs call this mode unsafe, because a crash can lose the message. 8

The log: partitions, offsets and consumer groups

A Kafka topic is split into partitions. Messages with the same key, such as a customer ID, always go to the same partition. Inside one partition, every consumer reads messages in the order they were written. 3 Do not count on any order across partitions.

A consumer's position in a partition is a single number: the offset of the next message to read. The consumer commits this number to save its progress. It can also rewind to an old offset and read again, for example after you fix a bug. 2

Consumers that share the work join a consumer group. Each partition is read by exactly one consumer in the group at a time. 2 So a topic with 6 partitions can keep at most 6 consumers busy in one group. Other groups, like an analytics job, read the same messages with their own offsets.

Three delivery guarantees

The Kafka docs give three meanings. 2

  • At-most-once. Messages may be lost, but never run twice. You get this if you save your position before you do the work.
  • At-least-once. Messages are never lost, but may run twice. You get this if you do the work, then save your position. Kafka gives this by default.
  • Exactly-once. Each message is processed once and only once.

SQS standard queues are at-least-once too. AWS says more than one copy of a message might be delivered, and order is only best effort. 5

Why "exactly once" usually means "at least once, plus idempotency"

Your work and your "done" are two separate steps. A crash can always fall between them. So the broker must either skip the message or send it again.

Kafka gives exactly-once when you read from Kafka topics and write to Kafka topics, for example in Kafka Streams. It uses transactions, so the output and the new offset are saved together. But for an outside system, like a database or an email API, the docs say this "generally requires cooperation" from that system. One way is to store the offset in the same database as the result, in one transaction. 2

SQS FIFO queues promise "exactly-once processing". Read the details. It means SQS will not add a duplicate if you retry a send within a 5-minute window. 6 It cannot stop your worker from crashing after it emails and before it deletes.

The practical answer: accept at-least-once, and make the handler idempotent. Save a message ID or an idempotency key with the result. Skip any message you have already done. The lesson on idempotency, retries and timeouts shows how.

Dead-letter queues

Some messages fail every time, for example because of bad data. Without a limit, they come back forever. In SQS you set maxReceiveCount. After that many receives without a delete, the message moves to a dead-letter queue, where you can inspect it. 7

On a standard queue, a moved message keeps its first enqueue time. So set the dead-letter queue's retention longer than the main queue's, or messages may expire before you look. And on a FIFO queue, moving one message aside breaks the strict order of the rest. 7

Backpressure

If producers are faster than consumers, the backlog grows. Kafka consumers pull messages, so a slow consumer just falls behind and catches up later. It is not flooded. 2 RabbitMQ's prefetch setting limits how many unacked messages one consumer can hold. 8 Watch the backlog, or consumer lag in Kafka (how far a consumer is behind the newest message), and alert when it keeps growing. See backpressure and flow control.

Section 4 of 8

Trade-offs

OptionReplay old messages?OrderBest for
Queue (SQS standard, RabbitMQ)No A message is deleted after the ack.Best effort only.Jobs: send email, resize an image. Add workers to go faster.
SQS FIFO queueNo Yes , inside a message group (messages with the same group ID).Jobs where order matters per customer or per order, at lower throughput.
Log (Kafka)Yes , until retention ends.Yes , inside a partition.Events that many teams read, rebuilding data, audit trails, stream processing.
At-most-oncen/an/aMetrics or logs where losing a few is fine. A failed message is not retried.
At-least-once + idempotent handlern/an/aAlmost everything else, including payments. A failed message runs again, so the handler must be safe to repeat.

Simple rule: use a queue for "please do this job". Use a log for "this happened", when more than one reader needs it or you may want to replay. Either way, design for at-least-once.

Section 5 of 8

At work: how a senior uses this

  • For every consumer, ask: "What happens if this message runs twice?" If the answer is "a customer is charged twice", fix the handler before launch.
  • Set the visibility timeout from the slowest normal job, not the average. Add a heartbeat for long jobs.
  • Pick the Kafka key on purpose. It decides order and load. A hot key fills one partition while the others sit idle.
  • Count partitions before you scale consumers. More consumers than partitions in one group means some sit idle.
  • Give every queue a dead-letter queue and an alert on it. A dead-letter queue nobody watches is lost data.
  • Write down the guarantee for each topic in its README: at-least-once, the key, and where order holds.
Section 6 of 8

With AI

What to ask the agent

"Here is our SQS consumer. Find every point where a crash would cause a message to run twice or be lost. Then make the handler idempotent using the message ID."

"Our longest job takes up to 3 minutes. Add a heartbeat that extends the SQS visibility timeout every 60 seconds while the job runs, and stops when it ends."

"Write a test that kills our Kafka consumer after it processes a message but before it commits the offset. Assert that the restarted consumer does not create a duplicate row."

What to check yourself

  • Offsets are committed, or messages deleted, only after the work is saved. Look for auto-commit settings that commit on a timer.
  • The idempotency check and the result are saved in one transaction. A check in one step and a write in another can still race.
  • The test really crashes the process between the work and the ack. A clean shutdown proves nothing.
  • The agent did not "fix" duplicates by turning off retries. That only swaps duplicates for lost messages.
  • Ordering code does not assume order across partitions or message groups.
Section 7 of 8

Check yourself

1. An SQS worker needs 50 seconds for a job. The visibility timeout is 30 seconds. What happens?

Show answer

The message becomes visible again after 30 seconds, and another worker can run the same job. SQS does not know the worker is busy. If the message is not deleted before the timeout, it comes back. Extend the timeout while you work.

2. A Kafka consumer saves its offset first, then processes the messages. It crashes in between. What delivery do you get?

Show answer

At-most-once. The messages it did not finish are skipped. The saved offset is already past those messages, so the next consumer starts after them. The Kafka docs call this at-most-once.

3. Your consumer reads from Kafka and charges a card with an outside payment API. How do you get "exactly once" in practice?

Show answer

Use at-least-once delivery and make the charge idempotent, for example with an idempotency key. Kafka transactions cover writes inside Kafka. An outside system sees retries, so it must ignore duplicates itself.

4. Events for one bank account must be handled in order. You use Kafka. What do you do?

Show answer

Use the account ID as the message key, so all its events go to one partition. Kafka keeps order only inside a partition. Events with the same key go to the same partition.

5. What does maxReceiveCount do on an SQS queue with a dead-letter queue?

Show answer

It sets how many times a message can be received before SQS moves it to the dead-letter queue. After that many receives without a delete, SQS gives up on the message and moves it aside, so it stops coming back forever.

Your progress is saved only in this browser. Export it to keep it safe.

Was this clear?
Report a mistake (opens GitHub in a new tab)
Section 8 of 8

Go deeper

Show 9 sources and related lessons
  1. Martin Kleppmann, Designing Data-Intensive Applications (O'Reilly)
    1st edition (2017): chapter 11, Stream Processing (message brokers, acknowledgements and redelivery, log-based message brokers, consumer offsets, replaying old messages).
  2. Apache Kafka documentation: Design (Kafka 4.3) (opens in a new tab)
    Sections on the consumer position, push vs pull, and message delivery semantics. Defines at-most-once, at-least-once and exactly-once.
  3. Apache Kafka documentation: Introduction (Kafka 4.3) (opens in a new tab)
    Topics, partitions, keys, ordering within a partition, and retention.
  4. Amazon SQS Developer Guide: Visibility timeout (opens in a new tab)
    What happens to a message after a worker receives it, the 30-second default, and the 12-hour limit.
  5. Amazon SQS Developer Guide: Standard queues (opens in a new tab)
    At-least-once delivery, possible duplicates and best-effort order.
  6. Amazon SQS Developer Guide: Exactly-once processing in FIFO queues (opens in a new tab)
    What FIFO deduplication covers, and the 5-minute window.
  7. Amazon SQS Developer Guide: Using dead-letter queues (opens in a new tab)
    maxReceiveCount, retention advice, and a warning about FIFO order.
  8. RabbitMQ documentation: Consumer Acknowledgements and Publisher Confirms (opens in a new tab)
    Automatic vs manual acks, redelivery after a crash, and prefetch.
  9. Apache Kafka documentation: Topic Configs (Kafka 4.3) (opens in a new tab)
    retention.ms (time limit) and retention.bytes (size limit per partition) for each topic.

Case studies: Amazon: Why queue backlogs can turn a short outage into a long one · LinkedIn: The log: the simple idea under databases and streams · PhonePe: Moving 100 billion events a day into Kafka · Uber: Retry topics and dead-letter queues with Kafka

Related: Idempotency, retries and timeouts · Backpressure and flow control · Kafka internals · Stream processing · Sagas, outbox and CDC

Before this: Idempotency, retries and timeouts. Next: Kafka internals, and sagas, the outbox pattern and CDC for writing to a database and a queue together. Schema evolution and encoding covers changing the format of messages that are already in the log. Exactly-once and effectively-once shows how to process each message once, and event-driven architecture and event sourcing when to keep events as the source of truth.

Term

See full entry

Jump to a section

  1. 1What broke
  2. 2The idea in simple words
  3. 3How it works
  4. 4Trade-offs
  5. 5At work
  6. 6With AI
  7. 7Check yourself
  8. 8Go deeper