Design a Notification Service
🔔 What is a notification service? It is the shared system that sends the "Your code is 4821" text, the order-shipped email and the "someone replied" push message. Product teams hand it a template name and a recipient; it fills in the text, picks the channel, queues the message, and keeps trying until the provider accepts it or it is clear that it never will.
Almost every company builds one of these, and interviewers like it because it mixes plain object design (templates, channels) with a little scheduling (priorities, retries, workers). The prompt often names many features at once: multiple tenants, email/SMS/push, queues and workers, retries, priority and templates. They are all one design. We cover each as a requirement and show where it lives, and we come back to fairness between tenants and real threads in the follow-ups.
Requirements
"Design a notification service that other teams use to send email, SMS and push notifications. It should support templates, priorities and retries."
As usual, the nouns are given and the rules are not. These questions settle the rules.
Clarifying Questions
You: Who calls this service? One product, or many?
Interviewer: Many. Think of each calling team, or each business customer if we sell it, as a tenant. Each tenant has its own templates, and one tenant must never be able to use another tenant's templates.
You: What does a template look like, and what if a value is missing?
Interviewer: Plain text with placeholders like {name}. The caller passes the values. If a placeholder has no value, reject the send right away. Never send "Hi {name}" to a customer.
You: Does a template decide its channel, or does the caller?
Interviewer: The template. The "otp" template is an SMS, the "receipt" template is an email.
You: How do priorities work? Can a promo email delay a login code?
Interviewer: No. There are three levels: high, normal and low. Always send the most urgent ready message first. Among equals, first come, first served.
You: What happens when a provider fails, say the SMS gateway is down?
Interviewer: Retry with exponential backoff: wait 10 seconds after the first failure, 20 after the second. After the third failed attempt, give up and mark it failed.
You: Is delivery synchronous? Does send wait until the email is out?
Interviewer: No. send queues the message and returns an id. Workers take messages off the queue and deliver them. Callers can check the status by id.
You: How do we model time and the workers in code?
Interviewer: Pass the time in. Model one worker step as a method call; we can talk about real threads afterwards.
The last answer shapes the whole design. A worker loop that sleeps and reads the clock is hard to test and hard to trace on a whiteboard. A process(timestamp) method that does exactly one delivery is easy to do both with, and the threaded version wraps it later.
💡 Interview tip: For anything with queues, workers or retries, ask whether you can model "one step of the worker" as a method with an explicit time. You still get to talk about threads, but your core logic stays deterministic, and your walkthrough becomes a simple table.
Final Requirements
- Tenants register templates. A template has an id, a channel (email, SMS or push) and a body with
{key}placeholders. Template ids are unique per tenant, not globally. send(tenant, template, recipient, params, priority, time)renders the template and queues a notification. It fails right away if the tenant has no such template or a placeholder has no value.- Notifications have ids
1, 2, 3, ...(failed sends use none) and a status: pending, sent or failed. - A worker step delivers the most urgent ready notification: high before normal before low, then oldest first.
- A failed delivery is retried after 10, then 20 time units. The third failure marks it failed for good. A notification waiting for a retry does not block others.
- Each channel has its own provider; one provider being down does not affect the others.
- The retry rule and the providers will change (new channels, different backoff), so they must be swappable without touching the queue logic.
Out of scope
- User preferences, opt-outs and quiet hours (see Extensibility).
- Real provider APIs, bounces and delivery receipts.
- Storage of the queue across restarts, and many servers (see /system-design/notification-service).
- Rich templates: loops, conditionals, HTML layouts, translations.