Workflows vs Activities: Where to Draw the Line

The Two Halves

WorkflowActivity
What it isThe plan: the order of steps, branches, loops, waiting, error handling, and undo stepsOne piece of real work: an API call, a database write, a file conversion, a model call
Must be deterministic?YesNo. Do whatever you need.
How often it runsOnce per workflow task, so many timesOnce per attempt. The result is saved and never worked out again.
RetriesBasically never retried. A failure blocks the workflow (see Determinism).Retried automatically, using a policy you can set. This is the main reason activities exist.
TimeoutsAn optional timeout for the whole runFour separate ones. Getting them wrong is the most common production bug.
StateLocal variables, kept safe by replayNo 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 workflow / activity split. Left, the workflow function: deterministic, replayed, no I/O; it does the orchestration, sequencing, branching, waiting and compensation, and runs once per workflow task, so many times. Middle, three activities it schedules: ChargeCard, which calls Stripe; ReserveInventory, which writes Postgres; and SendReceipt, which calls SendGrid. Each activity runs once per attempt, its result is recorded and never recomputed, and it is retried automatically. Right, the outside world: Stripe, Postgres and SendGrid. Only activities touch the outside world. The test: if this line ran ten times, would anything be wrong? If yes, it belongs in an activity.
The workflow decides; the activities act — and only activities touch the outside world

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.

A timeline of one activity with two attempts, and the span each of the four timeouts guards. The activity is scheduled and waits in the task queue; Schedule-To-Start guards that wait, and catches no worker polling or a backed-up queue. Attempt 1 starts; Start-To-Close guards one attempt. Attempt 1 heartbeats three times, then its worker dies silently; once no heartbeat arrives within the heartbeat timeout, the attempt is killed and retried. Without a heartbeat, the dead worker would only be noticed when Start-To-Close expires, much later. After a backoff and a wait in the queue, attempt 2 runs and completes. Schedule-To-Close spans everything from first scheduling to completion, retries included: the business deadline.
Which span each timeout actually guards Set Start-To-Close (or Schedule-To-Close; you must set one of them). Add a heartbeat for anything long. Schedule-To-Start fires when you don't have enough workers, and the fix for that is more workers, not a timeout.
TimeoutWhat it catchesWhat to do
Start-To-CloseOne attempt that hangsSet this one. Make it the p99 time of one attempt, plus some room. You must set either this or Schedule-To-Close.
Schedule-To-CloseThe whole activity, retries included, going past a business deadlineSet it when there's a real deadline, like "charge within 1 hour or give up."
Schedule-To-StartNo worker is picking up tasks, or the queue is badly backed upUsually 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.
HeartbeatA long activity whose process died without a wordA 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 interval1 second
Backoff coefficient2.0
Maximum interval100× initial
Maximum attemptsUnlimited

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.