Workflow Execution Lifecycle
This page follows a typical sequence of events in the execution of a simple workflow, showing how requests flow between User Applications, Frontend, History, Matching services, and Workers.Example Workflow
We’ll trace the execution of this simple workflow:The diagrams below show the detailed interaction between services. For high-level architecture, see Architecture Overview.
In the diagrams below:
- “Initialize history” and “Append event to history” refer to durable writes to the persistence layer
- “Persist Mutable State and history tasks” refers to atomic database transactions
Step 1: Start Workflow Execution
The user application sends aStartWorkflowExecution request:
- Workflow History is initialized with
[WorkflowExecutionStarted, WorkflowTaskScheduled] - A Transfer Task is created to add a Workflow Task to the Matching service
State After Step 1
Code Entrypoints
Code Entrypoints
- History service
StartWorkflowhandler:service/history/api/startworkflow/api.go - Queue processors:
service/history/history_engine.go:303 - Transfer task processor:
service/history/queues/queue_immediate.go:150
Step 2: Worker Polls and Processes Workflow Task
A worker dequeues the Workflow Task and advances workflow execution:- The workflow code executes and becomes blocked on the activity call
- A
WorkflowTaskStartedevent is appended to history
State After Step 2
Code Entrypoints
Code Entrypoints
- History service
RecordWorkflowTaskStartedhandler:service/history/handler.go:319
Step 3: Worker Completes Workflow Task with ScheduleActivity Command
The workflow launches an activity, causing the worker to send aScheduleActivityTask command:
- Events are appended:
WorkflowTaskCompleted,ActivityTaskScheduled - A Transfer Task is created to add an Activity Task to Matching
State After Step 3
Code Entrypoints
Code Entrypoints
- History service
ScheduleActivityTaskcommand handler:service/history/workflow_task_handler.go:338
Step 4: Worker Polls and Executes Activity
A worker dequeues the Activity Task and executes the activity:- An
ActivityTaskStartedevent is appended - The activity code runs (can have side effects)
State After Step 4
Code Entrypoints
Code Entrypoints
- History service
RecordActivityTaskStartedhandler:service/history/handler.go:287
Step 5: Worker Completes Activity Task
Once the activity completes, the worker sends the result back:- Events are appended:
ActivityTaskCompleted,WorkflowTaskScheduled - A new Workflow Task is created to allow the workflow to process the result
State After Step 5
Code Entrypoints
Code Entrypoints
- History service
RespondActivityTaskCompletedhandler:service/history/handler.go:361
Step 6: Worker Processes Final Workflow Task
The worker dequeues the Workflow Task and advances the workflow:- The workflow finds it has reached its end (received activity result)
- The worker will send a
CompleteWorkflowExecutioncommand
This step has the same sequence diagram as Step 2 above.
Step 7: Worker Completes Workflow Execution
The worker sendsRespondWorkflowTaskCompleted with a CompleteWorkflowExecution command:
- Events are appended:
WorkflowTaskCompleted,WorkflowExecutionCompleted - Additional tasks are created for visibility, archival, and retention
Final State
Code Entrypoints
Code Entrypoints
- History service
RespondWorkflowTaskCompletedhandler:service/history/handler.go:478
Alternative Path: Activity Failure and Retry
If an activity fails, it can be automatically retried:State After Activity Failure
Code Entrypoints
Code Entrypoints
- History service
RespondActivityTaskFailedhandler:service/history/handler.go:400 - Activity retry timer handling:
service/history/timer_queue_active_task_executor.go:426
Key Takeaways
Event Sourcing
Event Sourcing
Every state change is recorded as an immutable event in Workflow History. The complete history can be used to reconstruct any past state.
State Transitions
State Transitions
Each request (from user or worker) triggers a state transition that atomically:
- Appends new events to history
- Updates Mutable State
- Creates new tasks (Transfer or Timer)
Task Queues as Buffers
Task Queues as Buffers
History Service generates tasks that are processed asynchronously by Queue Processors, providing natural buffering and backpressure.
Worker-Driven Execution
Worker-Driven Execution
Workflows only make progress when workers poll for and complete tasks. The cluster never directly executes user code.
Further Reading
History Service
Deep dive into History Service internals
Matching Service
How task queues work
Event Sourcing
Event sourcing implementation details
Architecture Overview
High-level system architecture