Durable timer

On this page 7

A durable timer is a wait inside a workflow whose deadline is stored persistently by the orchestration engine rather than held in process memory, so the wait survives crashes, restarts and redeployments and resumes the workflow when the time arrives. In the reliability of automation, durable timers make it safe to write logic such as “wait 30 days, then send a renewal notice” directly in workflow code without a cron job or a polling loop.

Durable timers are a core primitive of durable execution platforms such as Temporal, Cadence, Azure Durable Functions, AWS Step Functions and Cloudflare Workflows. While a timer is pending, the workflow consumes no worker thread, memory or compute, which is why a single cluster can hold millions of sleeping workflows.

How a durable timer is recorded and fired

A durable timer works by turning a sleep call into an event in the workflow history and a row in the engine’s timer store. When workflow code calls an API such as workflow.sleep() or context.CreateTimer(), the engine records a timer-started event with the absolute fire time, and the worker then releases the workflow from memory.

A timer service inside the engine scans persisted deadlines, usually from an index ordered by fire time. When a deadline passes, it appends a timer-fired event to the history and schedules a new workflow task. A worker picks up that task, rebuilds the workflow state by replaying the history, sees that the timer has fired and continues with the next line of code.

The following table summarizes the stages of a single timer.

StageWhat the engine storesWorker activity
CreationTimer identifier and absolute fire timeWorkflow code reaches the wait call
PendingEntry in the persistent timer indexNone; workflow is evicted from memory
FiringTimer-fired event in the historyNone until a task is dispatched
ResumptionNew workflow task in the task queueWorker replays history and continues
CancellationTimer-canceled event in the historyCode takes an alternative branch
Lifecycle of a durable timer

Because the deadline is stored as an absolute timestamp, a restart of the whole cluster does not reset the countdown. If the engine was down when the deadline passed, the timer fires as soon as the engine recovers, so the wait is guaranteed to end, though possibly late.

Durable timers versus in-memory waits and schedulers

A durable timer differs from an ordinary setTimeout or Thread.sleep in where the waiting state lives. An in-memory wait disappears with the process, while a durable timer belongs to the workflow instance and is protected by the engine’s database.

MechanismSurvives restartHolds resources while waitingTypical duration
In-memory sleepNoThread or event loop slotMilliseconds to minutes
Delayed queue messageYes, if the broker persists itBroker storage onlySeconds to days, broker-dependent
Cron or scheduled jobYesScheduler entryRecurring, not per instance
Database pollingYesPeriodic queriesAny, with polling delay
Durable timerYesHistory and timer index onlySeconds to months or years
Ways to make an automated process wait

The decisive advantage is per-instance context. A cron job must rediscover which records are due, whereas a durable timer already knows which order, customer or contract it belongs to, and the code after the wait has all local variables restored by replay.

Common patterns built on durable timers

Most reliability patterns in long-running automation combine a durable timer with an external event. The workflow waits for whichever comes first, which turns a timer into a deadline or timeout for human approval, payment confirmation or a partner callback.

PatternHow the timer is usedExample
Timeout raceTimer competes with an incoming signalCancel an order if unpaid after 24 hours
Reminder and escalationSeries of timers between notificationsRemind an approver after 2 days, escalate after 5
Delayed retryTimer between attempts of a business stepRetry a failed shipment booking after 1 hour
Grace periodTimer before an irreversible actionDelete an account 30 days after closure
Polling with sleepTimer between status checksCheck a batch export every 10 minutes
Recurring loopTimer at the end of each iterationMonthly billing cycle per subscription
Typical patterns and their purpose

In a timeout race, the losing branch must be handled explicitly. When the signal arrives first, good practice is to cancel the pending timer so the history stays short and no stale task is scheduled later.

Precision, determinism and engine limits

Durable timers guarantee that a workflow will not resume early, but they do not guarantee sub-second precision. Firing delay depends on the timer scan interval, task queue backlog and worker availability; in a healthy cluster it is usually well under a few seconds, while an overloaded system can add minutes.

Workflow code must obtain time only through the engine’s deterministic clock, for example workflow.now() or context.CurrentUtcDateTime. Reading the system clock directly produces a different value during replay and can cause non-determinism errors or timers computed from the wrong base time.

  • Some engines historically capped a single timer and chained shorter timers internally for longer waits.
  • Very long loops of timers grow the history and should be reset with a continue-as-new operation.
  • Changing a timer duration in deployed code requires workflow versioning, because running instances replay the old history.
  • Timer events count toward history size and billing on managed platforms.

Mistakes that weaken timer reliability

Most failures attributed to durable timers come from workflow code that breaks the replay contract or from assumptions about exact timing. The table lists frequent problems and remedies.

SymptomCauseFix
Replay fails after deployTimer duration changed without versioningAdd a version branch or patch marker
Timer fires at an odd hourLocal time or daylight saving not handledCompute deadlines in UTC with explicit zones
Workflow never resumesWorker for the task queue is not runningMonitor task queue backlog and worker count
History limit reachedEndless loop of timers in one runUse continue-as-new at regular intervals
Action runs twice after timeoutSide effect not idempotentMake downstream activities idempotent
Timer mistakes, causes and fixes

A durable timer only protects the wait itself. The work that follows the timer still needs idempotent activities, a sound retry policy and monitoring, because an activity can fail or run more than once after the workflow resumes.

In everyday language

A durable timer is like writing a deadline in a shared paper calendar instead of trying to remember it. If the person who wrote it goes on vacation, gets sick or changes desks, the date is still in the calendar. When the day comes, whoever is on duty sees the note and carries on.

For software, this means a process can wait for hours, weeks or months without keeping a computer busy. Nothing is forgotten when servers restart, and the waiting step picks up exactly where it left off.

Seen in the real world

An insurance company automates policy renewals with a durable execution engine. Each policy has its own workflow that sends an offer 45 days before expiry, then waits on a durable timer for either the customer’s acceptance signal or a 14-day deadline.

If the customer does not respond, a second timer sends a reminder, and a third timer escalates the case to an agent seven days before expiry. During a planned database migration the cluster was offline for two hours; timers that came due during the outage fired within minutes of recovery, and no renewal notice was lost or sent twice because the notification activities checked a message identifier before sending.

With roughly 400,000 active policies, almost all workflows sit in the pending timer state at any moment, using storage rather than compute.