Prompt
How do I handle retries and rate limits for outgoing email?
Latest observation
Handling retries and rate limits for outgoing email is mostly about being polite, resilient, and observant. A good approach depends on whether you’re sending through an ESP like SES/SendGrid/Mailgun or directly via SMTP, but the principles are the same.
1) Classify failures correctly
Treat email send failures differently based on the error:
-
Transient errors: retryable
Examples:- 4xx SMTP responses
- timeouts
- temporary provider outages
- connection resets
-
Permanent errors: do not retry
Examples:- invalid recipient address
- rejected domain
- policy/permission errors
- 5xx SMTP responses that indicate a hard failure
If you’re using SMTP:
- 4xx = temporary problem → retry
- 5xx = permanent problem → stop and log
2) Use exponential backoff with jitter
Don’t retry immediately or on a fixed interval. Use:
- exponential backoff: 1s, 2s, 4s, 8s, ...
- add jitter to spread load and avoid synchronized retries
Example strategy:
- Retry after 1s
- then 2s
- then 4s
- then 8s
- cap at something like 1–5 minutes
- stop after a max number of attempts, e.g. 5–8 retries
A common formula:
delay = min(base * 2^attempt, maxDelay) + random(0, jitter)
3) Respect provider rate limits
Providers often impose limits on:
- messages per second
- recipients per minute
- connection count
- daily quotas
Best practices:
- read provider docs for exact limits
- throttle sending globally and per domain if needed
- queue outgoing mail and process at a controlled rate
- implement backpressure so bursts don’t overwhelm the provider
If the provider returns a rate-limit response:
- pause sending
- retry after the suggested
Retry-Aftertime if provided - otherwise use backoff
4) Queue email sends asynchronously
Avoid sending email inline in the user request path if possible.
Recommended pattern:
- App creates an email job
- Job goes into a queue
- Worker sends email
- Worker retries transient failures
- Worker marks permanent failures as dead-lettered/failed
This gives you:
- controlled throughput
- better retry handling
- less user-facing latency
- easier observability
5) Make sends idempotent
Retries can cause duplicate emails if the original actually succeeded but the response was lost.
To reduce duplicates:
- generate an internal message ID
- store send status before/after send
- ensure workers don’t process the same job twice
- deduplicate at the application level if possible
For transactional emails, duplicates can be painful, so it’s worth adding safeguards.
6) Separate concerns: validation, sending, and bounce handling
Before sending:
- validate address format
- optionally do domain/MX checks
- prevent obviously invalid sends
After sending:
- process bounces, complaints, and unsubscribes
- suppress addresses that hard-bounce
- stop sending to recipients who complained or unsubscribed
This reduces repeated failures and improves reputation.
7) Monitor deliverability and failure patterns
Track:
- send success rate
- transient vs permanent failure counts
- retry counts
- queue depth
- provider rate-limit events
- bounce rate
- complaint rate
Alerts to consider:
- spike in 4xx errors
- provider throttling
- queue backlog growing
- sudden increase in bounces
8) Use provider-specific guidance
Different providers have different policies:
- some want gradual ramp-up for new accounts/domains
- some limit concurrent SMTP connections
- some recommend dedicated IP warmup
- some provide API-based sending with better retry semantics than SMTP
If available, use:
- provider SDKs
- webhooks for bounces/complaints
- rate-limit headers or retry-after instructions
9) Practical retry policy example
A reasonable default:
- retry only on transient errors/timeouts
- max attempts: 5
- backoff: 1s, 2s, 4s, 8s, 16s
- jitter: random 0–50% of delay
- cap delay at 60s
- dead-letter after max attempts
- alert on repeated failures for the same recipient or domain
10) Pseudocode example
def send_with_retry(email_job):
max_attempts = 5
base_delay = 1
max_delay = 60
for attempt in range(max_attempts):
try:
send_email(email_job)
mark_sent(email_job)
return
except TemporaryEmailError as e:
delay = min(base_delay * (2 ** attempt), max_delay)
delay += random.uniform(0, delay * 0.5) # jitter
log_retry(email_job, attempt, e, delay)
sleep(delay)
except PermanentEmailError as e:
mark_failed(email_job, e)
return
mark_dead_letter(email_job)
11) A good operational rule of thumb
- Don’t retry forever
- Don’t retry immediately
- Don’t ignore rate-limit signals
- Do queue and throttle
- Do suppress hard failures
- Do monitor everything
If you want, I can also give you:
- a Node.js implementation with retries and backoff,
- a Python implementation, or
- a design for a queue-based email worker.