Prompt
building a multi tenant email sending setup
Latest observation
A solid multi-tenant email sending setup usually comes down to tenant isolation, deliverability, scaling, and observability.
Recommended architecture
1. Separate concerns
Split the system into these parts:
- API / app layer: accepts send requests
- Queue: buffers jobs per tenant
- Worker layer: sends emails asynchronously
- Provider adapter: SES, SendGrid, Mailgun, Postmark, etc.
- Tracking/analytics: logs sends, bounces, complaints, opens/clicks if needed
2. Tenant isolation model
Decide how tenants are isolated:
Option A: Shared provider account, logical isolation
- One email provider account
- Each tenant gets:
- their own sender identity
- from-domain or subdomain
- SPF/DKIM/DMARC setup
- rate limits and quotas in your app
Best for: smaller systems, simpler ops, lower cost
Option B: Per-tenant provider account
- Each tenant brings their own SES/SendGrid/etc. credentials
- You send on their behalf
Best for: enterprise/BYOK models, compliance, stronger isolation
Option C: Hybrid
- Small tenants use shared infrastructure
- Large tenants use dedicated accounts / dedicated IPs / subdomains
Best for: most SaaS platforms
3. Domain strategy
For deliverability, each tenant should ideally send from a domain or subdomain they control.
Example:
tenant-a.commail.tenant-a.comnotify.tenant-a.com
You’ll want to configure:
- SPF
- DKIM
- DMARC
- optionally custom tracking domains
If using a shared domain, use subdomains per tenant to reduce reputation blast radius.
4. Queue design
Use a queue with tenant-aware metadata:
tenant_idmessage_idtemplate_idproviderpriorityscheduled_at
Important:
- enforce per-tenant rate limits
- support dead-letter queues
- add retry with backoff
- make sends idempotent
5. Rate limiting and quotas
You need to protect both your system and provider reputation:
- max emails/minute per tenant
- daily sending quota per tenant
- global platform limits
- burst limits
- per-domain throttling
This prevents one tenant from hurting deliverability for everyone.
6. Data model
Typical tables/entities:
tenantstenant_email_settingssender_identitiesemail_messagesemail_eventssuppression_listprovider_accountsdelivery_logs
Track:
- status: queued, sent, delivered, bounced, complained, failed
- provider message ID
- tenant ID
- from/to/subject
- template/version
- timestamps
7. Bounce/complaint handling
This is critical in multi-tenant systems.
Handle:
- hard bounces
- soft bounces
- complaints
- unsubscribes
- invalid addresses
Maintain:
- global suppression list
- tenant-level suppression list
- per-sender/domain reputation metrics
8. Security and compliance
- encrypt provider credentials
- use tenant-scoped access control
- audit all send actions
- support unsubscribe compliance
- handle consent and marketing vs transactional mail separately
- if needed: GDPR/CCPA retention policies
9. Observability
Add metrics:
- send rate per tenant
- bounce rate
- complaint rate
- delivery latency
- provider error rate
- queue depth
- suppression count
Logs should always include:
- tenant ID
- request ID
- provider response
- email message ID
10. Practical implementation pattern
A common pattern:
- App receives
POST /send-email - Validate tenant + permissions
- Persist email job record
- Put job on queue
- Worker picks it up
- Resolve tenant config
- Apply rate limits / suppression checks
- Send through provider
- Store provider response
- Update status
- Process webhook events for bounce/delivery/complaint
11. Provider choice
Good options depending on needs:
- Amazon SES: cheap, scalable, more setup
- SendGrid: easier API, more features
- Mailgun: flexible, good developer experience
- Postmark: excellent for transactional email
12. If you want the safest default
For most SaaS products:
- Use shared infrastructure
- Give each tenant a custom subdomain
- Use one queue per tenant or partitioned queue
- Enforce strict per-tenant quotas
- Store all events per tenant
- Add a path for dedicated provider accounts later
If you want, I can also give you:
- a reference architecture diagram,
- a database schema, or
- a Node/Python implementation example for multi-tenant email sending.