ballerina/workflow Ballerina library

0.3.0
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.
Copy
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:

Copy
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():

Copy
// 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:

PatternExample
Wait for allctx->await([f1, f2])
Wait for any (first wins)ctx->await([f1, f2], 1)
Quorum (N of M)ctx->await([f1, f2, f3], 2)
With deadlinectx->await([f1, f2], timeout = {hours: 48})

Error Handling

Activity errors are returned as plain Ballerina values. The workflow decides what happens next:

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

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

Copy
[ballerina.workflow]
mode = "IN_MEMORY"

For production, connect to a Temporal server:

Copy
[ballerina.workflow]
mode = "TEMPORAL"
temporalHost = "localhost"
temporalPort = 7233
namespace = "default"
taskQueue = "my-task-queue"

Documentation

GuideDescription
Get StartedWrite and run your first workflow
Key ConceptsWorkflows, activities, external data, and timers
Write Workflow FunctionsSignatures, determinism rules, and durable sleep
Write Activity FunctionsActivity patterns and retry options
Handle DataWaiting for external input and sending data
Handle ErrorsPropagation, retry, fallback, and compensation
Configure the ModuleConnection settings, TLS, and namespaces

Examples

ExampleDescription
Get StartedFirst workflow
Order ProcessingHTTP-triggered workflow with result polling
Human in the LoopPause for a human approval
Wait for AllDual authorization — both teams must approve
Alternative WaitFirst responder wins (approval ladder)
Forward RecoveryPause for corrected data and retry
Error PropagationFail the workflow on a critical error
Error FallbackFall back to a secondary activity
Error CompensationSaga: undo committed steps on failure

Import

import ballerina/workflow;Copy

Other versions

Metadata

Released date: 7 days ago

Version: 0.3.0

License: Apache-2.0


Compatibility

Platform: java21

Ballerina version: 2201.13.0

GraalVM compatible: No


Pull count

Total: 150

Current verison: 18


Weekly downloads


Source repository


Keywords

workflow


Contributors