Workflows vs Activities: Where to Draw the Line
The Two Halves
| Workflow | Activity | |
|---|---|---|
| What it is | The plan: the order of steps, branches, loops, waiting, error handling, and undo steps | One piece of real work: an API call, a database write, a file conversion, a model call |
| Must be deterministic? | Yes | No. Do whatever you need. |
| How often it runs | Once per workflow task, so many times | Once per attempt. The result is saved and never worked out again. |
| Retries | Basically never retried. A failure blocks the workflow (see Determinism). | Retried automatically, using a policy you can set. This is the main reason activities exist. |
| Timeouts | An optional timeout for the whole run | Four separate ones. Getting them wrong is the most common production bug. |
| State | Local variables, kept safe by replay | No state carried between attempts, except heartbeat details |
When you're not sure, ask: "If this line ran ten times, would anything go wrong?" If yes, put it in an activity. If it only makes decisions using values you already have, put it in the workflow.
The Four Activity Timeouts
Each activity attempt goes through three stages: scheduled → started → closed. Each stretch has its own timeout. People often set one, leave the rest at their defaults, and are then surprised by how things fail.
| Timeout | What it catches | What to do |
|---|---|---|
| Start-To-Close | One attempt that hangs | Set this one. Make it the p99 time of one attempt, plus some room. You must set either this or Schedule-To-Close. |
| Schedule-To-Close | The whole activity, retries included, going past a business deadline | Set it when there's a real deadline, like "charge within 1 hour or give up." |
| Schedule-To-Start | No worker is picking up tasks, or the queue is badly backed up | Usually leave it unset. It fires when you don't have enough workers. Failing work for that reason is rarely what you want. Add workers instead. |
| Heartbeat | A long activity whose process died without a word | A must for anything longer than about a minute. Without it, a dead worker is only noticed when Start-To-Close runs out. For a 6-hour activity, that's a 6-hour gap. |
Retries: the Defaults, and When They're Wrong
| Initial interval | 1 second |
|---|---|
| Backoff coefficient | 2.0 |
| Maximum interval | 100× initial |
| Maximum attempts | Unlimited |
By default, Temporal retries forever. That's surprising, but it's on purpose. If a service you depend on goes down, your business process should pause, not be thrown away.
The downside: an input that can never work will be retried forever too. A badly formed card number gets retried every 100 seconds until someone notices. You have two ways to stop this:
- Non-retryable error types. In the retry policy, list the errors that are pointless to retry, such as validation errors or 4xx responses. These fail the activity right away. The workflow gets a normal error it can handle.
- A Schedule-To-Close limit for anything with a business deadline.
- Guarantees: Once an activity's result is in the history, replay never runs it again. A failed activity is retried according to its policy, even across worker restarts, without you storing any state.
- Does not guarantee: That the code inside an activity runs only once. If an activity finishes but the result never reaches the server, it will be retried and run again. See At-least-once and idempotency.