Sync vs Async Event Handling

Sync vs Async Event Handling

When the bus delivers an event, does the subscriber run inside the same transaction as the publisher, or after it commits? This single choice changes everything: consistency model, error handling, latency, testability.

Sync (same transaction)

The bus dispatches before publish() returns. Subscribers run in the calling thread, inside the same DB transaction.

async def place_order(self, cmd: PlaceOrderCommand) -> OrderId:
    async with self._uow:
        order = Order.new(cmd)
        await self._orders.save(order)
        for event in order.pull_events():
            self._bus.publish(event)  # subscribers run NOW, before commit

Properties:

Async (separate transaction)

The bus enqueues the event; subscribers run later, in their own context.

async def place_order(self, cmd: PlaceOrderCommand) -> OrderId:
    async with self._uow:
        order = Order.new(cmd)
        await self._orders.save(order)
        for event in order.pull_events():
            await self._outbox.add(event)  # save event to outbox in same tx

# Later, a worker reads outbox and publishes
async def dispatch_outbox():
    while events := await outbox.fetch_pending():
        for event in events:
            await bus.publish(event)
            await outbox.mark_done(event.id)

Properties:

Decision matrix

Question Sync Async
All-or-nothing semantics required? Yes No
Cross-context (different DB)? Cannot use sync Use async
Slow subscriber acceptable? No Yes
Need exactly-once? Sync handles trivially Hard; needs idempotency + dedup
Strong consistency? Sync Eventual
Decoupled deployment? No (shared process) Yes

When to use sync

When to use async

The middle ground: deferred-sync

Publish synchronously but defer dispatch until after commit:

async def place_order(self, cmd):
    async with self._uow:
        order = Order.new(cmd)
        await self._orders.save(order)
        events = order.pull_events()
        await self._uow.commit()
    # now dispatch — transaction succeeded
    for event in events:
        self._bus.publish(event)

Subscribers run sync (immediately after commit) but cannot block the transaction. Best of both: low latency, strong "transaction succeeded" guarantee, no in-transaction risk.

The catch: if the process crashes between commit and dispatch, the events are lost. Solve with the outbox pattern — write events to a table in the same transaction, then dispatch from there.

Anti-patterns

Default