Prompt
How do I handle email retries in a SaaS?
Latest observation
A good way to handle email retries in a SaaS is to treat email sending as an asynchronous, durable job with clear retry rules, idempotency, and provider-aware failure handling.
Here’s a practical approach:
1) Don’t send emails inline
When your app needs to send an email, write an event/job to a queue or outbox table first, then process it asynchronously.
Why:
- avoids slowing down user requests
- survives transient outages
- gives you a place to track retries, status, and failures
2) Classify failures
Not all email failures should be retried.
Retryable failures
These are usually temporary:
- SMTP timeout
- DNS/network issues
- 429 rate limits
- provider 5xx errors
- temporary deferrals / greylisting
Non-retryable failures
These should generally stop immediately:
- invalid recipient address
- hard bounce
- blocked sender/domain
- malformed message
- permission/auth failure due to bad config
- unsubscribed/complained recipient, depending on your rules
3) Use exponential backoff with jitter
Retry transient failures with increasing delays, e.g.:
- 1 min
- 5 min
- 15 min
- 1 hour
- 6 hours
Add jitter so many jobs don’t retry at once.
Example:
- attempt 1: immediate
- attempt 2: after 2–5 min
- attempt 3: after 10–20 min
- attempt 4: after 1–2 hours
4) Set a max retry limit
Don’t retry forever.
Typical pattern:
- max attempts: 5–8
- max age: 24–72 hours
After that:
- mark as permanently failed
- alert ops or create a dead-letter queue entry
5) Make sending idempotent
Retries can cause duplicate emails if the provider accepted the message but your app didn’t get the response.
Use an idempotency strategy such as:
- internal message ID
- deduplication key stored in your DB
- provider idempotency key if supported
Before sending, check whether that message was already marked as sent.
6) Track status lifecycle
Store states like:
queuedsendingsentretryingdelivered(if provider gives webhook confirmation)bouncedcomplainedfailed_permanentdead_lettered
This helps with observability and support.
7) Separate delivery from bounce handling
A successful API response only means the provider accepted the email, not that it reached the inbox.
Use provider webhooks for:
- bounce
- complaint
- spam report
- deferred
- delivered
Update your retry logic based on those events.
8) Use a dead-letter queue or failure table
For messages that exceed retry limits:
- move them to a dead-letter queue
- store the reason, last error, attempts, and timestamps
- surface them in an admin/support dashboard
9) Respect provider and recipient rules
Retrying too aggressively can hurt deliverability.
Be careful with:
- rate limits
- domain reputation
- complaint rates
- unsubscribes
- sending to invalid or stale addresses
Sometimes you should suppress retries entirely for certain domains or failure patterns.
10) Monitor and alert
Track:
- send success rate
- retry rate
- bounce rate
- complaint rate
- median time to send
- dead-letter volume
Alert when:
- retry volume spikes
- provider errors increase
- queue backlog grows
- delivery rates drop
A simple implementation pattern
A common architecture:
- App writes email job to
email_outbox - Worker picks up job
- Worker attempts send via provider
- On transient failure:
- increment attempt count
- set
next_retry_at
- On permanent failure:
- mark failed
- On success:
- mark sent
- Webhooks update bounce/complaint/delivery status
Example retry policy
- Retry on: timeouts, 429, 5xx, temporary deferrals
- Stop on: 4xx permanent errors, invalid email, auth errors, bounce
- Max attempts: 6
- Backoff: exponential with jitter
- Max retry window: 48 hours
Extra SaaS considerations
- Tenant isolation: one noisy tenant shouldn’t exhaust queue capacity for everyone else
- Priority classes: password reset emails may get higher priority than marketing emails
- Compliance: honor unsubscribe, GDPR/CCPA, and suppression lists
- Multi-provider fallback: if one provider is down, fail over carefully, but avoid duplicate sends
If you want, I can also give you:
- a database schema for email retries,
- a retry algorithm/pseudocode, or
- an example using AWS SES / SendGrid / Postmark.