A Kafka consumer loading batches into the warehouse loses rows after a crash — find and fix the bug
The loader below reads the orders topic and inserts each batch into raw.orders in the warehouse. After every crash of the job (OOM, node restart) the daily order count in the warehouse comes out lower than in the source system. The insert can take 30 seconds or more on a busy warehouse.
Constraints: no order may be lost; a crash at any line must be survivable; keep the batch size.
from confluent_kafka import Consumer
consumer = Consumer({
"bootstrap.servers": "kafka:9092",
"group.id": "orders-loader",
# enable.auto.commit is not set
})
consumer.subscribe(["orders"])
while True:
msgs = consumer.consume(num_messages=5000, timeout=1.0)
if not msgs:
continue
consumer.commit(asynchronous=False) # record progress first
rows = [parse(m) for m in msgs]
warehouse.insert("raw.orders", rows) # 30+ s on a busy warehouseFind and fix the bug, and say what the fix does to rows after a crash.
Commit-then-insert loses the batch if the job dies in between; insert-then-commit re-inserts it after a crash. enable.auto.commit (default true, every 5 s) commits on a timer, not after the insert — disable it, commit after the insert, make the insert repeat-safe.
- Believing
enable.auto.commitwaits until the sink has stored the batch before it commits - Treating write-then-commit as exactly-once and skipping a repeat-safe insert
- Committing first to save progress, which turns every crash into silently missing rows
- Which column would you use as the unique key to make the warehouse insert safe to repeat?
- How does a consumer group rebalance in the middle of a long insert change this picture?
The bug is committing the offset before the insert. If the job dies between commit() and warehouse.insert(), the group has already recorded progress for a batch the warehouse never got: after the restart reading resumes at the next offset and 5,000 orders are gone. The second trap is that enable.auto.commit is unset and defaults to true (auto.commit.interval.ms = 5000): the client commits its stored offsets on a timer, and with enable.auto.offset.store (also true by default) an offset is stored as soon as the message is handed to the application — whether or not the insert finished. So moving commit() down is not enough on its own — auto-commit can still commit the batch in the middle of a 30-second insert.
from confluent_kafka import Consumer
consumer = Consumer({
"bootstrap.servers": "kafka:9092",
"group.id": "orders-loader",
"enable.auto.commit": False, # we record progress ourselves
})
consumer.subscribe(["orders"])
while True:
msgs = consumer.consume(num_messages=5000, timeout=1.0)
if not msgs:
continue
rows = [parse(m) for m in msgs]
# repeat-safe insert: ON CONFLICT / MERGE on order_id
# or on (topic, partition, offset)
warehouse.upsert("raw.orders", rows, key=["order_id"])
consumer.commit(asynchronous=False) # only after the insertNow a crash before the commit re-reads the batch instead of losing it: the load becomes at-least-once. Inserting the same batch again does not double the orders, because the sink rejects or overwrites a row whose key it already has.