Kayavak / The Backend / BE·51 Asynchronous Work, Jobs and Messaging / Deeper
Deeper · The Backend
Asynchronous Work, Jobs and Messaging
The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.
Why does this exist?
Some work shouldn't happen while a person waits: sending email, generating reports, notifying other systems. Message queues let a program hand work to another reliably: IBM's MQSeries (1993) and the JMS API (1998) made them standard in enterprise systems. Kafka (LinkedIn, open-sourced in 2011) changed the model to a durable, replayable log that many consumers read at their own pace, and became the backbone of event-driven systems.
Long-running business processes that span services can't use one database transaction. Hector Garcia-Molina and Kenneth Salem described sagas in 1987: a sequence of local transactions, each with a compensating action to undo it if a later step fails. And underneath it all sits an impossibility: over an unreliable network you can't guarantee a message is delivered exactly once (a form of the Two Generals problem), so systems deliver at least once and make handlers idempotent.
The mental model
Try it yourself first: write your own model in the Mental Model Explorer, then compare.
Moving work out of the request trades immediacy for reliability and decoupling. Messages will be delayed, duplicated and reordered; design every consumer so that processing a message twice is harmless.
| Question | Answer |
|---|---|
| What is it? | Running work outside the request: in-process async tasks, durable jobs, and messaging between services |
| Problem it solves | Slow or failure-prone work, spikes in load, and services reacting to each other without tight coupling |
| Parts | Executors, schedulers, job tables, queues and logs, consumer groups, the outbox, dead-letter queues, sagas |
| How it works | Producers record work durably; consumers process it, acknowledge it, and retry failures with backoff |
| Assumptions | Delivery is at least once; consumers are idempotent |
| When they change | A non-idempotent consumer double-charges or double-ships on redelivery |
| Where it fails | Dual writes (database and broker updated separately), poison messages, unbounded backlogs |
| Related ideas | Idempotency keys (ch45); retries (ch52); event-driven architecture and consistency (Distributed Systems) |
What if?
What if you saved an order and then published "order created" as two separate steps?
Reason it through, then open
If the process crashes between them, the order exists and nobody hears about it; if the publish succeeds and the database commit fails, other services react to an order that doesn't exist. The transactional outbox writes the event to a table in the same database transaction as the order, and a separate relay publishes it, so both happen or neither does.
What if a message always fails to process?
Reason it through, then open
With unlimited retries it blocks its queue or partition forever, and everything behind it waits: a poison message. After a bounded number of attempts it should move to a dead-letter queue, with an alert, where someone can inspect and replay it once the bug is fixed.
Where the boundary is
| Aspect | Queue | Log (Kafka-style) |
|---|---|---|
| A message after processing | Removed | Kept for the retention period |
| Several consumer groups | Compete for messages | Each reads every message independently |
| Replay history | No | Yes, from any offset |
| Ordering | Per queue, weakened by parallel consumers | Per partition |
How sure are we?
| Claim | How it's known | Where from |
|---|---|---|
| Exactly-once delivery over an unreliable network can't be guaranteed | Theorem | Related to the Two Generals problem; "exactly-once" products mean effectively-once processing within their system |
| Sagas were described in 1987 | Historical | Garcia-Molina and Salem, SIGMOD 1987 |
| Kafka was open-sourced by LinkedIn in 2011 | Historical | Apache Kafka's history |
Still open
Durable execution engines (Temporal and similar) that persist a workflow's progress as it runs are changing how long-running processes are written; whether they replace hand-built sagas and job tables broadly is still playing out.
What would change your mind? If your broker and database could commit atomically together, the outbox would be unnecessary. Distributed transactions across them exist but are rarely worth their cost.
Recall
Why must message consumers be idempotent?
Delivery is at least once, so the same message can be processed more than once.
What problem does the transactional outbox solve?
Dual writes: it records the event in the same transaction as the data, then publishes it separately.
What is a saga?
A sequence of local transactions with compensating actions to undo earlier steps if a later one fails.
What should happen to a message that always fails?
After bounded retries, move it to a dead-letter queue and alert.
Reflect
Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.