Problem
Design and implement an email sending service that uses multiple email providers (SendGrid, AWS SES, Postmark) with automatic failover. If the primary provider is down or rate-limited, the system should transparently switch to a backup provider without losing any emails.
Requirements
- Multi-Provider Support: Integrate with at least 3 email providers behind a unified interface.
- Automatic Failover: If the primary provider fails, automatically route emails through a backup provider.
- Circuit Breaker: Implement a circuit breaker per provider. After N consecutive failures (or >50% failure rate in a window), "open" the circuit and stop sending to that provider.
- Priority Routing: Route transactional emails (password reset, order confirmation) through the fastest/most reliable provider. Route marketing emails through the cheapest provider.
- Delivery Tracking: Track the delivery status of each email across providers using webhooks from each provider.
- Rate Limiting: Respect each provider's rate limits. Distribute volume across providers when approaching limits.
- Template Abstraction: Define email templates once and render them for any provider (each provider has a different template format).
Constraints
- The system sends 50,000 emails per day.
- Transactional emails must be sent within 5 seconds of being triggered.
- Marketing emails can be batched and sent over hours.
- Provider failover must happen within 10 seconds of detecting a failure.
- No email should be lost, even during provider outages (at-least-once delivery).
- The system must comply with CAN-SPAM and GDPR (unsubscribe links, consent tracking).
What to Design
- The provider abstraction layer and interface
- The circuit breaker implementation with state machine
- The failover and provider selection algorithm
- The delivery tracking unification across providers
- The template rendering pipeline