The Retry addon provides a robust retry mechanism for handling unsuccessful network operations using an exponential backoff algorithm with jitter. It automatically retries failed operations multiple times with increasing delays between attempts to improve the chances of success.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/gofiber/fiber/llms.txt
Use this file to discover all available pages before exploring further.
Overview
The retry addon is designed to handle transient failures in network operations by:- Implementing exponential backoff with jitter to avoid thundering herd problems
- Breaking synchronization across clients to prevent collision
- Providing configurable retry behavior for different use cases
- Supporting any function that returns an error
Installation
Signatures
Usage
Basic Example
Create a retry mechanism with default configuration and retry a network request:Custom Configuration
Configure the retry behavior to match your specific requirements:Configuration
Config Structure
Default Configuration
How It Works
Exponential Backoff with Jitter
The retry mechanism uses exponential backoff with jitter:- Initial Attempt: Executes the function immediately
- First Retry: Waits
InitialInterval+ random jitter (0-1000ms) - Subsequent Retries: Multiplies the interval by
Multipliereach time - Maximum Cap: Once
MaxBackoffTimeis reached, all further retries wait this maximum duration - Jitter: Adds 0-1000ms random delay to prevent thundering herd
Retry Flow
Best Practices
Choose appropriate retry counts
Choose appropriate retry counts
Set
MaxRetryCount based on your use case:- API calls: 3-5 retries for quick failures
- Background jobs: 10-15 retries for eventual consistency
- Critical operations: Higher counts with longer backoff times
Configure timeouts
Configure timeouts
Always use reasonable timeouts in your retry function to prevent hanging:
Handle idempotency
Handle idempotency
Ensure your operations are idempotent (safe to retry):
- Use unique request IDs
- Check for duplicate operations
- Design operations to be naturally idempotent
Log retry attempts
Log retry attempts
Add logging to track retry behavior:
Use Cases
API Rate Limiting
Handle rate-limited API responses:Database Connection
Retry database connections during startup:External Service Calls
Retry calls to external services with transient failures:The retry mechanism does not sleep after the final failed attempt, ensuring quick error returns when all retries are exhausted.
Error Handling
TheRetry method returns:
nilif any retry attempt succeeds- The last error if all retry attempts fail