LESSON 03 · Delivery semantics

Get one effect from repeated delivery

In practice, “exactly once” means a record may be delivered more than once while its effect is applied only once.

SYSTEM VIEW

The data flow

Diagram view
Delivered—
Credited—

Watch a consumer handle one payment, credit account 9 by $20, then replay it with a crash in the middle to see it lost, doubled and applied once.

  1. The consumer reads offset 500, credit account 9 by $20, and does two things: the work, crediting Postgres from $100 to $120, and the commit, moving its offset on to 501. A crash can land between the two, and their order decides what breaks.

    Work
    credit in Postgres
    Commit
    next offset, 501

    What changed

    • Account 9: $100 → $120
    • Committed offset 500 → 501
  2. Replay 500, committing first: at-most-once. The consumer commits 501, then crashes before the credit. Its replacement resumes at 501, so account 9 stays at $100: the $20 is lost, never repeated. That suits high-volume telemetry, where a missing sample is harmless.

    Order
    commit, then work
    Account 9
    $100, not $120

    What changed

    • Commit 501 before the credit
    • Consumer crashes
    • $20 credit lost
  3. Replay 500, working first: at-least-once. The consumer credits account 9, $100 → $120, and crashes before committing. Its replacement resumes at 500 and credits another $20: $140. Redelivery can’t be designed away: after a crash, the sender can’t tell whether the work finished.

    Order
    work, then commit
    Should be
    $120
    Is
    $140

    What changed

    • Credit before the commit
    • Crash before the commit
    • Second $20 credit: $140
  4. “Exactly once” in practice is at-least-once delivery plus an effect that lands once. Replay 500: one transaction credits account 9 and inserts event ID 500 under a unique constraint. After the same crash, the repeat’s insert conflicts and changes nothing: $120.

    Unique key
    event ID
    Account 9
    $120
    Cost
    IDs kept past the longest replay

    What changed

    • evt 500 in the credit’s transaction
    • Repeat conflicts: still $120
  5. Or make the effect transactional: keep the offset in Postgres. Replay 500: one transaction writes $120 and offset 501, so a crash leaves both or neither; here the replacement reads 501 and moves on. Kafka transactions do the same when output and offsets both live in Kafka.

    One transaction
    credit + offset
    Resume from
    501, in Postgres

    What changed

    • Offset kept in Postgres
    • $120 and offset 501 commit together
    • Replacement resumes at 501
  6. Neither fix reaches a third-party Email API. Replay 500: the consumer sends account 9’s receipt, then crashes before its transaction commits. The replacement resumes from offset 500 in Postgres and redoes both: one credit, but a second email. Use the provider’s own idempotency support, or accept the risk.

    Account 9
    $120, credited once
    Emails sent
    2

    What changed

    • Email API
    • Crash after the email, before the commit
    • Second receipt sent

Space plays · ← → step · Home resets

THE IDEA

How it works

A consumer does two things for each record: it does the work and it records its progress. A crash can land between them, and their order decides what breaks. Commit the offset first and you get at-most-once delivery: a crash after the commit skips the record, so it is lost but never repeated. That suits high-volume telemetry where a missing sample is harmless. Do the work first and you get at-least-once delivery: a crash before the commit repeats the record, but nothing is lost.

A consumer reads offset 500, “credit account 9 by $20”, writes the credit to Postgres, and crashes before committing. Its replacement resumes at offset 500 and credits another $20. Redelivery cannot be designed away, because after a crash or a lost acknowledgment the sender cannot tell whether the work finished. What you can control is the effect.

So “exactly once” in practice means at-least-once delivery plus an effect that lands once. An idempotent effect gives the same result when repeated: insert the event ID into a table with a unique constraint, in the same transaction as the credit, and the repeat conflicts and changes nothing. A transactional effect commits the result and the offset atomically, by keeping the offset in Postgres with the credit and resuming from it, or through Kafka transactions when the output and offsets both live in Kafka. Neither reaches a third-party email or card API, which needs the provider’s own idempotency support or an accepted risk. Deduplication tables also cost storage and must outlive the longest possible replay.

DECISION POINT

A consumer turns “order shipped” events into emails through a third-party API with no idempotency support. A teammate suggests enabling Kafka transactions to stop duplicates. What do you decide?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Committing before the work risks loss; committing after it risks repeats.
  • Exactly once describes the effect, built on at-least-once delivery.
  • Kafka transactions stop at the edge of Kafka, so external calls need their own protection.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

A consumer commits each offset before writing the result to its database. It crashes between the two steps. What happens to that record?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.