System designLoading progress…

Outbox and sagas

Design asynchronous processing with explicit acknowledgment, ordering, replay and duplicate-handling rules.

Outbox and sagas

Saving a row and then publishing an event are two separate steps: a crash between them leaves the database and the message system disagreeing. The outbox pattern records the event in the same transaction as the data and publishes it afterwards, at-least-once. A multi-step workflow such as reserve, charge, ship can be a saga: if a step fails, the steps already done are undone by compensating actions, in reverse order. A compensation is a new action, not a perfect rewind.

1. INSERT order o1 (committed) 2. CRASH 3. publish order_created: never happens the row exists, the event does not: two systems disagree
saga steps reserve → charge → ship, failing at:
completed: reserve compensations (reverse order): undo reserve the compensating action is another step, not a perfect rewind (a refund may keep a fee, an email stays sent)
Crash between the row and the event; fail a saga step.

Predict first. Does writing a row then separately publishing an event form an atomic pair by default?

Chapter context

Asynchronous work decouples when a request is accepted from when its side effects finish. Queues and logs buffer differences in timing, but they do not remove processing cost or guarantee globally unique effects by themselves.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

A fluid approximation for backlog B is

ΔB≈(λ−μ)Δt\Delta B\approx(\lambda-\mu)\Delta t

while the queue is nonempty, where lambda is arrival rate and mu processing rate. Backlog cannot go below zero. Delivery guarantees must specify which system boundary and side effects they cover.

Worked example

Arrivals at 120 jobs/s and processing at 100 jobs/s grow the queue by 20 jobs/s, reaching 1200 after one minute. If arrivals then drop to 80 jobs/s, net drain rate is 20 jobs/s and clearing that backlog takes 60 seconds under the constant-rate model.

Reference code

The following example uses the Python standard library and does not call a remote service.

arrival,service,seconds=120,100,60
backlog=max(0,(arrival-service)*seconds)
assert backlog == 1200
new_arrival=80
drain=service-new_arrival
assert drain == 20 and backlog/drain == 60

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

Exactly-once terminology must state its scope. Retrying can duplicate external effects. A partition's order is not a universal order across an entire system.