Skip to content

Compensation Engine Specification

Document ID: WF-009 Version: 1.0.0 Status: Draft Owner: Workflow Engine Team Last Updated: 2026-06-26


This document defines the Compensation Engine for the Wovyr Workflow Engine.

The Compensation Engine provides reliable rollback for long-running distributed workflows by executing business-defined compensation actions instead of relying on traditional ACID database transactions.

The engine supports:

  • Saga Pattern
  • Distributed transactions
  • Rollback workflows
  • Partial rollback
  • Nested compensation
  • Parallel compensation
  • Compensation retry
  • Compensation auditing
  • Recovery after failures

The Compensation Engine must provide:

  • Deterministic rollback
  • Durable execution
  • Distributed recovery
  • Replay compatibility
  • Nested transaction support
  • High availability
  • Complete auditability

The engine follows these principles:

  1. Every compensatable activity explicitly defines its compensation.
  2. Compensation never assumes database rollback.
  3. Compensation is idempotent.
  4. Compensation is event sourced.
  5. Compensation is checkpointed.
  6. Compensation is deterministic.
  7. Compensation is resumable after failures.

Workflow Runtime
Compensation Manager
┌─────────────┼──────────────┐
▼ ▼ ▼
Compensation Retry Engine Event Bus
Planner
Compensation Scheduler
Activity Workers

Workflow Running
Activity Failure
Failure Policy
Compensation Planned
Compensation Scheduled
Compensation Running
┌─────┴──────────────────────┐
▼ ▼
Rollback completed Rollback failed
(CompensationCompleted) (CompensationStepFailed)
│ │
▼ ▼
execution: Failed execution: Compensating

The execution’s own terminal state is Failed either way — that branch is about the rollback, not the workflow. A saga that rolled back cleanly did not succeed, so recording it Completed would hide it from ?status=failed and list it among the successes; CompensationCompleted in the event log is what marks the rollback itself as clean. A rollback that could not finish stays Compensating (non-terminal, by design: it needs an operator).


Each activity may define a compensation activity.

Example:

activities:
reserve_inventory:
type: function
compensate: release_inventory
charge_payment:
type: payment
compensate: refund_payment
create_shipping:
type: shipping
compensate: cancel_shipping

Completed activities are pushed onto the compensation stack.

Example:

Reserve Inventory
Charge Payment
Generate Invoice
Create Shipment
Compensation Stack
Create Shipment
Generate Invoice
Charge Payment
Reserve Inventory

Rollback executes in reverse order.


Rollback order:

Forward
A
B
C
D
Failure
Reverse
D
C
B
A

This guarantees consistency.


Supported policies:

PolicyDescription
AlwaysAlways compensate
On FailureOnly after failure
ManualUser initiated
ConditionalExpression based
NeverNo compensation

Only completed activities participate.

Example:

A ✔
B ✔
C ❌
D Not Started
Rollback
B
A

Activities that never completed are ignored.


Sub-workflows maintain independent compensation stacks.

Parent Workflow
├── Child Workflow A
└── Child Workflow B

Each child compensates independently before the parent continues.


Independent branches may compensate concurrently.

Failure
┌──────┴──────┐
▼ ▼
Refund Release Stock
│ │
└──────┬──────┘
Notify User

Dependencies determine execution order.


If compensation fails:

Rollback
Failure
Retry
Escalation
Manual Recovery

Policies determine subsequent actions.


Compensation uses the Retry Engine.

Supported strategies:

  • Fixed
  • Linear
  • Exponential
  • Exponential + Jitter

Retry history is persisted.


Generated events include:

CompensationStarted
CompensationCompleted
CompensationFailed
CompensationRetried
CompensationSkipped

All events are persisted.


States:

Pending
Scheduled
Running
┌──┴─────────────┐
▼ ▼
Completed Failed
Retrying

Stored metadata:

compensationId:
workflowId:
executionId:
activityId:
compensationActivity:
state:
attempts:
workerId:
timestamp:

Persistence occurs after every state transition.


Recovery steps:

  1. Restore checkpoint.
  2. Restore compensation stack.
  3. Replay events.
  4. Resume unfinished compensation.
  5. Continue rollback.

Recovery must be deterministic.


Replay restores:

  • Compensation stack
  • Completed compensations
  • Retry counts
  • Pending compensations

Replay never duplicates completed compensation.


Example:

activities:
reserveInventory:
compensate:
activity: releaseInventory
retry:
attempts: 5
chargePayment:
compensate:
activity: refundPayment

The Scheduler treats compensation as standard activities with elevated priority.

Default priority:

Critical

Compensation execution preempts non-critical background work.


Compensation activities enforce:

  • Authorization
  • Tenant isolation
  • Secret management
  • Immutable audit trail
  • Worker authentication

Metrics:

  • Compensation count
  • Rollback duration
  • Failed compensations
  • Retry attempts
  • Average rollback latency
  • Compensation queue depth

Every compensation event logs:

workflowId:
executionId:
activityId:
compensationId:
workerId:
status:
attempt:
duration:
timestamp:

pub trait CompensationEngine {
fn register(
&mut self,
activity: ActivityId,
compensation: ActivityId,
);
fn compensate(
&mut self,
execution: ExecutionId,
) -> Result<()>;
}

engine-workflow/
└── compensation/
├── engine.rs
├── planner.rs
├── scheduler.rs
├── stack.rs
├── recovery.rs
├── replay.rs
├── retry.rs
├── persistence.rs
├── metrics.rs
└── mod.rs

  • Compensation ordering
  • Stack management
  • Policy evaluation
  • Retry behavior
  • Workflow rollback
  • Nested workflows
  • Parallel compensation
  • Scheduler integration
  • Large rollback chains
  • Thousands of compensations
  • Concurrent rollbacks
  • Worker crash
  • Database outage
  • Scheduler restart
  • Network partition

RequirementTarget
Compensation planning< 5 ms
Rollback scheduling< 20 ms
Replay correctness100%
Recovery correctness100%
Duplicate compensation0
Audit completeness100%

  • Workflow Overview
  • Workflow DSL
  • Execution Model
  • DAG Engine
  • Scheduler
  • State Machine
  • Checkpointing
  • Retry Engine
  • Persistence
  • Event Bus
  • Distributed Execution

  • Cross-region compensation
  • Multi-cluster rollback
  • AI-assisted recovery planning
  • Automatic compensation optimization
  • Compensation simulation mode
  • Rollback cost estimation
  • Interactive rollback dashboard

VersionDateDescription
1.0.02026-06-26Initial Compensation Engine Specification