Outbox cleanup — pick your poison
Three cleanup strategies trade WAL noise, race risk, and operational complexity; INSERT-then-DELETE in the same transaction collapses cleanup into the write path.
The outbox closes the dual-write gap — but every business write now also writes a row whose entire purpose is to be deleted, so the table will grow forever unless we pick a cleanup strategy. Three strategies are on the table; each loses something different.
Scene 09
Outbox cleanup — pick your poison
- Watch
- Try it
- Predict
- Capture
Three lanes, one workload. Watch each lane age across 30 days — the outbox row count, the op=d noise in the WAL, and the Kafka topic size diverge. Lane 1 stays small but pumps op=d events; lane 2 stays small via tombstones + compaction; lane 3 climbs daily and drops at month-end.
Highlighted lines are the ones running in the diagram right now.
def hardDeleteAfterEmit(row):kafka.produce(topic = route(row),key = row.aggregate_id,value = row.payload,)# race: if the consumer lags, op=d may arrive# before the consumer has read op=cdb.execute('DELETE FROM outbox WHERE id = %s',row.id,)# WAL now records op=d; Outbox SMT must filter
# topic configured cleanup.policy=compactdef tombstoneAndCompact(key):kafka.produce(topic = outbox_topic,key = key,value = None, # tombstone)# log compaction will eventually drop prior# values for this key — per-key 'keep latest'# race: lagging consumer may miss the value# before compaction reclaims it
def insertDeleteSameTx(event):db.execute('BEGIN')db.execute('INSERT INTO outbox(...) VALUES (...)',event,)db.execute('DELETE FROM outbox WHERE id = %s',event.id,)db.execute('COMMIT')# WAL has both records; Debezium emits op=c# then op=d atomically — sink ignores op=d
Where this sits in Build a CDC pipeline (Debezium + outbox)
Scene 09 of 12. Hard-delete after emit, tombstone + log compaction, partition-by-date drop — three trade-offs across WAL noise, race risk, and operational complexity. INSERT+DELETE same-tx collapses cleanup into the write.
Up next. Cleanup is sorted; the outbox emits one event per business action — but those events are about to be sharded across Kafka partitions, and the question of which events stay in order matters as soon as there is more than one.
All 12 scenes in Build a CDC pipeline (Debezium + outbox) · Every curriculum