Skip to main content
This guide covers how to handle errors that occur during workflow execution. You can safely handle failures caused by external API calls, network issues, temporary outages, and implement automatic retry patterns when necessary.

Basic Error Handling

Errors in workflows are handled with standard try-catch statements. Log the error and re-throw it if necessary to put the workflow in a failed state.
Key Concepts:
  • Log errors with logger.error()
  • Fail the workflow with throw error
  • Failed workflows are automatically retried by the Worker

Step-Level Error Handling

Not all steps are required. Some steps can fail without stopping the workflow. Wrap these optional steps in try-catch to absorb errors.
Use Cases:
  • Cache failure (data must still be saved)
  • Notification failure (order must still complete)
  • Logging failure (business logic continues)

Retry Patterns

Manual Retry

External API or network requests can fail temporarily. In such cases, multiple retries can improve success rates.
Retry Strategy:
  1. Maximum 3 attempts
  2. 5 second wait between attempts
  3. Throw error if all attempts fail

Exponential Backoff

Exponential Backoff gradually increases the delay between retries, preventing server overload while improving retry success rates.
Backoff Intervals:
  • 1st failure -> 2 second wait
  • 2nd failure -> 4 second wait
  • 3rd failure -> 8 second wait
  • 4th failure -> 16 second wait
Benefits:
  • Provides time to recover from temporary overload
  • Distributes server load
  • Improves retry success rate

Compensating Transactions

In distributed transactions, when some operations fail, already completed operations must be rolled back. This is called a Compensating Transaction.
Compensating Transaction Pattern:
  1. Track completion status of each operation
  2. Check completed operations when failure occurs
  3. Cancel operations in reverse order
  4. Re-throw the error
Practical Uses:
  • Payment refund
  • Inventory restoration
  • Reservation cancellation
  • File deletion

Timeout Handling

If an external API doesn’t respond, the workflow could wait indefinitely. Set a timeout to fail after a certain duration.
Timeout Strategies:
  • Short timeout (5-10 seconds): Fast APIs
  • Medium timeout (30-60 seconds): Standard APIs
  • Long timeout (5-10 minutes): File processing

Error Type-Based Handling

Use different handling strategies based on error types. Network errors should be retried, but data validation errors should fail immediately.
Error Classification:

Practical Examples

1. Email Sending with Dead Letter Queue

If email sending fails 3 times, add it to a Dead Letter Queue for manual processing later.
DLQ Pattern:
  • 3 retry failures
  • Add to DLQ (process later)
  • Notify administrator

2. API Call with Circuit Breaker

When an external API keeps failing, protect the system by blocking requests with a Circuit Breaker.
Circuit Breaker States:
  • Closed: Normal operation
  • Open: Blocked after 5 failures (for 1 minute)
  • Half-Open: Retry after 1 minute

3. File Upload with Partial Retry

When uploading multiple files, retry each file independently so that some failures don’t prevent others from succeeding.
Partial Retry Strategy:
  • Each file has independent steps
  • 3 retries per file
  • Workflow succeeds even if some fail
  • Returns list of failed files

Important Notes

Error Handling Best Practices:
  1. Error Logging: Always log errors to enable problem tracking.
  2. Retry Limits: Prevent infinite retries by setting a maximum count.
  3. Compensating Transactions: Clean up completed operations on failure.
  4. Timeout Settings: Prevent infinite waits.
  5. Dead Letter Queue: Store final failures separately for manual processing.
  6. Error Type Distinction: Differentiate between retryable and non-retryable errors.

Next Steps

@workflow Decorator

Learn workflow definition and scheduling

Step

Divide and manage tasks with Steps

Worker Setup

Configure the Worker process