Synthesis and Self-Test
Synthesis — How the Pieces Pay for Each Other
Determinism isn't a style rule, it's an integrity constraint on the storage format
Because the history is the only durable state (see Event history), your function is the reducer that turns it back into variables. A reducer that returns different answers for the same input is a corrupt database. That's why time.Now() is banned (see Determinism): not because clocks are impure, but because the storage layer's correctness depends on your function being a pure function of the log.
Replay multiplies workflow code, so the activity boundary is the only place effects can live
Replay (see Replay) runs your workflow body dozens of times. The workflow/activity split (see Workflows vs activities) is therefore not an aesthetic decomposition — it is the line between "code that may run N times" and "code whose result is recorded after one run." The same logic explains why heartbeat details are the only checkpoint mechanism inside an activity: an activity is never replayed, so nothing about it is reconstructable, so it must report its own progress.
The thing that makes a 30-day sleep free is the thing that makes a 10-second loop fatal
Waiting costs events, not threads (see Timers) — so duration is free and frequency is expensive. One sleep of 30 days is 2 events; 30 days of 10-second polling is ~260,000 and hits the hard history limit in under a day (see History limits and continue-as-new). Almost every "my workflow died mysteriously" story is this single trade-off read backwards.
Outward polling plus shard ownership is what decouples the two scaling problems
Workers poll out (see Workers and task queues) so the server needs no registry; every workflow has exactly one shard owner (see Inside the server) so the server needs no consensus in the hot path. The result is two independent capacity questions — the server scales on storage and shard count, your fleet scales on CPU — and two independent failure domains: a shard handover pauses workflows, a worker outage queues tasks, and neither loses anything.
The three bills for durability, in one sentence each
Determinism (see Determinism) is what you pay because state is derived by re-running code. Versioning (see Versioning) is what you pay because code changes and history doesn't. Idempotency (see At-least-once and idempotency) is what you pay because durability stops at the network boundary. A team that understands only the happy path will get all three bills anyway — on a Tuesday, in production, from a deploy that looked trivial.
Self-Test
Question 1
Trace exactly how time.Now() in workflow code turns into a production incident — and explain why workflow.Now() is fine when it returns a time too.
Show answer
The workflow's state is not stored; it is re-derived by re-running the function over the event history (see Event history, Replay). A branch on time.Now() can take the if at 16:58 and the else when replayed at 17:05, producing a command that contradicts the recorded event. The SDK detects the mismatch and fails the workflow task — the execution stays RUNNING but stops advancing, and every in-flight instance hits it (see Determinism). workflow.Now() is safe because it does not read the machine clock: it returns the recorded timestamp of the workflow task being processed, which is in history and therefore identical on every replay. The general rule: a value is legal in workflow code if and only if it is recoverable from history.
Question 2
A workflow calls workflow.Sleep(30 days). Name every resource held during those 30 days, what physically wakes it, and two independent reasons it might fire late.
Show answer
Resources held: its history rows and one timer-task row in the owning shard's timer queue. No thread, no coroutine, no worker memory — the execution is evicted from the worker cache within seconds (see Timers, Workers and task queues). What wakes it: the History service's timer-queue processor on the shard that owns hash(namespace, workflowID) % shards scans for fire times that have passed, appends TimerFired + WorkflowTaskScheduled, and Matching hands the task to whichever worker is polling (see Inside the server). Late firing: (1) the shard's timer processor is backed up or the shard is mid-ownership-handover, so the scan is behind; (2) no worker is polling that task queue, so the task is created on time but nobody executes it — which is why schedule-to-start latency, not timer accuracy, is the metric that catches this (see Workers and task queues).
Question 3
You insert one new activity call at the top of a workflow and deploy on a Tuesday. 40,000 executions are in flight. What is the blast radius, which metric moves, and how do you recover without losing any of them?
Show answer
Every in-flight execution that replays under the new binary produces a command that contradicts its history the next time it gets a workflow task. Each one fails its workflow task with a non-determinism error, stays RUNNING, and retries with backoff forever (see Determinism). Nothing is lost and nothing is corrupted — but nothing progresses either, so downstream the symptom is orders silently stalling. The metric is workflow_task_execution_failed spiking right after the deploy; execution counts and error rates in your own app stay flat, which is why this is easy to misdiagnose. Recovery: roll back the workers — the next retry replays cleanly against the old binary and everything resumes. Then ship it properly, behind a GetVersion/patched branch or on a new build ID, so old executions keep taking the old path (see Versioning). The permanent fix is a CI replay test against real downloaded histories (see Replay).
Question 4
An activity charges a card successfully, then the worker is OOM-killed before reporting completion. Trace what Temporal does, what the customer experiences, and explain why Temporal cannot fix this for you.
Show answer
The server only knows an activity started and never reported. When Start-To-Close (or Heartbeat, if set — seconds instead of the full timeout) expires, it appends the timeout event, applies the retry policy, and schedules a new attempt. The new attempt calls Stripe again and the customer is charged twice (see Workflows vs activities, At-least-once and idempotency). Temporal cannot fix it because the effect happened in a system that shares no transaction with the history: the acknowledgement crossed a network and was lost, which is the two-generals problem, not an implementation gap. The fix lives at the boundary — derive a stable idempotency key from activity.GetInfo (workflow ID + activity ID, identical across retries) and let the payment provider deduplicate. Note what is exactly-once: the workflow's decision to charge, and the result value once recorded. The gap is only the activity body.
Question 5
A workflow that ran fine for months is suddenly terminated by the server after ~17 hours. No code changed, no errors in your logs, no activity failures. What happened, and what are the three possible fixes?
Show answer
It hit the history event limit (~51,200 events). Almost certainly a polling loop: an activity plus a short timer per iteration is ~5+ events, so a 10-second loop generates ~2,900 events/hour and reaches the cap in under a day (see Timers, History limits and continue-as-new). Your logs are clean because nothing failed — the workflow was doing exactly what it was told, and the server terminated it for exceeding a structural limit. Watch for the ~10,000-event warning, which fires hours earlier. Three fixes, usually all together: (1) increase the poll interval to what the business actually needs; (2) move the polling inside one long-lived activity that heartbeats, turning thousands of events into one; (3) ContinueAsNew every N iterations to reset history — remembering to drain pending signals across the seam first (see Signals, queries and updates, History limits and continue-as-new). Note the secondary symptom that would have hinted earlier: workflow task CPU climbing steadily, because every task replays a history that keeps growing.
Question 6
Your CD pipeline restarts every worker every 10 minutes. Explain why this is harmless for correctness but expensive — and what you'd graph to prove it.
Show answer
Correctness is untouched because sticky execution is purely an optimization: any worker can rebuild any workflow from history, and a lost sticky cache just means the sticky task times out in seconds and the task falls back to the shared queue (see Replay, Workers and task queues). The cost is that every long-running workflow's next task becomes a full replay from event 1 instead of applying a handful of new events — CPU per workflow task scales with history length, so a fleet of 20,000-event workflows can burn most of its CPU re-deriving state it had in memory ten minutes ago. Graph: sticky cache hit rate falling, workflow task execution latency (not schedule-to-start) rising, and worker CPU rising with flat business throughput. This is also why the History limits and continue-as-new part's advice to keep histories short is a cost lever, not just a limit-avoidance lever.