SteelReed

Search

Search posts, questions…

Messaging & KafkaQuestion 18 / 21

Middle

How do you design an idempotent Kafka consumer, and how long should you keep the processed-message table?

Short answer

Kafka delivers at-least-once, so a consumer must give the same result even on duplicates: store the eventId in a dedup table with a unique key in the same transaction as the processing; skip it if it is already there.

unique key violationinsert succeedsReceive event + eventIdOpen DB transactionINSERTprocessed_messageSkip, commit offsetRun business logicCommit DB, then offset

3 points to say:

  1. At-least-once means messages can repeat; the consumer must be idempotent, not rely on the broker.
  2. eventId + a unique dedup table, in the same transaction as the side effect; or naturally idempotent operations (upsert, version).
  3. For calls to external systems, send an idempotency key; keep the dedup table ≥ the topic's retention.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.