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:
- All-or-nothing: if any subscriber throws, the transaction rolls back.
- Latency: publisher waits for all subscribers.
- Consistency: strong (full ACID).
- Failure mode: one slow subscriber slows everyone.
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:
- Eventual consistency: subscribers might lag.
- Latency: publisher returns immediately.
- Independent failure: one subscriber failure doesn't block others.
- Requires idempotency: subscribers may see the same event twice.
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
- Domain events within one aggregate's transaction that need to fire only if the transaction commits. (But then you might just put the logic on the aggregate.)
- Within-context derivations that must be consistent (counter, denormalised projection).
- Validation reactions that can abort the transaction. ("If an order fails fraud check, reject it.")
When to use async
- Cross-context (integration events).
- Slow side-effects (email, third-party API, cache warming).
- Independent failure required (notification failure shouldn't roll back the order).
- Throughput: the publisher's request path shouldn't wait for all subscribers.
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
- Sync everywhere by default, leading to slow request paths and brittle transactions.
- Async everywhere by default, leading to debugging nightmares for what should be a same-tx update.
- Subscribers that must be sync but aren't. Counter goes stale, denormalised view is wrong for seconds, support gets confused.
- Subscribers that must be async but aren't. Email send blocks the order POST. Customer waits 8 seconds.
Default
- In-process domain events -> deferred-sync (publish after commit, in-process dispatch).
- Cross-context integration events -> async, broker-backed, with outbox.