Temporal: A Durable Execution Workflow Engine
Temporal is the archetypal workflow engine, and understanding it also deepens your understanding of "resuming from checkpoints".
Core concepts
| Concept | Description |
|---|---|
| Workflow | The orchestration logic (written as ordinary code); must be deterministic |
| Activity | Concrete work with side effects: calling an LLM, reading/writing the database, calling external APIs. Can fail and be retried |
| Worker | The process you deploy that runs Workflow and Activity code |
| Temporal Service | Stores each Workflow's event history and handles scheduling, timers and retries |
| Signal / Query / Update | Send a message to / query the state of / send and wait for a result from a running Workflow |
| Retry Policy | Activities have a retry policy by default; Workflows are not retried by default |
How it works: durable execution and replay
- Temporal records every event during a Workflow's execution (Activity scheduled, completed and its result, Signal received, timer fired…).
- After a Worker crashes, a new Worker replays the event history to rebuild the Workflow's in-memory state. Activities that already completed are not re-executed; their results are taken straight from the history.
- So Workflow code must be deterministic: given the same history, replay must make the same decisions. You can't use the current time, random numbers, direct network calls and so on. The Python SDK offers replacement APIs such as
workflow.now()and uses a sandbox to detect non-deterministic behavior.
Default retry policy (official docs)
| Parameter | Default |
|---|---|
| Initial Interval | 1 second |
| Backoff Coefficient | 2.0 |
| Maximum Interval | 100× the initial interval (i.e. 100 seconds) |
| Maximum Attempts | 0 (unlimited) |
| Non-Retryable Errors | None |
So always set timeouts on Activities (e.g. start_to_close_timeout) and declare non-retryable error types, or they will retry forever.
In practice: waiting for human review with a Signal (Python SDK)
python
from datetime import timedelta
from temporalio import workflow
from temporalio.common import RetryPolicy
with workflow.unsafe.imports_passed_through():
from app.activities import parse_doc, extract_fields, deliver
@workflow.defn
class ContractReview:
def __init__(self):
self.review: dict | None = None
@workflow.signal
def submit_review(self, review: dict):
self.review = review # Signals are delivered at least once: handling must be idempotent
@workflow.run
async def run(self, doc_id: str) -> dict:
opts = dict(start_to_close_timeout=timedelta(minutes=2),
retry_policy=RetryPolicy(maximum_attempts=5, non_retryable_error_types=["BadInput"]))
text = await workflow.execute_activity(parse_doc, doc_id, **opts)
fields = await workflow.execute_activity(extract_fields, text, **opts)
if fields["confidence"] < 0.8:
await workflow.wait_condition(lambda: self.review is not None, timeout=timedelta(days=3))
fields = self.review or fields
await workflow.execute_activity(deliver, fields, **opts)
return fieldsOnce the reviewer submits in the frontend, the backend calls handle.signal(ContractReview.submit_review, data) and the flow continues, even if it waited three days and the Worker restarted in the meantime.
Common pitfalls
- Calling an LLM / the database directly in a Workflow: breaks determinism. It must go in an Activity.
- Activities without timeouts: combined with the unlimited default retries, problems stay hidden indefinitely.
- Changing the code of running Workflows: causes replay mismatches. Use the versioning (patching) mechanism.
- Event history too large: Workflows that loop for a long time should use Continue-As-New to reset the history.
Common interview questions
- How do you split Workflows and Activities? Why must Workflows be deterministic?
- After a Worker crashes, how does Temporal resume execution? Are completed Activities re-run?
- What is the default Activity retry policy? What are the risks?
- How does Temporal handle waiting days for a human review? How would a home-grown solution do it?
- How does Temporal differ from Celery, Airflow and Dify/n8n?
How it relates to my projects
- AI Document Review Pipeline W5: orchestration migrated to Temporal, with Signals for human review.
To be verified / may go stale
- The example is written against the public API of the Temporal Python SDK; for details such as the
timeoutparameter ofwait_condition, follow the current SDK docs.
Sources
- Temporal Docs: Retry Policies — defaults (1s, 2.0, 100×, unlimited); Activities retry by default, Workflows don't
- Temporal Docs: Workflow Definition (determinism constraints)
- Temporal Docs: Python SDK Workflow basics — deterministic APIs such as
workflow.now() - Temporal Docs: Python SDK sandbox
- Temporal Docs: Python Message passing (Signals / Queries / Updates)
- Temporal Python SDK API: RetryPolicy
