Chasm Architecture
Chasm (Coordinated Heterogeneous Application State Machines) is Temporal’s library for building complex, durable, distributed applications using a hierarchical state machine model. It provides a framework for implementing Temporal features that require sophisticated state management beyond traditional workflows.Chasm is an internal implementation framework, not a user-facing API. It is used to implement features like Scheduler, Worker Deployment, and other advanced Temporal capabilities.
Motivation
Temporal initially built all features using the core Workflow concept. However, certain features required:- Complex state machines with multiple concurrent components
- Long-lived executions with evolving requirements
- Hierarchical composition of state machines
- Sophisticated lifecycle management
- Rich interaction patterns beyond workflow signals/queries
Core Concepts
Terminology
Chasm introduces several key concepts:Archetype
Archetype
An Archetype is a type of state machine (e.g., Workflow, Activity, Scheduler).Each archetype defines:
- State structure
- Allowed transitions
- Task types
- Request-reply handlers
Execution
Execution
An Execution is an instance of an archetype (e.g., a specific workflow execution, a specific scheduler).Each execution has:
- A BusinessID (user-meaningful identifier)
- Unique across non-terminal executions in a namespace
- Can consist of multiple runs
Component
Component
A Component is a node in the execution tree.An execution is structured as a tree where:
- Root component represents the execution
- Child components represent sub-state machines
- Each component has its own state and tasks
ComponentRef
ComponentRef
A ComponentRef uniquely identifies a component within an execution.Contains:
- ExecutionKey (namespace + business ID)
- ComponentPath (tree path to component)
- Transition information (for multi-cluster failover)
Architecture
Hierarchical State Machines
Chasm applications are structured as trees of components:- Root component: Represents the overall execution
- Child components: Represent sub-state machines
- Leaf components: Have no children
- Has its own state (persisted)
- Schedules its own tasks (timers, side effects)
- Handles requests (via request-reply pattern)
- Can create/delete child components dynamically
State Management
Component state is persisted using Temporal’s existing infrastructure:- Stored in Mutable State (same as workflow state)
- Uses event sourcing (state changes generate events)
- Atomic updates via database transactions
- Cached for performance
Task Processing
Components schedule tasks for execution: Timer Tasks:- Execute at a specific time
- Drive time-based state transitions
- Handled by History Service timer queue
- Make external service calls
- Execute immediately but with retries
- Handled by History Service transfer queue
Component Lifecycle
Component Creation
Components are created dynamically:Component Deletion
Components can delete themselves:State Transitions
Components transition state via task handlers:Request-Reply Pattern
Chasm supports synchronous request-reply interactions:Request Types
- Queries: Read-only, do not modify state
- Updates: Modify state, wait for effect to be durable
- Signals: Fire-and-forget state modifications
Request Handling
Components implement request handlers:Routing
Requests are routed to the appropriate component:- Root requests: Sent to root component
- Component-specific requests: Include ComponentPath in request
- Broadcast requests: Fan out to multiple components
Example: Scheduler Implementation
Temporal’s Scheduler feature is implemented using Chasm:Component Structure
Task Flow
Generator Task:- Fires periodically based on schedule spec
- Buffers actions into Invoker
- Reschedules itself for next interval
ProcessBufferTask: Evaluates overlap policiesExecuteTask: Starts workflows via RPC
- Processes manual backfill requests
- Buffers actions into Invoker
- Deletes itself when complete
Completion Callbacks
Chasm supports asynchronous completion callbacks for side effects:Nexus Completion
When a component starts a workflow:- Generate a completion callback token
- Include token in
StartWorkflowExecutionrequest - When workflow completes, callback is triggered
- Component receives notification and updates state
Advantages of Chasm
Composition
Composition
Build complex features by composing simpler components hierarchically.
Reusability
Reusability
Components can be reused across different archetypes.
Testability
Testability
Components can be tested independently.
Evolution
Evolution
Components can be added/removed without breaking existing state.
Performance
Performance
Only active components are loaded into memory.
Comparison with Workflows
| Aspect | Traditional Workflow | Chasm Execution |
|---|---|---|
| Structure | Single state machine | Tree of state machines |
| State | Workflow variables | Per-component state |
| Tasks | Workflow & Activity Tasks | Component-specific tasks |
| Composition | Child workflows | Component hierarchy |
| Lifecycle | Start → Complete | Dynamic component creation/deletion |
| Use Cases | Business workflows | Advanced Temporal features |
Implementation Details
Storage
Chasm state is stored in the same tables as workflow state:- Component state in Mutable State columns
- Component tasks in History task queues
- Uses existing persistence infrastructure
Code Organization
Chasm library is located in/chasm directory:
/chasm/lib: Component implementations (Scheduler, Activity, NexusOperation)/chasm/interfaces.go: Core interfaces/chasm/nexus_completion.go: Completion callback support
Integration with History Service
Chasm components are executed by History Service:- Task execution goes through existing queue processors
- State updates use existing transaction mechanisms
- No separate service required
Use Cases
Chasm is used to implement:Scheduler
Periodic workflow execution with backfill support
Worker Deployment
Managing worker versioning and deployment
Nexus Operations
Cross-namespace workflow invocations
Activity Execution
(Planned) Enhanced activity state management
Development Guidelines
When to Use Chasm
Consider Chasm when:- Feature requires multiple concurrent state machines
- Need dynamic component creation/deletion
- Require sophisticated lifecycle management
- State machine structure evolves over time
When NOT to Use Chasm
Don’t use Chasm when:- Simple workflow is sufficient
- State machine structure is fixed
- No need for hierarchical composition
- Complexity overhead not justified
Best Practices
Keep Components Focused
Keep Components Focused
Each component should have a single, well-defined responsibility.
Minimize Component State
Minimize Component State
Store only essential state; derive everything else.
Handle Failures Gracefully
Handle Failures Gracefully
Tasks may fail and be retried; ensure idempotency.
Document State Machines
Document State Machines
Use diagrams to document component interactions and state transitions.
Future Directions
Chasm is evolving to support:- More archetypes: Activity, Query, Update as first-class Chasm components
- Better tooling: Visualization and debugging tools
- Cross-cluster support: Enhanced multi-cluster coordination
- Performance optimizations: Lazy loading, state compression
Further Reading
Schedules Architecture
Detailed Scheduler implementation using Chasm
History Service
How Chasm integrates with History Service
Event Sourcing
State management foundation
Architecture Overview
High-level system architecture