Tech challenge · Kafka

Duplicates in the warehouse after a consumer restart

HardKafka

A consumer reads payments from Kafka and inserts them into a warehouse table. After each deployment the table has a few hundred duplicate payments. Offsets are auto-committed every 5 seconds. Explain where the duplicates come from and how you would get effectively-once results.

Think it through or sketch an answer first, then open the hint or the solution.

Show a hint

What is written between the last offset commit and the restart?

Show the solution

Records processed after the last commit are read again when the consumer restarts, so they are written twice: that is at-least-once delivery. Commit offsets only after the sink write succeeds, and make the sink idempotent: upsert or MERGE on payment_id, or store the offsets in the same transaction as the data. Kafka transactions give exactly-once for Kafka-to-Kafka processing, but an external database still needs idempotent writes.

Common mistakes

  • Committing offsets before writing, which turns duplicates into lost data.
  • Believing an exactly-once setting removes the need for an idempotent sink in an external system.

Learn more

More Kafka challenges

Today's challenge and the full archive