ARCHITECTURE · DURABLE EXECUTION · 5 MIN READ
An agent that can pause and resume
Persist progress and make side effects safe to retry.
Jump to a section 11 sections
EXAMPLE USE CASE
Resume draft preparation after a worker crashes
A worker can stop after an effect is recorded but before the execution checkpoint advances.
- Input
- A stable run identifier, a draft operation ID and a persistent SQLite checkpoint database.
- Expected output
- A completed checkpoint and exactly one draft effect when the same run is retried.
- Success check: Crash the fresh run after its effect, then restart it; the retry reuses the operation ID and does not duplicate the row.
- Try a failure: Generate a fresh operation ID on each retry and observe why it defeats deduplication.
Example scope: A local persistent recovery lab. A remote API requires its own idempotency/reconciliation; the linked Temporal example provides an actual durable workflow runtime.
Create an explicit run/operation identity and retain the expected result contract.
enqueueTo 02 · Durable controllerPersist progress and schedule replay-safe workflow steps without keeping one web request alive.
activityTo 03 · Activity workerpause or finishTo 06 · Review or completionRun non-deterministic work as activities with timeouts and a bounded retry policy.
operation IDTo 04 · Business serviceUse a stable operation ID and reconcile uncertain writes before sending the same effect again.
effect resultTo 05 · Result + checkpointPersist the actual effect/result and execution position; external API idempotency remains a separate requirement.
Pause for review when required and expose completion, rejection, cancellation or exhausted retries to the caller.
Result or stopping point
Persist progress and schedule replay-safe workflow steps without keeping one web request alive.
Components for this use case
- Temporal, Inngest or Trigger.dev can persist workflow execution. The business API owns idempotency for writes. A database owns durable run and operation identifiers.
- A checkpoint records where to continue; an idempotency record prevents repeating an effect. They solve different failure windows.
Persist the execution boundary
- Store the state needed to continue a task after a process exits.
- Checkpoints track a particular execution or thread.
- Long-term user memory serves a different purpose and can live alongside the workflow state.
Make replay deliberate
- A retry can repeat an external request.
- Assign idempotency keys to writes, record committed outcomes, and reconcile uncertain results before retrying.
- A workflow engine cannot undo an external side effect merely by restoring local state.
Choose the runtime
- Explicit agent graphs and durable workflow engines offer different abstractions.
- Evaluate pause/resume behavior, timers, human approval, deployment operations, and recovery using your actual failure scenarios.
Set up the lab
- Use a new working directory for this example.
python3 -m venv .venv
source .venv/bin/activateImplement the example
Save as durable.py
import argparse
import json
import sqlite3
parser = argparse.ArgumentParser()
parser.add_argument("--run", default="run-104")
parser.add_argument("--crash-after-effect", action="store_true")
args = parser.parse_args()
con = sqlite3.connect("workflow-demo.sqlite")
con.execute("CREATE TABLE IF NOT EXISTS effects (operation TEXT PRIMARY KEY, result TEXT)")
con.execute("CREATE TABLE IF NOT EXISTS checkpoints (run TEXT PRIMARY KEY, step TEXT)")
row = con.execute("SELECT step FROM checkpoints WHERE run=?", (args.run,)).fetchone()
previous = row[0] if row else "prepare-draft"
operation = args.run + ":draft"
if previous != "complete":
# The effect commits first: a crash can leave the checkpoint behind it.
with con:
con.execute("INSERT OR IGNORE INTO effects VALUES (?,?)", (operation,"draft-created"))
if args.crash_after_effect:
raise SystemExit("Simulated crash after committed effect; rerun this run ID")
with con:
con.execute("INSERT OR REPLACE INTO checkpoints VALUES (?,?)", (args.run,"complete"))
count = con.execute("SELECT count(*) FROM effects WHERE operation=?", (operation,)).fetchone()[0]
assert count == 1
print(json.dumps({"run": args.run, "resumed_from": previous, "step":"complete", "effects":count}))
con.close()Run and verify
python durable.py
python durable.py
# In a fresh run, exercise the crash window:
python durable.py --run fault-demo --crash-after-effect
# The previous command intentionally exits 1. Now resume:
python durable.py --run fault-demo- The default run finishes; repeating it reports resumed_from complete and one effect.
- The fault-demo run records its effect then exits before checkpointing. Restarting that exact run ID finishes with one effect.
Troubleshooting and failure tests
- A changing operation ID breaks deduplication even if the workflow checkpoint persists.
- The SQLite uniqueness rule only controls this local effect. A remote API needs its own idempotency key or reconciliation.
Before production
- Use Temporal or another durable runtime for service activity history, retry policy and distributed worker recovery.
- Version workflow definitions and define compensation, dead-letter handling and the owner of incomplete runs.