outbox-demo · sequence diagram

Scenario B · Relay crash

The relay publishes and crashes before marking SENT. The next run publishes the same event again, and the consumer skips the duplicate.

1 transaction 1 transaction alt · duplicate Mainscenarios OrderServicecreateOrder() OutboxRelaypublishPendingEvents() FakeDatabasesimulated DB FakeKafkaorders.events PaymentConsumerhandle(offset) createOrder("Bob") INSERT orders + outbox (NEW) COMMIT orderId Relay run 1: publishes, then crashes setSimulateCrashAfterPublish(true) publishPendingEvents() SELECT pending 1 event (NEW) UPDATE status=PROCESSING send(key=orderId, eventId) offset 1 CRASH before marking SENT RuntimeException: Simulated relay crash the event stays PROCESSING Relay run 2: publishes again setSimulateCrashAfterPublish(false) publishPendingEvents() SELECT pending 1 event (PROCESSING) UPDATE status=PROCESSING send(key=orderId, eventId) offset 2 (duplicate) UPDATE status=SENT The consumer processes offset 1 handle(1) read(1) record(key, eventId) BEGIN TRANSACTION INSERT processed_events true (new) INSERT payments (PENDING) COMMIT commit offset 1 The consumer gets the duplicate (offset 2) handle(2) read(2) same eventId BEGIN TRANSACTION INSERT processed_events false: duplicate key ROLLBACK commit offset 2 SKIP: no payment created Result: 1 payment for Bob; the duplicate was skipped
Solid arrows: calls. Dashed: replies. Red: errors and crashes. Dashed blue boxes: database transactions; amber: the duplicate branch.

What to look for