Delivery guarantees

Choose at-least-once or exactly-once, make the consumer idempotent, and park poison messages on a dead-letter topic so a payment is never posted twice.

Updated CompTech Team

Kafka will deliver a record more than once. A producer retries when it is unsure the broker got the write. A consumer crashes after posting a payment and before committing the offset, and the next owner of the partition posts it again. "Exactly once" is a property you build at the place that has the side effect. For a payment, that place is the ledger, not the consumer loop.

This lab sets the producer so it does not duplicate on its own retry, sets the consumer so a second delivery is harmless, and puts records that will never succeed onto a dead-letter topic.

Prerequisites

  • The payments topic and a posting consumer from producers and consumers
  • A database with a unique constraint you can use. Crunchy Postgres is the one in our lab
  • Permission to create a KafkaTopic for the dead-letter records

What the phrases mean

Guarantee What actually happens Fit for payments
At most once Commit the offset before you post, or send without waiting for replicas. You can lose a record No
At least once Post, then commit. A crash in between redelivers. You can double-post unless the post is idempotent Yes, with a unique payment id
Exactly once, inside Kafka A transactional producer writes to output topics and commits offsets as one transaction. A second consumer sees each input once Yes, for consume-transform-produce. It does not cover the database
Exactly once, into a database The database enforces the payment id. Kafka may deliver twice. The second transaction does nothing This is the one to build

Idempotent producer (enable.idempotence=true, which requires acks=all) stops one producer instance from appending the same batch twice when it retries. It does nothing for two instances, and nothing for the consumer. Turn it on anyway. It is the cheap half.

enable.idempotence=true
acks=all
max.in.flight.requests.per.connection=5

Spring and Quarkus turn this on when acks is all and idempotence is left at its default. The .NET snippet in the previous lab sets EnableIdempotence = true explicitly. Keep it.

The ledger rejects the second post

CREATE TABLE posted_payment (
  payment_id text PRIMARY KEY,
  account    text NOT NULL,
  amount     bigint NOT NULL,
  posted_at  timestamptz NOT NULL DEFAULT now()
);
boolean postIfNew(Payment payment) {
    try {
        jdbc.update(
            "insert into posted_payment(payment_id, account, amount) values (?, ?, ?)",
            payment.id(), payment.account(), payment.amount());
        return true;
    } catch (DuplicateKeyException alreadyPosted) {
        return true; // the earlier delivery did the work
    }
}

Commit the Kafka offset only after this returns. A duplicate key is success: the payment is on the ledger, so the offset may move. Returning false here would retry forever.

This is at-least-once delivery with an idempotent consumer. It survives a rebalance, a second pod, and a producer you restarted. It does not require a transactional Kafka producer. Use transactions when the consumer writes to another topic and that write must appear once: a consumer that updates account-balance and commits its offset in one transaction. The setting is a transactional.id unique to the instance, and a consumer with isolation.level=read_committed. The database unique key stays even then, because the transaction does not include Postgres.

The other way to get one write is the outbox: the service inserts the payment and an outbox row in one database transaction, and Debezium publishes the outbox. Use that when the service is the system of record. Use the idempotent consumer above when Kafka is already the system of record and the ledger is downstream.

Dead letters

A record with a negative amount, or a body that will not parse, will fail the same way on every retry. Retrying it blocks the partition, because order is per partition and you cannot commit past it without deciding.

apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaTopic
metadata:
  name: payments.dlt
  namespace: kafka
  labels:
    strimzi.io/cluster: lab
spec:
  partitions: 3
  replicas: 3
  config:
    retention.ms: "1209600000"
    min.insync.replicas: "2"

After a small fixed number of attempts, produce the original record to payments.dlt with headers that say why (error, original-partition, original-offset) and commit the input offset. Someone has to read the dead-letter topic. Fourteen days of retention buys that person a week of holiday. A consumer that catches every exception and commits is how payments vanish. A consumer that never catches is how one bad record stops a partition. The dead-letter topic is the third path.

What's next

  1. Publish the ledger change from an outbox with CDC and Debezium, if the database is the system of record.
  2. Describe Payment in a schema so a poison body fails at decode, in schema registry.
  3. Alert when payments.dlt receives anything, using the lag and rate alerts in monitoring.