Module workflow
ballerina/workflow Ballerina library
Ballerina Workflow Library
The ballerina/workflow library provides durable, fault-tolerant workflow orchestration for Ballerina applications. It lets you define long-running business processes — spanning minutes, hours, or days — that automatically recover from crashes and process restarts without losing progress.
Overview
Workflows and activities are ordinary Ballerina functions:
@workflow:Workflow— A durable function that orchestrates a business process. The runtime checkpoints every step and replays recorded history to recover from failures.@workflow:Activity— A function that performs a single non-deterministic operation (API call, database query, email send). Once an activity completes, its result is recorded and never re-executed during replay.
import ballerina/workflow; @workflow:Activity function checkInventory(string item) returns boolean|error { // Call external inventory API return true; } @workflow:Workflow function processOrder(workflow:Context ctx, OrderRequest request) returns OrderResult|error { boolean inStock = check ctx->callActivity(checkInventory, {"item": request.item}); if !inStock { return {orderId: request.orderId, status: "OUT_OF_STOCK"}; } return {orderId: request.orderId, status: "COMPLETED"}; }
Starting a Workflow
Use workflow:run() to start a workflow instance from any entry point — HTTP service, scheduled job, message consumer, or main:
string workflowId = check workflow:run(processOrder, {orderId: "ORD-001", item: "laptop"});
Receiving External Data
A workflow can pause and wait for external input — approvals, payment confirmations, user decisions — using future-based event records. Send data to a running workflow with workflow:sendData():
// In the workflow — wait for a human decision ApprovalDecision decision = check wait events.approval; // From outside — deliver the decision check workflow:sendData(processOrder, workflowId, "approval", {approverId: "mgr-1", approved: true});
Multi-Future Waits with ctx->await
Use ctx->await to wait for multiple futures at once, with optional quorum and timeout:
| Pattern | Example |
|---|---|
| Wait for all | ctx->await([f1, f2]) |
| Wait for any (first wins) | ctx->await([f1, f2], 1) |
| Quorum (N of M) | ctx->await([f1, f2, f3], 2) |
| With deadline | ctx->await([f1, f2], timeout = {hours: 48}) |
Error Handling
Activity errors are returned as plain Ballerina values. The workflow decides what happens next:
string|error result = ctx->callActivity(chargeCard, {"amount": input.amount}); if result is error { // retry with a different card, fall back, or compensate }
Enable automatic retries for transient failures:
string result = check ctx->callActivity(chargeCard, {"amount": input.amount}, retryOnError = true, maxRetries = 3);
Configuration
Add a Config.toml to your project. For local development with no server:
[ballerina.workflow] mode = "IN_MEMORY"
For production, connect to a Temporal server:
[ballerina.workflow] mode = "TEMPORAL" temporalHost = "localhost" temporalPort = 7233 namespace = "default" taskQueue = "my-task-queue"
Documentation
| Guide | Description |
|---|---|
| Get Started | Write and run your first workflow |
| Key Concepts | Workflows, activities, external data, and timers |
| Write Workflow Functions | Signatures, determinism rules, and durable sleep |
| Write Activity Functions | Activity patterns and retry options |
| Handle Data | Waiting for external input and sending data |
| Handle Errors | Propagation, retry, fallback, and compensation |
| Configure the Module | Connection settings, TLS, and namespaces |
Examples
| Example | Description |
|---|---|
| Get Started | First workflow |
| Order Processing | HTTP-triggered workflow with result polling |
| Human in the Loop | Pause for a human approval |
| Wait for All | Dual authorization — both teams must approve |
| Alternative Wait | First responder wins (approval ladder) |
| Forward Recovery | Pause for corrected data and retry |
| Error Propagation | Fail the workflow on a critical error |
| Error Fallback | Fall back to a secondary activity |
| Error Compensation | Saga: undo committed steps on failure |