Overview
Cloudflare Workflows enable you to build reliable, long-running processes that survive Worker restarts, network failures, and errors. Workflows provide automatic retries, state persistence, and event-driven execution.Workflows are built on Durable Objects and provide high-level primitives for steps, retries, sleeps, and event handling.
Core Concepts
Workflow Instances
Each workflow execution is a separate instance with:- Unique ID: Identifier for tracking and introspection
- Persistent state: Survives across executions
- Status tracking: Queued, running, complete, errored, terminated
- Event queue: Receive external events during execution
Steps
Steps are the building blocks of workflows. Each step:- Executes code with automatic retries
- Caches results for idempotency
- Supports custom retry policies
- Has configurable timeouts
Workflow Lifecycle
Creating Workflows
Define Workflow Class
src/workflow.ts
Configure Binding
wrangler.json
Trigger Workflow
src/index.ts
Workflow Steps
Basic Steps
Steps automatically cache results and retry on failure:Step Configuration
number
default:"5"
Maximum number of retry attempts (0 to disable retries)
number
default:"1000"
Initial delay between retries in milliseconds
string
default:"exponential"
Retry backoff strategy:
"linear" or "exponential"string
default:"10 minutes"
Maximum step execution time (e.g.,
"30 seconds", "5 minutes", "1 hour")Retry Behavior
Exponential backoff:- Attempt 1: Immediate
- Attempt 2: 1s delay
- Attempt 3: 2s delay
- Attempt 4: 4s delay
- Attempt 5: 8s delay
- Attempt 1: Immediate
- Attempt 2: 1s delay
- Attempt 3: 1s delay
- Attempt 4: 1s delay
- Attempt 5: 1s delay
Non-Retryable Errors
Mark errors as non-retryable to immediately fail the workflow:Sleep and Timing
Sleep Duration
Pause workflow execution:Sleep Until Timestamp
Sleep until specific time:Event Handling
Wait for Events
Pause execution until an external event arrives:Send Events to Workflow
Event Patterns
User confirmation:Instance Management
Create Instance
Get Instance Status
Batch Creation
Create multiple instances at once:Advanced Patterns
Human-in-the-Loop
Saga Pattern
Compensating transactions for distributed workflows:Scheduled Workflows
Trigger workflows on a schedule:src/index.ts
wrangler.json:
Testing
Workflows can be tested using Vitest and Workers testing utilities:workflow.test.ts
Best Practices
Idempotent Steps
Design step functions to be safely retryable. Use step names as cache keys for idempotency.
Timeout Wisely
Set realistic timeouts. Default is 10 minutes, but long-running external calls may need more.
Handle Events
Always set timeouts on
waitForEvent to prevent workflows from waiting indefinitely.State Size
Keep workflow state small. Large objects in step results consume Durable Object storage.