System Design (High-Level Design) Module 11 – Distributed transactions and workflows
Durable execution and workflow engines
How durable execution replays an event history to survive crashes, why workflow code must be deterministic, and when an engine beats a status table.
What you will learn
- Explain how durable execution survives crashes in long workflows
- Write workflow code that respects the determinism rules of replay
- Deploy a change to workflows that are still running without breaking them
- Choose between a workflow engine and a hand-rolled state machine for a given process
Before you start
What you’ll learn · Before you start
What you will learn
- Explain how durable execution survives crashes in long workflows
- Write workflow code that respects the determinism rules of replay
- Deploy a change to workflows that are still running without breaking them
- Choose between a workflow engine and a hand-rolled state machine for a given process
Before you start
On this page
An onboarding flow waits up to 7 days for a person to review a merchant’s documents, and in that week the worker running it is restarted and redeployed many times. durable execution: Running a long, multi-step process as ordinary code that survives crashes: an engine records each step's result in an event history and, after a crash, runs the code again from the start, reusing the recorded results. runs such a long, multi-step process as ordinary code that survives crashes. The engine records the result of every step in an event history (workflow): The durable log a workflow engine keeps for one execution: its input and the result of every step that has completed, such as activity results, timers that fired and signals received.; when the process running the code dies, another one runs the same code from the start, takes each finished step’s result from the history instead of repeating the step, and carries on where the first one stopped.
Workflow engines built on this idea let a workflow wait days for a timer or a person without holding a thread. The rule that makes replay work is determinism: workflow code must make the same decisions from the same history, so clock reads, random numbers and calls to other systems go through the engine, which records their results. For a short job of two steps, a queue and a status table are cheaper.
Replay rebuilds a workflow from its event history
Replay: how a workflow carries on after a crash
Text description of the diagram
The diagram has three panels, one above the other.
- Worker 1 runs the workflow code: check documents runs and its result is recorded; wait for review receives its signal, which is recorded; create account runs and its result is recorded; then the worker crashes.
- The event history, kept by the engine, now holds three entries: documents valid, review approved, account acct-77.
- Worker 2 runs the same code from the start. Check documents, wait for review and create account take their results from the history instead of running again. Send welcome is new, so it runs and its result is recorded.
Temporal’s documentation calls the event history “the durable log behind Durable Execution”. After a failure, a fresh worker loads that log and runs the workflow code over it once more, rebuilding the state the lost worker held in memory, which only works if the code makes the same choices every time (Temporal). Work that touches the outside world, such as API calls, database queries and file access, runs in activity (workflow): A step of a workflow that touches the outside world, such as an API call or a database query. The engine records its result when it completes and reuses that result when the workflow code is replayed.: an activity’s result is recorded when it completes, and during replay the recorded result is reused, not recomputed (Temporal). Step through the diagram’s execution, one recorded result at a time:
The drawing shows the whole scene; its 7 steps are listed below.
Every step as text
- Check the documents. Worker 1 runs the workflow code. Its first activity checks the documents, and the engine records the result in the execution's history.
- Wait for a review. The code waits for the review signal. While it waits, no thread is held; when the signal arrives, the engine records it too.
- Create the account. The account is created, and its result, account acct-77, is recorded.
- The worker crashes. Worker 1 dies before the next step. Its local variables are gone; the history is not, because the engine keeps it durably.
- The same code again. Worker 2 loads the history and runs the same workflow code from its first line.
- Replayed, not repeated. Each of the first three steps asks for a result the history already holds, so worker 2 takes the recorded result instead of running the step. No document is checked twice and no second account is created.
- The next step runs. The fourth step, send welcome, has no recorded result, so it really runs and is recorded. The execution has carried on exactly where worker 1 stopped.
Three workflow engines show the range of designs:
- Temporal records an event history per workflow and replays the workflow code against it after a failure.
- Azure Durable Functions runs orchestrator functions by event sourcing and replays them, so it imposes the same determinism rules (Azure).
- AWS Step Functions describes a workflow as a state machine written in the Amazon States Language, and its Standard workflows can run for up to a year (AWS).
Histories have limits, and long workflows meet them. Temporal caps an execution’s event history at 51,200 events or 50 MB and warns at 10,240 events or 10 MB (Temporal). A workflow that wakes once a minute to poll something adds at least two events per wake-up, a timer started and a timer fired, so it reaches the cap in under 18 days: 51,200 ÷ (2 × 1,440). Long-lived workflows are therefore written to wait for events rather than poll, or to start over with a fresh history at a safe point.
| Criterion | Workflow engine | Queue and status table |
|---|---|---|
| Fits | Many steps, timers, human approvals, compensations, durations of days | One to three steps that finish in seconds or minutes |
| Where the state lives | The engine's event history | A status column that your code updates |
| Timers and waiting | Built in and recorded | A scheduled job that scans for due rows |
| What you must get right | Deterministic workflow code; versioning running workflows | Every transition, retry and stale timer by hand |
| Running cost | An engine cluster to operate, or a managed service per execution | Tables and a queue you already run |
| When to choose | Onboarding, refunds with approval, document checks, long sagas | Send a receipt, resize an image, sync one record |
Checkpoint
Check: Replay from the event history
A replay engine, run: a crash, a clock and a deploy Pro: opens with a Pro, Premium or Ultimate pass
In this part:
- The determinism rules, and how engines enforce them
- Shipping a change to workflows that are still running
- Activities run at least once
- A refund with a timer and an approval, as a state machine
- Engine or status table: choosing, and how answers are judged
- Practise: make a workflow deterministic
- Remove the three replay bugs from a refund workflow
- Interview questions
- What does a durable execution engine store, and how does it bring a workflow back after the worker running it crashes?
- Design merchant onboarding: verify documents, wait up to 7 days for a human review with reminders every 2 days, then create the account and send credentials. Would you use a workflow engine or a queue with a status table? Justify it with the failure cases.
- Right after a deploy, hundreds of running workflows fail with non-determinism errors, while new ones run fine. What happened, how do you recover the failed ones, and how should the change have been shipped?
- A workflow's "charge card" activity charged a customer twice, although the engine records every activity's result. How is that possible, and how do you prevent it?
- Key takeaways
2 diagrams · 3 runnable examples (JavaScript, Python) · 1 exercise with sample tests · 3 interview answers · about 45 minutes
Opening this part with your pass…
This part opens with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so this part can’t be opened at the moment. The free part above stands on its own. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
Terms in this lesson
- activity (workflow)
- A step of a workflow that touches the outside world, such as an API call or a database query. The engine records its result when it completes and reuses that result when the workflow code is replayed.
- deterministic workflow code
- Workflow code that makes the same calls in the same order from the same history, so a replay rebuilds the same state; reading the clock, drawing random numbers and calling other systems go through the engine.
- durable execution
- Running a long, multi-step process as ordinary code that survives crashes: an engine records each step's result in an event history and, after a crash, runs the code again from the start, reusing the recorded results.
- event history (workflow)
- The durable log a workflow engine keeps for one execution: its input and the result of every step that has completed, such as activity results, timers that fired and signals received.
References
- Temporal documentation: Workflows (Temporal Technologies)
- Temporal documentation: Workflow definition (deterministic constraints and versioning) (Temporal Technologies)
- Temporal documentation: Understanding Temporal (Temporal Technologies)
- Temporal documentation: Workflow execution limits (Temporal Technologies)
- Temporal documentation: Activity definition (Temporal Technologies)
- Temporal documentation: Retry policies (Temporal Technologies)
- Durable orchestrator code constraints (Azure Durable Functions) (Microsoft)
- What is Step Functions? (Amazon Web Services)
- Choosing workflow type in Step Functions (Amazon Web Services)
Related tools
Report a problem with this lesson
Kept only in this browser. Your Learn progress