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.
3 points to say:
- At-least-once means messages can repeat; the consumer must be idempotent, not rely on the broker.
eventId+ a unique dedup table, in the same transaction as the side effect; or naturally idempotent operations (upsert, version).- 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.