> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/temporalio/temporal/llms.txt
> Use this file to discover all available pages before exploring further.

# Chasm Architecture

> Coordinated Heterogeneous Application State Machines framework

# 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.

<Note>
  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.
</Note>

## 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

Chasm was created to provide a structured framework for building these features while maintaining Temporal's durability and consistency guarantees.

## Core Concepts

### Terminology

Chasm introduces several key concepts:

<AccordionGroup>
  <Accordion title="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
  </Accordion>

  <Accordion title="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
  </Accordion>

  <Accordion title="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
  </Accordion>

  <Accordion title="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)
  </Accordion>
</AccordionGroup>

```mermaid theme={null}
flowchart TB
    subgraph Execution["Execution (e.g., Scheduler)"]
        Root["Root Component<br/>(Scheduler)"]
        
        subgraph Children["Child Components"]
            Gen["Generator"]
            Inv["Invoker"]
            BF1["Backfiller 1"]
            BF2["Backfiller 2"]
        end
        
        Root --> Gen
        Root --> Inv
        Root --> BF1
        Root --> BF2
    end
    
    style Execution fill:#e1f5ff
    style Children fill:#fff4e1
```

## 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

Each component:

* 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

**Side Effect Tasks:**

* Make external service calls
* Execute immediately but with retries
* Handled by History Service transfer queue

```mermaid theme={null}
sequenceDiagram
    participant Comp as Component
    participant History as History Service
    participant Queue as Queue Processor
    participant External as External Service
    
    Comp->>History: Schedule TimerTask(10s)
    History->>History: Persist task
    
    Note over Queue: Wait 10 seconds
    
    Queue->>History: Process TimerTask
    History->>Comp: Execute task handler
    Comp->>History: Update state<br/>Schedule SideEffectTask
    History->>History: Persist state & task
    
    Queue->>History: Process SideEffectTask
    History->>Comp: Execute side effect
    Comp->>External: RPC call
    External-->>Comp: Response
    Comp->>History: Update state
```

## Component Lifecycle

### Component Creation

Components are created dynamically:

```go theme={null}
// Pseudo-code
func (s *Scheduler) OnTriggerRequest(req TriggerRequest) {
    // Create a new Backfiller component
    backfiller := s.CreateChildComponent(
        "Backfiller",
        BackfillerState{
            BackfillID: generateID(),
            Request:    req,
        },
    )
    
    // Schedule initial task
    backfiller.ScheduleTask(BackfillerTask{
        FireTime: time.Now(),
    })
}
```

### Component Deletion

Components can delete themselves:

```go theme={null}
func (b *Backfiller) OnBackfillerTask() {
    if b.IsComplete() {
        b.DeleteSelf()
        return
    }
    
    // Continue processing
    b.ProcessNextBatch()
}
```

### State Transitions

Components transition state via task handlers:

```go theme={null}
func (c *Component) OnTimerTask() {
    // 1. Read current state
    state := c.GetState()
    
    // 2. Apply transition logic
    state.Counter++
    state.LastUpdate = time.Now()
    
    // 3. Schedule next task
    if !state.IsComplete {
        c.ScheduleTask(TimerTask{
            FireTime: time.Now().Add(time.Minute),
        })
    }
    
    // 4. Persist state
    c.UpdateState(state)
}
```

## 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:

```go theme={null}
type SchedulerComponent interface {
    // Query handler (read-only)
    OnDescribeRequest(req DescribeRequest) DescribeResponse
    
    // Update handler (modifies state)
    OnPauseRequest(req PauseRequest) PauseResponse
    
    // Signal handler (fire-and-forget)
    OnTriggerSignal(signal TriggerSignal)
}
```

### 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

```mermaid theme={null}
classDiagram
    class Scheduler {
        +Schedule schedule
        +Info info
        +OnDescribeRequest()
        +OnPauseRequest()
        +OnTriggerRequest()
    }
    
    class Generator {
        +Timestamp lastProcessedTime
        +OnGeneratorTask()
    }
    
    class Invoker {
        +BufferedStart[] bufferedStarts
        +OnProcessBufferTask()
        +OnExecuteTask()
    }
    
    class Backfiller {
        +UUID backfillID
        +BackfillRequest request
        +OnBackfillerTask()
    }
    
    Scheduler "1" --> "1" Generator : owns
    Scheduler "1" --> "1" Invoker : owns
    Scheduler "1" --> "0..*" Backfiller : owns
```

### Task Flow

**Generator Task:**

* Fires periodically based on schedule spec
* Buffers actions into Invoker
* Reschedules itself for next interval

**Invoker Tasks:**

* `ProcessBufferTask`: Evaluates overlap policies
* `ExecuteTask`: Starts workflows via RPC

**Backfiller Task:**

* Processes manual backfill requests
* Buffers actions into Invoker
* Deletes itself when complete

```mermaid theme={null}
sequenceDiagram
    participant Gen as Generator
    participant Inv as Invoker
    participant BF as Backfiller
    participant Frontend
    
    Note over Gen: GeneratorTask fires
    Gen->>Inv: EnqueueBufferedStarts(actions)
    Inv->>Inv: Schedule ProcessBufferTask
    Gen->>Gen: Schedule next GeneratorTask
    
    Note over Inv: ProcessBufferTask fires
    Inv->>Inv: Resolve overlap policies
    Inv->>Inv: Mark eligible starts
    Inv->>Inv: Schedule ExecuteTask
    
    Note over Inv: ExecuteTask fires
    Inv->>Frontend: StartWorkflowExecution
    Frontend-->>Inv: Started
    Inv->>Inv: Record result
    Inv->>Inv: Schedule next ExecuteTask<br/>(if retries pending)
    
    Note over BF: BackfillerTask fires
    BF->>Inv: EnqueueBufferedStarts(backfill actions)
    BF->>BF: Update lastProcessedTime
    BF->>BF: Schedule next BackfillerTask<br/>or delete self if complete
```

See [Schedules Architecture](https://github.com/temporalio/temporal/blob/main/docs/architecture/schedules.md) for full details.

## Completion Callbacks

Chasm supports **asynchronous completion callbacks** for side effects:

### Nexus Completion

When a component starts a workflow:

1. Generate a **completion callback token**
2. Include token in `StartWorkflowExecution` request
3. When workflow completes, callback is triggered
4. Component receives notification and updates state

```go theme={null}
// Start workflow with callback
token := chasm.GenerateCallbackToken(componentRef)
resp, err := client.StartWorkflowExecution(ctx, &workflowservice.StartWorkflowExecutionRequest{
    // ... other fields
    CompletionCallbacks: []*commonpb.Callback{
        {
            Variant: &commonpb.Callback_Nexus_{
                Nexus: &commonpb.Callback_Nexus{
                    Url: callbackURL,
                    Token: token,
                },
            },
        },
    },
})

// Later, when workflow completes
func (c *Component) OnCompletionCallback(result NexusResult) {
    c.state.LastResult = result
    c.UpdateState(c.state)
}
```

## Advantages of Chasm

<AccordionGroup>
  <Accordion title="Composition">
    Build complex features by composing simpler components hierarchically.
  </Accordion>

  <Accordion title="Reusability">
    Components can be reused across different archetypes.
  </Accordion>

  <Accordion title="Testability">
    Components can be tested independently.
  </Accordion>

  <Accordion title="Evolution">
    Components can be added/removed without breaking existing state.
  </Accordion>

  <Accordion title="Performance">
    Only active components are loaded into memory.
  </Accordion>
</AccordionGroup>

## 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

```go theme={null}
// Core Chasm interfaces
type Component interface {
    // State management
    GetState() interface{}
    UpdateState(state interface{}) error
    
    // Task scheduling
    ScheduleTask(task Task)
    
    // Child management
    CreateChildComponent(componentType string, state interface{}) Component
    DeleteSelf()
    
    // Request handling
    HandleRequest(req Request) (Response, error)
}
```

### 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

```go theme={null}
// Code entrypoint: service/history/chasm_engine.go
// ChasmEngine handles Chasm component execution
```

## Use Cases

Chasm is used to implement:

<CardGroup cols={2}>
  <Card title="Scheduler" icon="clock">
    Periodic workflow execution with backfill support
  </Card>

  <Card title="Worker Deployment" icon="server">
    Managing worker versioning and deployment
  </Card>

  <Card title="Nexus Operations" icon="link">
    Cross-namespace workflow invocations
  </Card>

  <Card title="Activity Execution" icon="bolt">
    (Planned) Enhanced activity state management
  </Card>
</CardGroup>

## 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

<AccordionGroup>
  <Accordion title="Keep Components Focused">
    Each component should have a single, well-defined responsibility.
  </Accordion>

  <Accordion title="Minimize Component State">
    Store only essential state; derive everything else.
  </Accordion>

  <Accordion title="Handle Failures Gracefully">
    Tasks may fail and be retried; ensure idempotency.
  </Accordion>

  <Accordion title="Document State Machines">
    Use diagrams to document component interactions and state transitions.
  </Accordion>
</AccordionGroup>

## 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

<Warning>
  Chasm is an internal framework and its APIs may change. External developers should not depend on Chasm directly but use the stable public APIs that are built on top of it.
</Warning>

## Further Reading

<CardGroup cols={2}>
  <Card title="Schedules Architecture" icon="calendar" href="https://github.com/temporalio/temporal/blob/main/docs/architecture/schedules.md">
    Detailed Scheduler implementation using Chasm
  </Card>

  <Card title="History Service" icon="database" href="/architecture/history-service">
    How Chasm integrates with History Service
  </Card>

  <Card title="Event Sourcing" icon="timeline" href="/architecture/event-sourcing">
    State management foundation
  </Card>

  <Card title="Architecture Overview" icon="sitemap" href="/architecture/overview">
    High-level system architecture
  </Card>
</CardGroup>
