History Limits, Continue-As-New, and Child Workflows

The Limits

LimitWarning atHard limitWhat happens at the hard limit
Events per workflow run~10,000~51,200The server terminates the workflow
Total history size10 MB50 MBThe server terminates the workflow
One payload (input, result, or signal)256 KB2 MBThe request is rejected

These are server settings, and some clusters change them. But treat them as fixed, because the reason behind them doesn't change: every workflow task replays the whole history.

A 40,000-event history means 40,000 events loaded and fed through your function every time anything happens. Raising the limit just gives you a slower, more expensive workflow.

Continue-As-New

Some workflows have no natural end: a subscription that renews every month, a workflow per device, a long polling loop. For these, you end the current run and start a new one with an empty history, in one step:

Continue-As-New, drawn as three runs left to right under one workflow ID, subscription-user-42, which stays the same throughout. Run 1 has run ID a1b2 and a history of 12,000 events. It calls ContinueAsNew, passing its state as input, and Run 2 starts with run ID c3d4 and a history back at event 1. Run 2 calls ContinueAsNew again, and Run 3 starts with run ID e5f6, again back at event 1. Each run gets a new run ID and an empty history; nothing is carried over except the input you pass.
One workflow ID, many runs — each run starts again from an empty history

Here's the part that catches people out: nothing is carried over automatically. The new run starts with an empty history and only the inputs you give it. If you still need a local variable, you have to pass it in yourself. The upside is that this makes you be clear about what state your loop really needs.

// A subscription workflow that runs "forever" — 12 months per run.
func Subscription(ctx workflow.Context, s State) error {
    for i := 0; i < 12; i++ {
        workflow.Sleep(ctx, 30*24*time.Hour)
        if err := workflow.ExecuteActivity(ctx, Charge, s).Get(ctx, nil); err != nil {
            return err
        }
        s.MonthsBilled++
    }
    // Fresh history, same workflow ID, state passed forward by hand.
    return workflow.NewContinueAsNewError(ctx, Subscription, s)
}

Child Workflows — Another Way to Split Up History

A child workflow is a separate workflow, started by a parent, with its own history. However much the child does, the parent's history only gets a few events (initiated, started, completed). That fact tells you when to use one:

Use a child workflow when…Use an activity when…
The sub-task is a long process with many steps, and needs its own timers, signals, and retriesThe sub-task is one piece of work
You're splitting work across hundreds of items, and their events would overflow the parent's historyThere are only a few items, and their results are small
You want to query, cancel, or see the sub-task in the UI on its ownNo one needs to see it separately
The sub-task should keep running after the parent ends (set the parent-close policy to ABANDON)It always ends inside the parent

Child workflows aren't free, though. Each one is a full workflow with its own shard row, its own tasks, and its own replays. For splitting up lots of small pieces of work, an activity is much cheaper. A good rule of thumb: use a child workflow when the sub-task deserves its own page in the UI.

  • Guarantees: Continue-as-new closes the old run and starts the new one in a single step, under the same workflow ID. So anyone who calls the workflow by its ID always reaches a live run.
  • Does not guarantee: That anything carries over on its own. Local variables, waiting signals, and running activities don't move to the new run. Only what you pass in as input does.