Prompt
How do I retry failed email sends safely?
Latest observation
Nov 18, 2026
OpenAI APIWeb search: off
To retry failed email sends safely, use a durable, idempotent, queue-based approach so retries don’t create duplicates or overload your mail provider.
Recommended pattern
-
Persist the email job before sending
- Store an email record with:
id/message_id- recipient(s)
- template/data
- status:
pending | sending | sent | failed - attempt count
- last error
- This gives you a reliable source of truth.
- Store an email record with:
-
Use an idempotency key
- Generate a stable key per logical email, e.g.:
order-confirmation:order_123:user_456
- Before sending, check whether that logical email was already sent successfully.
- If your provider supports it, pass an idempotency key or dedupe token.
- Generate a stable key per logical email, e.g.:
-
Retry only transient failures
- Retry for:
- timeouts
- network errors
- 429 rate limits
- 5xx provider errors
- Do not retry for:
- invalid email address
- hard bounces
- permission/auth errors
- malformed content
- Retry for:
-
Use exponential backoff with jitter
- Example delays:
- 1 min
- 5 min
- 15 min
- 1 hour
- 6 hours
- Add jitter so many failed emails don’t retry at the exact same time.
- Example delays:
-
Make the send operation idempotent
- Ensure that if the worker runs twice, the same email isn’t sent twice.
- Common approach:
- atomically mark job as
sending - only one worker can claim it
- mark
sentonly after provider confirms success
- atomically mark job as
-
Have a dead-letter path
- After N failed attempts:
- move to
dead_letter/manual_review - alert ops or log for investigation
- move to
- Avoid infinite retries.
- After N failed attempts:
-
Track provider response IDs
- Save the provider’s message ID / transaction ID.
- This helps with deduplication, debugging, and support tickets.
-
Separate retry queues
- Put retries in a delayed queue rather than hammering the primary queue.
- This keeps your system stable under outages.
-
Be careful with side effects
- If sending the email triggers other actions, make those idempotent too.
- Example: don’t decrement inventory or mark orders paid as part of “send email.”
Minimal safe flow
- Create email job in DB
- Worker picks job atomically
- Worker sends email
- If success: mark
sent - If transient failure: increment attempts, schedule next retry with backoff
- If permanent failure or max attempts reached: mark
failedand stop
Example retry policy
- Max attempts: 5
- Retry on: 429, 500–599, timeout, connection reset
- Backoff:
base_delay * 2^attempt + jitter - Dead-letter after 5 tries
If you want, I can also show:
- a database schema for email retries,
- a queue worker example in Python/Node/Go,
- or a provider-specific strategy for SES/SendGrid/Mailgun.