The @Retry Annotation
The@Retry annotation allows you to define custom retry policies for your tasks and workflows:
Method-Level Retry
You can also apply retry policies to individual methods:Implementing WithRetry
Create custom retry policies by implementing theWithRetry interface:
WithRetry Interface
TheWithRetry interface has a single method:
retry- The retry attempt number (0-indexed)e- The exception that caused the failure
Double- Delay in seconds before the next retry attemptnull- Stop retrying and fail the task
Common Retry Patterns
Exponential Backoff
Fixed Delay
Exception-Based Retry
Immediate Retry with Limit
Workflow Task Retry
You can manually retry a workflow’s task execution using the client:Retry Events
Infinitic emits CloudEvents for retry operations:retryScheduled- A retry has been scheduled for a failed taskretryTask- Retrying a specific taskretryExecutor- Retrying workflow executor
WithRetryBuilder
For more complex scenarios, you can useWithRetryBuilder to create retry policies dynamically:
Best Practices
Use exponential backoff for transient failures
Use exponential backoff for transient failures
Exponential backoff prevents overwhelming systems during outages and gives them time to recover.
Set maximum retry limits
Set maximum retry limits
Always set a maximum number of retries to prevent infinite retry loops.
Consider exception types
Consider exception types
Retry transient errors (network, timeout) but not permanent errors (validation, not found).
Add jitter to prevent thundering herd
Add jitter to prevent thundering herd
Add random variation to retry delays to prevent many tasks from retrying simultaneously.
Monitor retry metrics
Monitor retry metrics
Track retry rates to identify problematic services or workflows.
Related Topics
- Error Handling - Handle task and workflow failures
- Timeouts - Configure timeout behavior
- CloudEvents - Monitor retry events
- Monitoring - Track retry metrics