Prompt
How do I scale email sending for a SaaS app?
Latest observation
Scaling email sending for a SaaS app usually means solving for deliverability, throughput, reliability, and compliance at the same time. The right setup depends on whether you send mostly transactional email, marketing email, or both.
1) Separate email types
Use different pipelines and often different providers/configuration for:
- Transactional: password resets, receipts, alerts, invitations
- Marketing: newsletters, campaigns, drips
- Product/engagement: lifecycle emails, onboarding sequences
Why:
- Different deliverability expectations
- Different unsubscribe requirements
- Different volume patterns
- Marketing traffic can hurt transactional deliverability if mixed
2) Use a real email service provider
Don’t send directly from your app server or a raw SMTP server you manage unless you have a strong reason.
Common options:
- SendGrid
- Mailgun
- Postmark
- Amazon SES
- SparkPost
What to look for:
- SMTP + API support
- Bounce/complaint webhooks
- Dedicated IPs or IP pools
- Suppression lists
- Templating
- Rate limiting controls
- Good analytics
For many SaaS apps:
- Postmark is great for transactional
- SES is cost-effective at scale
- SendGrid/Mailgun are flexible all-around
3) Build an async email pipeline
Never send emails inline in your user request path.
Pattern:
- App creates an “email job” record
- Push job to a queue
- Worker processes jobs and sends email
- Store delivery status and provider message ID
- Process webhooks for bounce/complaint/delivery events
Typical components:
- Queue: SQS, RabbitMQ, Redis/BullMQ, Kafka, Sidekiq, Celery
- Worker service: horizontally scalable
- DB table for email jobs and status
This gives you:
- Retry handling
- Backpressure
- Decoupling from user requests
- Observability
4) Make sending idempotent
At scale, retries happen. Avoid duplicate emails by using:
- A unique job key
- Idempotency keys
- “Sent” status checks before sending
- Deduplication at the queue/job level
Example:
password_reset:user_123:token_abcinvoice_987campaign_2024_09_01:subscriber_555
5) Handle retries carefully
Not all failures are equal.
Retry:
- Temporary network issues
- 429 rate limits
- 5xx provider errors
- Timeouts
Do not retry automatically:
- Invalid recipient
- Hard bounce
- Suppressed/opt-out address
- Compliance blocks
Use:
- Exponential backoff
- Jitter
- Max retry count
- Dead-letter queue
6) Protect deliverability
This matters more than raw send capacity.
Set up:
- SPF
- DKIM
- DMARC
- Custom sending domain
- Proper reverse DNS if using dedicated IPs
- BIMI optionally
Best practices:
- Use a subdomain like
mail.yourdomain.com - Warm up new IPs gradually
- Keep complaint rates low
- Remove bounced and unsubscribed addresses
- Avoid spammy content and misleading subject lines
- Authenticate from aligned domains
7) Monitor sender reputation and metrics
Track:
- Delivered
- Bounced
- Complaints
- Opens/clicks if relevant
- Unsubscribes
- Inbox placement if you can measure it
- Queue depth
- Worker failure rates
- Provider rate limit events
Alert on:
- Complaint spike
- Bounce spike
- Sudden drop in delivery
- Queue backlog growth
- Webhook failures
8) Support batching and rate limiting
For bulk sends:
- Send in batches
- Throttle per provider limits
- Limit per domain if needed
- Schedule campaigns over time instead of blasting all at once
Example:
- 10k emails over 30–60 minutes instead of instantly
- Separate worker pools for transactional and marketing
- Per-tenant quotas if you’re multi-tenant
9) Segment by tenant/customer
In a SaaS app, especially multi-tenant:
- Enforce sending limits per tenant
- Prevent one tenant from hurting others
- Isolate tenants logically or physically if needed
- Consider separate sender domains or IP pools for large customers
10) Store templates separately
Use versioned templates and variables:
- Central template store
- Preview/testing
- Localization support
- Safe rendering to avoid injection issues
Good practices:
- HTML + plain text versions
- Template validation
- Render previews in staging
- Test with seed accounts before production
11) Add a webhook processing service
Most providers send events via webhooks:
- Delivered
- Opened
- Clicked
- Bounced
- Complaint
- Unsubscribed
Treat webhook processing as first-class:
- Verify signatures
- Make idempotent
- Update suppression lists
- Record event history
- Don’t do heavy work inline
12) Architect for compliance
Depending on audience and region:
- CAN-SPAM
- GDPR
- CASL
- PECR
- Regional consent rules
You’ll need:
- Unsubscribe links
- Physical address for marketing mail
- Consent tracking
- Data retention policies
- Preference management
13) A practical reference architecture
A simple scalable setup:
- App server
- writes email job to DB
- enqueues job
- Queue
- holds jobs for workers
- Worker fleet
- renders template
- sends via ESP API/SMTP
- updates status
- Webhook endpoint
- ingests provider events
- updates suppression and analytics
- Admin/ops dashboard
- shows send health, failures, backlog
14) Recommended rollout plan
If you’re early-stage:
- Use SES/Postmark/SendGrid
- Send asynchronously via queue
- Separate transactional and marketing
- Add SPF/DKIM/DMARC
- Store send results and webhooks
- Add retries and idempotency
- Add monitoring and alerting
- Warm up as volume grows
15) Common mistakes
- Sending synchronously in web requests
- Using one mailbox/provider for everything
- Ignoring bounces and complaints
- No unsubscribe handling
- No retry policy
- No queue backlog monitoring
- No template/version control
- Not authenticating your domain
- Overloading a new IP/domain with too much volume too fast
If you want, I can also give you:
- a reference architecture diagram,
- a database schema for email jobs, or
- a provider comparison for Postmark vs SES vs SendGrid.