Overview
Workflow versioning allows you to safely deploy changes to your workflow logic while maintaining backward compatibility with running workflow instances. This is critical for long-running workflows that may span days, weeks, or months.Why Versioning Matters
When you modify workflow code, existing running instances use the workflow history to replay their state. Without versioning, code changes can cause:- Replay inconsistencies
- Unexpected behavior in running workflows
- Failed workflow executions
- Data corruption
Using getVersion
ThegetVersion() method allows you to branch your workflow logic based on version numbers:
Method Signature
Parameters
A unique identifier for this version change. Use descriptive names like
add-fraud-check or update-payment-api.The minimum version that is still supported. Workflows below this version will throw a
VersionNotSupportedException.The maximum (current) version. New workflow executions will use this version.
How It Works
From the source code atsrc/Traits/Versions.php:15-69:
- First Execution: When a workflow first executes, it logs the
maxSupportedversion - Replay: On replay, it retrieves the logged version and validates itβs within the supported range
- Version Validation: If the version is outside the
[minSupported, maxSupported]range, aVersionNotSupportedExceptionis thrown
Version Evolution Strategy
Step 1: Add New Code Path
When adding new functionality:Step 2: Wait for Old Workflows to Complete
Monitor your running workflows. Once all workflows using version 1 have completed:Step 3: Remove Old Code Path
Once safe, remove the old version:Step 4: Simplify (Optional)
After all workflows are on version 2, you can remove versioning for this change:Multiple Version Changes
You can have multiple version branches in a single workflow:Best Practices
Use Descriptive Change IDs
Use Descriptive Change IDs
Choose meaningful names that describe the change:
- β
add-fraud-check - β
migrate-to-stripe-api-v3 - β
change1 - β
update
Increment Versions Gradually
Increment Versions Gradually
Increase
maxSupported by 1 for each change:Don't Reuse Change IDs
Don't Reuse Change IDs
Each version branch should have a unique
changeId:Test Both Paths
Test Both Paths
Write tests for all version branches:
Exception Handling
If a workflow tries to use an unsupported version:Implementation Details
The versioning system (fromsrc/Traits/Versions.php:20-68):
- Checks the workflow log for an existing version entry by index
- If found during replay, validates itβs within
[minSupported, maxSupported] - If not found (first execution), logs the
maxSupportedversion - Handles race conditions using database constraints
- Returns a resolved promise with the version number
Common Patterns
Gradual Feature Rollout
API Migration
Database Schema Changes
Related Topics
- Error Handling - Handle version exceptions
- Middleware - Version-aware middleware
- Workflows - Basic workflow concepts