Event Streaming LabPython · Bytewax

Chapter 34 · Reading

Connect transactions to streams

Follow an order from a database transaction to the broker and the consumer’s own database.

Prerequisites Replay without double counting, Recover after a failure and Exactly once—at which boundary? Find chapters in the course contents.

What you’ll learn

Explain what each database transaction saves and what can be retried when a process fails between writes.

The situation

Checkout commits an order row, then publishes OrderPlaced. A crash between those actions leaves a real order without an event. Publishing first risks an event for a transaction that later rolls back. Retrying the two independent writes does not create an atomic boundary between them.

Put the intended event in the business transaction

An outbox writes the domain change and an identified outbound event row in the same database transaction. Either both commit or neither does. A separate relay or change-data-capture connector publishes committed outbox records. A crash after publishing but before recording relay progress can still produce the event again, so consumers need stable event identity.

The outbox record should capture domain meaning and ordering scope: aggregate/entity ID, event ID, version/type and payload contract. Raw table changes are not always suitable public domain events. Avoid making every downstream consumer reconstruct business intent from a collection of undocumented row-level changes. The source database remains the authority for the committed transaction.

Read changes from the database log

Change data capture (CDC) reads a database’s log to publish changes. It can start with a consistent snapshot and then continue from the matching log position. Its positions can be log sequence numbers, transaction coordinates or another connector-specific token. Those positions are not Kafka offsets, application event IDs, event-time watermarks or engine checkpoints, although a complete pipeline must coordinate the relevant boundaries.

Plan snapshot consistency, retention of the database log, schema changes, deletes, restart behavior and source permissions. An outbox relay can simplify the public event contract, but it still needs monitoring and cleanup. Removing outbox rows before the relay can safely recover can lose delivery; retaining them forever creates another state-growth problem.

Worked case

This worked case uses an example written for the course.

Failure cutWithout coordinationPattern contributionRemaining requirement
DB commit before publishCommitted order, missing eventOutbox stores event with orderRelay must recover and retain data
Publish before relay progressPossible duplicate eventStable outbox event IDConsumer duplicate policy
Local sink write before progressPossible repeated contributionTransactional inbox + local changeRetention and identity scope
Remote effect before timeoutUnknown external outcomeService idempotency/reconciliationDo not infer failure means no effect

An inbox coordinates a consumer’s local database effect

A consumer can insert an event ID into a unique inbox table and apply its local business change in one transaction. A repeated ID then becomes a recognizable already-applied event rather than another contribution. The uniqueness scope and retention horizon are part of correctness; deleting the inbox identity too early may reopen the duplicate window.

This local transaction does not automatically include an external payment, email or third-party API. A timeout can follow a completed remote effect. Such effects need a service-specific idempotency key, transactional integration when actually supported, or durable reconciliation of unknown outcomes. Engine recovery and broker delivery remain separate from that remote contract.

Test the gaps you claim to close

Write a failure matrix before an end-to-end claim: before source commit, after commit before relay publication, after publication before relay progress, after sink effect before consumer progress and during replay after identity cleanup. Verify logical IDs and values, not just final row count. Preserve transaction/order metadata needed by the actual domain.

Use separate metrics for source-transaction age, relay lag, broker positions, processing lag and effect reconciliation. A fast stream processor cannot repair a missing source publication without another detection/rebuild mechanism. The browser course supplies identity, protocol and process-recovery mechanics; it does not run a database CDC connector or verify an outbox transaction here.

Think it through

A team adds an outbox and declares that payment charging is now exactly once. Which failure case disproves that conclusion, and what contract would you request next?

Show hints
  1. Hint 1

    The outbox coordinates the source transaction, not an arbitrary remote effect.

  2. Hint 2

    The relay may publish an identified event more than once.

  3. Hint 3

    A timed-out charge may already have succeeded.

Show explanation

A charge can succeed before the consumer receives its response or commits progress. Replay can then request it again. Ask for the payment service’s stable idempotency-key and receipt/reconciliation contract, and test that actual boundary. The outbox addresses the source database/publication gap; it does not automatically transact with the payment service.

How frameworks implement this

Debezium documents connector-specific snapshots, log positions and outbox routing. Kafka transactions have a defined Kafka-side scope. Flink/Bytewax/Spark recovery mechanisms must still be connected to a real sink contract. This chapter is an authored design analysis, not an executed database or distributed transaction test.

ReferencesPrimary sources for this chapter’s design case

Use these guides to check the framework or protocol contract discussed in this chapter. Moving “stable” or “current” guides can describe a different release; check the version used by your deployment. These readings do not execute the distributed frameworks they compare.

This mark tracks what you’ve read.