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:Scheduled
Activity task created but not yet picked up by a worker. The
ActivityTaskScheduled event is written to history.Started
Worker has picked up the task and begun execution. The
ActivityTaskStarted event records which worker and when.Activity Info
The server maintains rich metadata for each activity inActivityInfo:
ActivityInfo Structure
ActivityInfo Structure
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:- Loads the workflow’s mutable state
- Checks if activity is still in expected state
- Times out the activity or triggers retry
- Schedules a new workflow task
Activity Retry
Activities support automatic retry with exponential backoff:Retry Policy
Retry Logic
History Service evaluates retry policy
Checks attempt count, error type, and calculates next backoff interval
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
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
- Activity code calls heartbeat API with optional progress payload
- Worker sends HeartbeatActivityTask RPC to History Service
- History Service updates activity info with heartbeat time and details
- 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
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
Activity Dispatch
Activities are dispatched through the Matching Service:- Transfer Task created in History Service
- Queue Processor reads task and calls Matching Service
- Matching Service adds to appropriate task queue partition
- Worker polls and receives activity task
- Task includes activity input, attempt number, heartbeat details
Related Concepts
- Workflows - Schedule and orchestrate activities
- Task Queues - Route activity tasks to workers
- Workers - Execute activity code