Skip to content

Courses / Agentic AI / Module 14

Module 14: Durable execution and deployment: Temporal, queues, retries, long-running tasks

12 h · theory 4 · hands-on 5 · project 2 · revision 1

You will be able to

  • Run long tasks as durable workflows with Temporal
  • Retry, time out and compensate safely
  • Deploy the agent service with Docker and CI/CD
  • Scale workers

Capstone increment: Capstone live at a public HTTPS URL with Temporal workers and CI/CD.

Job-description skills: Durable execution, Distributed systems, Cloud · Lab tools: temporalio, docker, docker-compose, gh

1. Agent runs last minutes to days

A real incident run calls tools that time out, waits hours for an approval and spans deploys of your own service. A Python process holding the run in memory loses everything when it restarts. Durable execution keeps a run’s progress outside the process so any worker can continue it after a crash, a deploy or a week-long wait.

Temporal is the most widely used open-source engine for this. Workflows are your control flow; activities are the model calls and tool calls; the Temporal service records every result in an event history.

APIPOST /incidentsWorkflowdeterministic codeActivitiesLLM, tools, retriesSignalhuman approvalCompleteresult storedEvent historyevery resultTask queuework to doWorkersscale out, crash-safeThe workflow is the agent’s control flowTemporal servicerecordpoll + run
Model calls and tools are activities: retried, timed out and recorded. A crash replays the history instead of redoing the work.

2. Workflows, activities and replay

Workflow code must be deterministic: no direct network calls, random numbers or clock reads; those go in activities or through the SDK’s safe APIs. Activities do the real work and can fail and be retried. When a worker crashes, another worker replays the workflow from its history: completed activities return their recorded results instead of running again, and the workflow continues from where it was.

Signals send data into a running workflow (an approval, a cancellation); queries read its state; durable timers wait for hours or days without holding a process. Try the crash simulator to see what restarting from scratch costs compared with resuming.

Crash, then recoverThe worker dies mid-run (a deploy, an out-of-memory kill, a spot instance reclaimed). What happens when it comes back?

Duplicate side effects 3 · model calls paid twice ₹18 · extra hours waiting for a second approval 3

  1. Fetch logs and metrics read only
    ran 2×
  2. Diagnose with the model model call ₹18
    ran 2×
  3. Open a change ticket side effect: ticket
    ran 2×
  4. Wait for human approval waits about 3 h
    ran 2×
  5. Apply the config change side effect: config change
    ran 2×
  6. Notify the on-call channel side effect: message
    ran 2× (crashed here)
Guided experiments
  1. Crash during the last step and restart from the top. Count the duplicate tickets, config changes and messages. Who has to approve the change again?
  2. Now resume from recorded progress. Which step still runs twice, and why can no engine avoid that for a step that crashed after its side effect?
  3. Turn on idempotency keys. Why does “at-least-once” execution plus idempotent steps behave like “exactly once”?
  4. Crash while waiting for approval and resume. Why does the wait survive? What would it take to get that with a plain Python script and a cron job?

3. Retries, timeouts and compensation

Give every activity a start-to-close timeout and a retry policy with backoff, and mark errors that must not be retried (invalid arguments, permission denied). Long activities should heartbeat so a stuck one is detected and retried. Activities run at least once, so side-effecting ones need idempotency keys (Module 2).

When a multi-step change fails partway, compensate: undo completed steps in reverse order (the saga pattern). Some actions can’t be undone; gate those behind approval.

Common misconception: “Durable execution means exactly-once.” It means the workflow’s progress is never lost. Activities can still run more than once; idempotency makes that safe.

4. Deploy the agent service

Package the API and the workers as Docker images. For development, run the Temporal dev server, Postgres and your services with docker compose; in production, use Temporal Cloud or a self-hosted cluster. Keep model keys and tool credentials in a secret manager, and pin model versions in config.

Deploy from CI after tests and the eval gate pass. Changing workflow code that has runs in flight needs care: use the SDK’s versioning so old runs replay with the code they started with.

5. Scale workers

Workers poll task queues, so scaling is adding workers. Use separate queues for model calls and for tools, so a slow provider doesn’t starve fast tools, and cap concurrency per queue to stay inside provider rate limits. Scale on queue backlog rather than CPU.

Watch schedule-to-start latency (how long tasks wait for a worker) and activity failure rates on the dashboard, alongside the agent metrics from Module 12.

In production: Practise the failure: kill a worker in the middle of a run and confirm the run finishes on another worker with no duplicate side effects.

Hands-on: coding window

The coding window and the full VS Code lab are available after you sign in.

Sign in to code

Labs

Lab 14.1: Remediation as a workflow

Model the incident run as a Temporal workflow with activities for model calls, tools and ticketing, each with timeouts, retries and idempotency keys; approval arrives as a signal.

Expected: A workflow that waits for approval and completes after it.

Lab 14.2: Kill the worker

Start a run, kill the worker during an activity, start another worker, and show the run completing. Check the ticketing system for duplicates.

Expected: The run completes; history shows one recorded result per activity; no duplicate tickets.

Lab 14.3: Deploy (capstone increment)

Deploy the API and workers with docker compose behind Caddy, connected to Temporal, with a CI/CD workflow gated on tests and evals.

Expected: Capstone live at a public HTTPS URL with Temporal workers and CI/CD.

Go deeper

Videos

Further reading

  • Docs Temporal documentation · Temporal. Workflows, activities, retries, timers and signals.
  • Docs Temporal Workflow · Temporal docs. Why workflow code must be deterministic and how replay rebuilds state after a crash.
  • Docs Saga design pattern · Azure Architecture Center. Compensating actions for multi-step work that can't be one transaction.
  • Paper Sagas · Garcia-Molina and Salem, 1987. The original paper: long-lived transactions as steps with compensations.

GitHub repositories

Checkpoint quiz

1. What must workflow code avoid?
2. A worker crashes after two activities completed. What happens on restart?
3. How does an approval reach a waiting workflow?
4. Why do side-effecting activities still need idempotency?
5. Best signal to scale workers on?

Interview questions

  1. Why do agents need durable execution? What breaks without it?
  2. Explain workflows, activities and replay in Temporal.
  3. How would you implement a human approval that may take two days?
  4. What is a saga, and when would you use one in an ops agent?
  5. How do you deploy a change to workflow code while runs are in flight?

Ask the professor

Sign in to ask questions about this module.

Discussion

Community discussion appears here once giscus is configured (see README).