---
url: /services/workflow/common-workflow-compositions.md
description: >-
  Patterns for composing BPMN workflows including fan-out/fan-in, pipelines, and
  conditional flows using exclusive gateways.
---

# Common Workflow Compositions

Understanding common workflow compositions helps you design better workflows and solve problems more effectively.

## 1. Fan-Out/Fan-In (Parallel Processing)

```
○ → [Prepare Data] → ◇+ → [Process A] → ◇+ → [Combine Results] → ●
                      └ → [Process B] → ┘
```

**Use when**: Processing the same data with multiple services simultaneously

**Benefits**:

* Faster execution (parallel instead of sequential)
* Independent processing paths
* Results synchronized automatically

**Example**: Execute a quantum circuit on multiple backends to compare results.

## 2. Pipeline (Sequential Processing)

```
○ → [Step 1] → [Step 2] → [Step 3] → [Step 4] → ●
```

**Use when**: Each step depends on the previous step's output

**Benefits**:

* Clear data flow
* Simple to understand and debug
* Each step transforms data for the next

**Example**: Generate circuit → Encode → Execute → Decode → Visualize

## 3. Conditional Flow (Exclusive Gateway)

```
○ → [Check Condition] → ◇× → [Path A] → ●
                         └→ [Path B] → ●
```

**Use when**: Different actions needed based on data or conditions

**Benefits**:

* Dynamic workflow behavior
* Handle different scenarios
* Optimize based on input characteristics

**Example**: Use fast optimization for small problems, accurate optimization for large problems.

**Setting Conditions**:

1. Add an **Exclusive Gateway** (◇×) from the palette
2. Create multiple outgoing paths
3. Select each sequence flow (arrow)
4. In properties, set **Condition expression**:
   ```javascript
   = count(flightRoutes) < 5  // Fast path
   = count(flightRoutes) >= 5  // Accurate path
   ```

## 4. Error Handling with Boundary Events

```
○ → [Main Task] → [Success Action] → ●
    │
    └→ [Error Handler] → [Cleanup] → ●
```

**Use when**: You need to handle failures gracefully

**Benefits**:

* Graceful degradation
* User notification on errors
* Cleanup and recovery actions

See [Handling Errors in Workflow Services](./error-handling.md) for the full contract — which BPMN errors are raised, how the boundary auto-wires to `SERVICE_FAILED`, and how to read the failure payload via the per-host `<hostId>_serviceError` variable.

### 5. Looping (Multi-Instance)

```
○ → [For Each Item in Array] → [Process Item] → [Collect Results] → ●
```

**Use when**: You need to process array data iteratively

**Benefits**:

* Process collections automatically
* Parallel or sequential execution
* Aggregate results

**Implementation**:

1. Select a service task
2. In properties, find **Multi Instance**
3. Set **Loop Type**: Parallel or Sequential
4. Set **Input Collection**: Array variable name
5. Set **Element Variable**: Name for current item

**Example**: Process each flight route individually for detailed analysis.

## 6. Timer Events (Scheduled Execution)

```
○ (clock icon) → [Periodic Task] → [Process Data] → ●
```

**Use when**: You need periodic or scheduled execution

**Benefits**:

* Automated scheduling
* Time-based triggers
* Periodic monitoring

**Types**:

* **Timer Start Event**: Trigger workflow on schedule
* **Timer Intermediate Event**: Wait for duration
* **Timer Boundary Event**: Timeout handling

**Example**: Run optimization every hour with latest flight data.

## 7. Message Events (External Triggers)

```
○ (envelope icon) → [Wait for Message] → [Process] → ●
```

**Use when**: Workflows triggered by external systems

**Benefits**:

* Event-driven architecture
* Integration with external systems
* Asynchronous communication

**Use Cases**:

* API webhooks
* External data updates
* User actions
