LAB 06 · KAFKA / WAREHOUSE
The consumer recovered.
The warehouse counted the same events twice.
A billing dashboard jumps 18% after a consumer restart even though the producer volume is unchanged.
The consumer rebalances cleanly and offsets later advance. Delivery has not necessarily been exactly once at the warehouse.
Prove whether duplication entered in Kafka, or at the hand-off to the warehouse.
CONSUMER EXCERPT
Relevant write boundary only
for message in consumer:
event = json.loads(message.value())
warehouse.execute(
"INSERT INTO billing_events "
"(event_id, account_id, amount, occurred_at) "
"VALUES (:event_id, :account_id, :amount, :occurred_at)",
event,
)
consumer.commit(message)
The offset is committed after the warehouse write. That ordering protects against loss, but it does not itself make the side effect idempotent.
EVIDENCE TRAIL
Open the signals in order
01Source versus sinkDid messages multiply before the warehouse?
Kafka records, partition 7: 84,001
Unique event_id in warehouse: 84,001
Warehouse rows: 99,220
Rows in replayed window: 15,219The records and business keys did not multiply in Kafka. Stored occurrences did multiply in the warehouse.
02Restart timelineWhat happened between the write and the offset?
02:13:06 warehouse batch committed
02:13:07 worker terminated before offset commit
02:13:18 partition 7 reassigned at offset 420800
02:13:31 same event_id values inserted againReplaying messages after a crash in this gap is expected for an at-least-once consumer. The question is whether the sink can safely accept that replay.
03Warehouse constraintWhat stops a duplicate fact from being stored?
billing_events unique index on event_id: absent
warehouse write mode: INSERT
dedupe staging table: disabledThe warehouse has no uniqueness boundary for the business event. Each valid replay becomes another billing row.
LIKELY ROOT CAUSEAt-least-once delivery met a non-idempotent warehouse write; a valid replay became duplicate facts.
The application commits Kafka offsets only after writing. Crashing in that gap produces a normal replay. Because the warehouse accepts every INSERT, the recovery window becomes duplicate data.
INSERT INTO billing_events (...)MERGE INTO billing_events USING staged_events ON target.event_id = source.event_idREVIEW BEFORE APPLYINGDo not mistake a Kafka “exactly once” setting for end-to-end warehouse correctness. Define the business idempotency key, enforce it in the sink, and decide how corrections and upserts behave.