Overview
Activities automatically retry when they fail. This is essential for handling transient failures like network timeouts, rate limits, and temporary service outages.Default Retry Behavior
Fromsrc/Activity.php:37-39,77-80, activities have unlimited retries:
- Initial attempt: immediate
- 1st retry: wait 1 second
- 2nd retry: wait 2 seconds
- 3rd retry: wait 5 seconds
- 4th retry: wait 10 seconds
- 5th retry: wait 15 seconds
- 6th retry: wait 30 seconds
- 7th retry: wait 60 seconds
- 8th+ retry: wait 120 seconds (continues indefinitely)
Custom Backoff Strategies
Override thebackoff() method to customize retry timing:
Exponential Backoff
Double the delay between each retry:Linear Backoff
Increase delay by a fixed amount:Fibonacci Backoff
Use Fibonacci sequence for organic growth:Immediate Retries with Backoff
Retry quickly at first, then slow down:Custom Backoff with Jitter
Add randomness to prevent thundering herd:Time-of-Day Aware Backoff
Adjust retries based on time:Limiting Retry Attempts
By default, activities retry indefinitely. Limit retries by overriding$tries:
backoff() should match $tries - 1.
Limiting by Time
Retry for a maximum duration:Limiting by Exception Count
Stop after a certain number of failures:Non-Retryable Exceptions
Some failures shouldn’t be retried (validation errors, resource not found, etc.):src/Activity.php:127-129, when a NonRetryableExceptionContract is thrown:
Custom Non-Retryable Exceptions
Conditional Retries
Retry only for specific exception types:Dynamic Backoff
Calculate backoff based on exception details:Retry Queue Configuration
Control which queue and connection handle retries:src/Activity.php:57-72, queue configuration is inherited from workflow options.
Monitoring Retries
Track retry behavior:Best Practices
Start Conservative
Start Conservative
Begin with short delays and increase gradually:
Consider External Rate Limits
Consider External Rate Limits
Match your backoff to API rate limits:
Add Jitter for Distributed Systems
Add Jitter for Distributed Systems
Prevent thundering herd when many workers retry simultaneously:
Use Timeout with Retries
Use Timeout with Retries
Prevent indefinite execution:
Make Activities Idempotent
Make Activities Idempotent
Ensure retries are safe:
Common Patterns
Circuit Breaker Pattern
Stop retrying if service is consistently down:Gradual Backoff
Increase delays after each failure:Scheduled Retries
Retry at specific times:Testing Retry Logic
Related Topics
- Error Handling - Exception handling strategies
- Activities - Activity basics
- Middleware - Retry middleware
- Queue Configuration - Queue settings