Retries class adds a retry policy to Modal functions, allowing you to automatically retry failed function executions with configurable backoff strategies.
Basic usage
The simplest way to add retries is to specify a maximum number of retry attempts:Custom retry policies
For more control over retry behavior, use theRetries class:
Fixed-interval retries
Retry with a constant delay between attempts:backoff_coefficient=1.0, the delay remains constant at initial_delay for all retry attempts.
Exponential backoff
Retry with exponentially increasing delays:backoff_coefficient=2.0, delays will be: 1s, 2s, 4s, 8s (up to max_delay).
Custom backoff strategy
Use different backoff coefficients for various retry patterns:Retry parameters
max_retries
The maximum number of retry attempts when a function fails:max_retries must be non-negative. Setting it to 0 means no retries will occur.initial_delay
The number of seconds that must elapse before the first retry:0 to 60 seconds.
backoff_coefficient
Controls how much the retry delay increases with each attempt:1.0 (fixed-interval) to 10.0.
max_delay
Prevents the retry delay from growing infinitely:max_delay.
Valid range: 1 to 60 seconds.
How retries work
When a function fails:- Modal checks if there are remaining retry attempts
- If yes, it waits for the calculated delay period
- The delay is calculated as:
initial_delay * (backoff_coefficient ^ (attempt - 1)) - The delay is capped at
max_delay - After the delay, the function is retried
- This continues until success or
max_retriesis exhausted
Retry delay calculation
Forinitial_delay=1.0 and backoff_coefficient=2.0:
- Attempt 1 delay:
1.0 * (2.0 ^ 0)= 1.0 seconds - Attempt 2 delay:
1.0 * (2.0 ^ 1)= 2.0 seconds - Attempt 3 delay:
1.0 * (2.0 ^ 2)= 4.0 seconds - Attempt 4 delay:
1.0 * (2.0 ^ 3)= 8.0 seconds
Common patterns
Quick retries for transient failures
Patient retries for rate limits
No retries
API reference
Retries()
The maximum number of retries in the presence of failures. Must be non-negative.
Coefficient controlling how much the retry delay increases each attempt. A value of
1.0 creates fixed-delay retries. Must be between 1.0 and 10.0.Number of seconds that must elapse before the first retry. Must be between
0 and 60 seconds.Maximum length of retry delay in seconds, preventing infinite growth. Must be between
1 and 60 seconds.Simple integer retries
For convenience, you can pass an integer directly to theretries parameter:
Maximum number of retries with default settings (1-second initial delay, 2.0 backoff coefficient).