Design a Workflow Engine
Understanding the Problem
🔀 What is a workflow engine? A workflow engine runs processes made of many steps, where each step might call a service, wait for a timer, or wait for a person. "When an expense is submitted, ask the manager to approve; if it's over $5,000, also ask finance; then pay it and email the receipt." The engine remembers where every run is, retries steps that fail, and picks up exactly where it left off after a crash, even if the run lasts a month.
This question comes in a few flavors that turn out to be one problem. Temporal or Cadence let developers write a workflow as ordinary code and guarantee it runs to completion. Zapier and IFTTT let non-developers wire a trigger ("new row in Google Sheets") to actions ("post in Slack, create a Trello card"). Approval workflows route a document through people. Under the hood, all three are a durable state machine per run, advanced by events: a step finished, a timer fired, a webhook arrived, a manager clicked "Approve." We'll build that core and cover each flavor along the way.
Functional Requirements
Core Requirements
- Users should be able to define a workflow (steps, branches, parallel steps, retries) and start runs of it, from an API call, a schedule, or an external trigger.
- The system should execute each run's steps in order, retrying failed steps, and continue correctly after any crash.
- Workflows should be able to wait: for a timer ("wait 3 days"), for an external event, or for a human approval.
- Users should be able to see the status and full history of a run, and cancel it.
Below the line (out of scope)
- The visual editor for building workflows.
- Hosting the code that steps run (developers run their own workers; Zapier-style integrations are built-in connectors).
- Billing per task.
Non-Functional Requirements
We'll design for a large multi-tenant service: 20 million workflow runs started per day, averaging 8 steps each. Most runs finish in seconds, but some wait for people and stay open for weeks.
Core Requirements
- Durability: once a run has started, its progress is never lost. A crash at any moment resumes from the last completed step.
- Correct step semantics: each step runs at least once, and the engine never records a step's result twice. Combined with idempotent steps, that gives exactly-once effects.
- Low overhead: moving from one step to the next should add tens of milliseconds, not seconds.
- Scale: tens of millions of open runs at once, most of them idle and waiting.
- Isolation: one tenant's burst or a third-party outage shouldn't slow down other tenants.
Below the line (out of scope)
- Hard real-time guarantees. A timer that fires 1 second late is fine.
- Cross-region active-active execution of a single run.
Back-of-the-envelope. 20M runs × 8 steps = 160M step executions per day, ≈ 1,850 per second on average and perhaps 10,000 per second at peak. Each step produces about three state changes (scheduled, started, completed), so the engine does around 30,000 durable writes per second at peak. At roughly 1 KB per event, that's 160M × 3 × 1 KB ≈ 480 GB of history per day.
The other number is open runs. If 5% of runs wait on a person for an average of 3 days, that's 1M new waiting runs a day × 3 days ≈ 3M runs waiting at any moment, plus everything sleeping on timers. Most of the engine's "work" is remembering, not computing.
The key insight: a waiting workflow must cost nothing but storage. If your design keeps a thread, a process or a connection per open run, it falls over at a few hundred thousand runs. Store the state, release the resources, and wake the run up when an event arrives.