Skip to main content

Activities

Activities are the mechanism in Temporal for executing non-deterministic operations or side effects like API calls, database operations, or file I/O. Unlike workflow code which must be deterministic, activity code can interact with the outside world.

Activity Execution Model

Activities are scheduled by workflow code and executed by workers. The History Service orchestrates the activity lifecycle through events and task queues.

Activity Scheduling Flow

Activity State Machine

The History Service tracks activity state in Mutable State:
1

Scheduled

Activity task created but not yet picked up by a worker. The ActivityTaskScheduled event is written to history.
2

Started

Worker has picked up the task and begun execution. The ActivityTaskStarted event records which worker and when.
3

Completed / Failed / Timed Out / Canceled

Activity reaches terminal state. Result or failure is recorded in corresponding event.

Activity Info

The server maintains rich metadata for each activity in ActivityInfo:

Activity Timeouts

Temporal enforces four types of activity timeouts to prevent stuck activities:

Schedule-To-Start

Maximum time activity can wait in the task queue before being picked up by a worker. Detects unavailable workers.

Start-To-Close

Maximum execution time from when worker starts until completion. Detects hung activity execution.

Schedule-To-Close

End-to-end timeout including queue time, execution, and all retries. Overall deadline for activity.

Heartbeat

Maximum time between heartbeats from long-running activities. Detects crashed workers.

Timeout Implementation

Timeouts are implemented as Timer Tasks in the History Service:
When a timer fires, the queue processor:
  1. Loads the workflow’s mutable state
  2. Checks if activity is still in expected state
  3. Times out the activity or triggers retry
  4. Schedules a new workflow task

Activity Retry

Activities support automatic retry with exponential backoff:

Retry Policy

Retry Logic

1

Activity fails

Worker sends RespondActivityTaskFailed with failure information
2

History Service evaluates retry policy

Checks attempt count, error type, and calculates next backoff interval
3

Schedule retry or fail workflow

If retryable: Create ActivityRetryTimer task and keep activity in Scheduled state If not retryable: Append ActivityTaskFailed event and schedule workflow task
4

Timer fires

Activity is re-dispatched to Matching Service for another attempt
Activity retry attempts do NOT create new history events. Only the final success or non-retryable failure creates an event. This keeps history bounded for activities with many retries.

Activity Heartbeating

Long-running activities should heartbeat to prove they’re still alive:

Heartbeat Mechanism

  1. Activity code calls heartbeat API with optional progress payload
  2. Worker sends HeartbeatActivityTask RPC to History Service
  3. History Service updates activity info with heartbeat time and details
  4. Heartbeat timer is reset to detect future missed heartbeats

Heartbeat Benefits

Failure Detection

Quickly detect when worker or activity has crashed without waiting for full timeout

Progress Tracking

Application can query activity details to show progress to users

Resumption

Activity can resume from last heartbeat point if worker crashes

Observability

Heartbeat timestamps visible in workflow execution history

Activity Cancellation

Workflows can request activity cancellation:

Cancellation Flow

Activity cancellation is a request, not a guarantee. The activity code must explicitly check for cancellation and handle it gracefully.

Local Activities

Local activities are a special optimization for very short activities:
  • Execute in the same worker process as workflow
  • Not recorded in history until completion
  • Lower latency (no History Service roundtrip)
  • Limited to short operations (seconds)
  • No cross-worker routing
Use local activities for fast operations like cache lookups or simple calculations that don’t justify the overhead of full activity scheduling.

Activity Dispatch

Activities are dispatched through the Matching Service:
  1. Transfer Task created in History Service
  2. Queue Processor reads task and calls Matching Service
  3. Matching Service adds to appropriate task queue partition
  4. Worker polls and receives activity task
  5. Task includes activity input, attempt number, heartbeat details