Prompt
How do I set up retries for failed serverless jobs?
Latest observation
Retries for failed serverless jobs usually depend on what kind of “serverless” compute you’re using and where the failure happens. The common pattern is:
- Let the platform retry transient failures automatically
- Make your job idempotent
- Use a dead-letter queue / failure destination for permanent failures
- Add your own retry logic only when the platform doesn’t support enough control
General best practices
1) Make jobs idempotent
Retries can run the same job more than once, so your code should safely handle duplicates.
Examples:
- Use a unique job/task ID
- Check whether the work was already completed
- Write results with upserts or deduplication keys
2) Retry only transient errors
Retry things like:
- Network timeouts
- 429 rate limits
- Temporary downstream service failures
- Brief database connectivity issues
Don’t retry blindly for:
- Invalid input
- Auth failures
- Bad configuration
- Business logic errors that won’t succeed later
3) Use exponential backoff with jitter
A good retry policy is:
- 1st retry after a short delay
- Then increase the delay each attempt
- Add randomness to avoid retry spikes
Example:
- 1s, 2s, 4s, 8s with jitter
4) Set a max retry count
Avoid infinite retry loops. Common values:
- 3 retries for API calls
- 5–10 for asynchronous background jobs
5) Capture failed jobs
Send failures to:
- Dead-letter queue (DLQ)
- Failure bucket/table
- Alerting system
- Manual reprocessing queue
Platform-specific examples
AWS Lambda
If Lambda is triggered asynchronously, AWS can retry automatically.
- Asynchronous invocation: Lambda retries automatically
- You can configure:
- Maximum retry attempts
- Maximum event age
- Dead-letter queue or destination
- For event sources like SQS, retries are usually controlled by the queue and event source mapping
If you’re using Lambda + SQS:
- Configure the visibility timeout
- Set a redrive policy to move messages to a DLQ after N failures
Google Cloud Functions / Cloud Run jobs
Depending on trigger type:
- Pub/Sub-triggered functions can be retried on failure
- You can enable retry behavior on event triggers
- Use DLQ for Pub/Sub subscriptions if needed
For Cloud Run jobs:
- Build retries into your orchestration layer, or rerun failed tasks via scheduler/workflow tooling
Azure Functions
- Queue-triggered functions can retry through queue behavior and poison message handling
- Durable Functions support built-in retry policies
- You can configure retry policies for orchestrations and activities
If you control the job runner
If you’re building your own serverless worker, use a retry wrapper.
Example logic
import time
import random
def retry(fn, max_attempts=5):
delay = 1
for attempt in range(max_attempts):
try:
return fn()
except Exception as e:
if attempt == max_attempts - 1:
raise
sleep = delay + random.uniform(0, 0.5)
time.sleep(sleep)
delay *= 2
Better version:
- Catch only retryable exceptions
- Add logging
- Track attempt count
- Send final failures to a DLQ or alert
Orchestrating retries with workflows
If your job is part of a pipeline, use:
- AWS Step Functions
- Google Workflows
- Azure Durable Functions
- Temporal
- Airflow / Dagster / Prefect for more advanced workflows
These tools let you define:
- Retry count
- Backoff
- Timeout
- Catch/fallback paths
Recommended setup
A solid default architecture is:
- Serverless job triggered by queue/event
- Idempotent processing
- Automatic retry for transient errors
- Exponential backoff
- Max retry limit
- Dead-letter queue for poison messages
- Alerting on repeated failures
If you tell me which platform you’re using (AWS Lambda, Azure Functions, Cloud Functions, Cloud Run, etc.) and how the job is triggered (HTTP, queue, cron, event), I can give you the exact retry setup and config.