Timers: Why a 30-Day Sleep Costs Nothing
The Sequence
Two things in that picture make this work:
- The timer is a row in a queue sorted by time. It is not a callback waiting in memory. Each history shard (see Inside the server) has a timer queue, sorted by wake-up time. A background process keeps asking "is anything due?" So a billion waiting timers cost a billion rows and an index scan. They take up no memory.
- The workflow is removed from memory between tasks. Workers keep recently active workflows in a cache, so they don't have to replay them every time (see Workers and task queues). But a workflow that will do nothing for 30 days is dropped from the cache within seconds. When the timer fires, a worker rebuilds it by replay. So "how many open workflows can I have?" is a question about storage, not memory.
cost of a sleeping workflow ≈ its history rows + one timer-task row
Everything in Temporal that waits uses this same timer: activity timeouts, workflow timeouts, and the pause between retries. There is only one way to wait in Temporal, and this is it.
- Guarantees: A timer fires once, at or after its deadline. It survives any number of restarts of the workers and the server. It uses no memory or threads while it waits.
- Does not guarantee: That the timer fires on time. Or that waiting is free if you do it over and over: each wait adds history events, and history has a size limit (see History limits and continue-as-new).