LOW LEVEL DESIGN · MACHINE CODING · MEDIUM · 20 OF 42

Design a Notification Service (Email, SMS, Push): LLD with Java Code

A multi-channel notification service designed and coded: email, SMS and push senders as strategies, templates rendered per channel, user preferences where security alerts cannot be muted, per-category rate limits, idempotency keys, an outbox with exponential-backoff retries and a dead-letter list, delivery events observed by an engagement tracker, thread safety and the follow-ups, with full Java 17 code.

Patterns usedObserverStrategy

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
1 / 18

Press play to hear the narration, or step through at your own pace. The full Java code is below.

Full Java solution

One file, Java 17, 364 lines, with a small main() demo. Save it as NotificationService.java, then run javac NotificationService.java && java NotificationService.

import java.util.ArrayList;
import java.util.Comparator;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.LongSupplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * A multi-channel notification service: templates rendered per channel, user preferences, per-category
 * rate limits, idempotent requests, an outbox with retries and exponential backoff, a dead-letter list,
 * channel senders as strategies, and delivery events observed by an engagement tracker.
 */
public final class NotificationService {

    // ---------- the model ----------
    enum Channel { EMAIL, SMS, PUSH }

    enum Category { TRANSACTIONAL, MARKETING, SECURITY }

    enum Status { QUEUED, SENT, RETRYING, DEAD, SKIPPED, RATE_LIMITED, OPENED, CLICKED }

    record User(String id, String name, Map<Channel, String> addresses) {}

    /** What a user wants: channels they accept, categories they muted. Security can never be muted. */
    record Preferences(Set<Channel> channels, Set<Category> muted) {
        boolean allows(Channel c, Category cat) {
            return channels.contains(c) && (cat == Category.SECURITY || !muted.contains(cat));
        }
    }

    record Template(String id, Category category, Map<Channel, String> bodies) {
        private static final Pattern VAR = Pattern.compile("\\{\\{(\\w+)\\}\\}");

        String render(Channel c, Map<String, String> vars) {
            Matcher m = VAR.matcher(bodies.get(c));
            StringBuilder out = new StringBuilder();
            while (m.find()) {
                String v = vars.get(m.group(1));
                if (v == null) throw new IllegalArgumentException(id + ": missing variable " + m.group(1));
                m.appendReplacement(out, Matcher.quoteReplacement(v));
            }
            return m.appendTail(out).toString();
        }
    }

    /** One request from a caller. The idempotency key makes a retried request harmless. */
    record Request(String idempotencyKey, String userId, String templateId, Map<String, String> vars) {}

    record Message(String id, String userId, Channel channel, String to, String body) {}

    record Event(String messageId, String userId, Channel channel, Status status, int attempt) {}

    // ---------- strategies and observers ----------
    /** Strategy: one sender per channel (an SMTP client, an SMS gateway, a push provider). */
    interface ChannelSender {
        Channel channel();

        void send(Message m); // throws on failure
    }

    /** Observer: engagement tracking, audit logs and metrics all hear every delivery event. */
    interface DeliveryListener { void onEvent(Event e); }

    /** Counts events per channel and status: sent, opened and clicked give the engagement rates. */
    static final class EngagementTracker implements DeliveryListener {
        private final Map<Channel, Map<Status, AtomicInteger>> counts = new ConcurrentHashMap<>();

        public void onEvent(Event e) {
            counts.computeIfAbsent(e.channel(), c -> new ConcurrentHashMap<>())
                    .computeIfAbsent(e.status(), s -> new AtomicInteger()).incrementAndGet();
        }

        int count(Channel c, Status s) {
            AtomicInteger n = counts.getOrDefault(c, Map.of()).get(s);
            return n == null ? 0 : n.get();
        }

        String openRate(Channel c) {
            int sent = count(c, Status.SENT);
            return sent == 0 ? "n/a" : (count(c, Status.OPENED) * 100 / sent) + "%";
        }
    }

    /** Fixed one-hour window per user, channel and category; categories without a limit are never limited. */
    static final class RateLimiter {
        private final Map<Category, Integer> perHour;
        private final LongSupplier clock;
        private final Map<String, long[]> windows = new ConcurrentHashMap<>(); // key -> {windowStart, count}

        RateLimiter(Map<Category, Integer> perHour, LongSupplier clock) {
            this.perHour = perHour;
            this.clock = clock;
        }

        boolean tryAcquire(String userId, Channel c, Category cat) {
            Integer limit = perHour.get(cat);
            if (limit == null) return true;
            long window = clock.getAsLong() / 3_600_000;
            boolean[] ok = new boolean[1];
            windows.compute(userId + "|" + c + "|" + cat, (k, w) -> { // compute is atomic per key
                if (w == null || w[0] != window) w = new long[] {window, 0};
                ok[0] = w[1] < limit;
                if (ok[0]) w[1]++;
                return w;
            });
            return ok[0];
        }
    }

    /** A message waiting in the outbox, due at a time, on its n-th attempt. */
    private record Job(Message msg, int attempt, long dueAt) {}

    // ---------- the service ----------
    private final Map<String, User> users = new ConcurrentHashMap<>();
    private final Map<String, Preferences> prefs = new ConcurrentHashMap<>();
    private final Map<String, Template> templates = new ConcurrentHashMap<>();
    private final Map<Channel, ChannelSender> senders = new EnumMap<>(Channel.class);
    private final List<DeliveryListener> listeners = new CopyOnWriteArrayList<>();
    private final Set<String> seenKeys = ConcurrentHashMap.newKeySet();
    private final PriorityBlockingQueue<Job> outbox =
            new PriorityBlockingQueue<>(64, Comparator.comparingLong(Job::dueAt));
    private final List<Message> deadLetters = new CopyOnWriteArrayList<>();
    private final Map<String, Message> sentById = new ConcurrentHashMap<>();
    private final RateLimiter limiter;
    private final LongSupplier clock;
    private final int maxAttempts;
    private final long backoffMillis;
    private final AtomicLong ids = new AtomicLong();

    NotificationService(List<ChannelSender> senderList, RateLimiter limiter, LongSupplier clock,
                        int maxAttempts, long backoffMillis) {
        for (ChannelSender s : senderList) senders.put(s.channel(), s);
        this.limiter = limiter;
        this.clock = clock;
        this.maxAttempts = maxAttempts;
        this.backoffMillis = backoffMillis;
    }

    void addUser(User u, Preferences p) {
        users.put(u.id(), u);
        prefs.put(u.id(), p);
    }

    void addTemplate(Template t) { templates.put(t.id(), t); }

    void addListener(DeliveryListener l) { listeners.add(l); }

    /** Fans one request out to every allowed channel and queues the messages. Returns how many queued. */
    int notify(Request r) {
        if (!seenKeys.add(r.idempotencyKey())) return 0; // duplicate request: already handled
        User u = users.get(r.userId());
        Template t = templates.get(r.templateId());
        if (u == null || t == null) throw new IllegalArgumentException("unknown user or template");
        Map<Channel, String> rendered = new EnumMap<>(Channel.class);
        for (Channel c : t.bodies().keySet()) rendered.put(c, t.render(c, r.vars())); // fail before queueing
        int queued = 0;
        for (Map.Entry<Channel, String> e : rendered.entrySet()) {
            Channel c = e.getKey();
            String to = u.addresses().get(c);
            Message m = new Message("m" + ids.incrementAndGet(), u.id(), c, to, e.getValue());
            if (to == null || !prefs.get(u.id()).allows(c, t.category())) publish(m, Status.SKIPPED, 0);
            else if (!limiter.tryAcquire(u.id(), c, t.category())) publish(m, Status.RATE_LIMITED, 0);
            else {
                outbox.add(new Job(m, 1, clock.getAsLong()));
                publish(m, Status.QUEUED, 1);
                queued++;
            }
        }
        return queued;
    }

    /** A worker calls this in a loop: send every job that is due, rescheduling failures with backoff. */
    int processDue() {
        int sent = 0;
        Job j;
        while ((j = takeDue()) != null) {
            try {
                senders.get(j.msg().channel()).send(j.msg());
                sentById.put(j.msg().id(), j.msg());
                publish(j.msg(), Status.SENT, j.attempt());
                sent++;
            } catch (RuntimeException e) {
                if (j.attempt() >= maxAttempts) {
                    deadLetters.add(j.msg());
                    publish(j.msg(), Status.DEAD, j.attempt());
                } else {
                    long delay = backoffMillis << (j.attempt() - 1); // 1 s, 2 s, 4 s ...
                    outbox.add(new Job(j.msg(), j.attempt() + 1, clock.getAsLong() + delay));
                    publish(j.msg(), Status.RETRYING, j.attempt());
                }
            }
        }
        return sent;
    }

    /** Peek and poll must happen together, or two workers could take the same job. */
    private synchronized Job takeDue() {
        Job head = outbox.peek();
        return head != null && head.dueAt() <= clock.getAsLong() ? outbox.poll() : null;
    }

    /** Called from a tracking pixel or a deep link. */
    void track(String messageId, Status status) {
        Message m = sentById.get(messageId);
        if (m != null) publish(m, status, 0);
    }

    List<Message> deadLetters() { return List.copyOf(deadLetters); }

    int pending() { return outbox.size(); }

    private void publish(Message m, Status s, int attempt) {
        Event e = new Event(m.id(), m.userId(), m.channel(), s, attempt);
        for (DeliveryListener l : listeners) l.onEvent(e);
    }

    // ---------- demo ----------
    /** A fake provider: records what it sent and can be told to fail the next n calls. */
    static final class FakeSender implements ChannelSender {
        private final Channel channel;
        final List<Message> sent = new CopyOnWriteArrayList<>();
        final AtomicInteger failNext = new AtomicInteger();

        FakeSender(Channel channel) { this.channel = channel; }

        public Channel channel() { return channel; }

        public void send(Message m) {
            if (failNext.getAndUpdate(n -> Math.max(0, n - 1)) > 0)
                throw new IllegalStateException(channel + " provider timeout");
            sent.add(m);
        }
    }

    static void check(boolean ok, String what) { if (!ok) throw new AssertionError(what); }

    public static void main(String[] args) throws InterruptedException {
        AtomicLong now = new AtomicLong(0);
        FakeSender email = new FakeSender(Channel.EMAIL), sms = new FakeSender(Channel.SMS);
        FakeSender push = new FakeSender(Channel.PUSH);
        RateLimiter limiter = new RateLimiter(Map.of(Category.MARKETING, 2), now::get);
        NotificationService svc =
                new NotificationService(List.of(email, sms, push), limiter, now::get, 3, 1_000);
        EngagementTracker tracker = new EngagementTracker();
        svc.addListener(tracker);
        svc.addListener(e -> {
            if (e.status() == Status.RETRYING || e.status() == Status.DEAD || e.status() == Status.SKIPPED)
                System.out.println("  [" + e.status() + "] " + e.channel() + " " + e.messageId()
                        + (e.attempt() > 0 ? " attempt " + e.attempt() : ""));
        });

        Map<Channel, String> all = Map.of(Channel.EMAIL, "customer1@example.com", Channel.SMS, "+10000000001",
                Channel.PUSH, "device-1");
        svc.addUser(new User("customer-1", "Customer", all),
                new Preferences(Set.of(Channel.values()), Set.of()));
        svc.addUser(new User("customer-2", "Shopper", Map.of(Channel.EMAIL, "customer2@example.com")),
                new Preferences(Set.of(Channel.EMAIL), Set.of(Category.MARKETING, Category.TRANSACTIONAL)));
        svc.addUser(new User("admin", "Admin", Map.of(Channel.SMS, "+10000000009")),
                new Preferences(Set.of(Channel.SMS), Set.of()));
        svc.addTemplate(new Template("order_shipped", Category.TRANSACTIONAL, Map.of(
                Channel.EMAIL, "Hi {{name}}, order {{order}} has shipped. Track it in the app.",
                Channel.SMS, "Order {{order}} shipped.",
                Channel.PUSH, "Your order {{order}} is on its way")));
        svc.addTemplate(new Template("sale", Category.MARKETING, Map.of(
                Channel.EMAIL, "Hi {{name}}, the weekend sale starts now.",
                Channel.PUSH, "Weekend sale is live")));
        svc.addTemplate(new Template("login_alert", Category.SECURITY, Map.of(
                Channel.EMAIL, "New sign-in to your account from {{device}}.",
                Channel.SMS, "New sign-in from {{device}}.")));

        // 1. Fan-out to three channels; SMS provider fails twice, retries with 1 s then 2 s backoff.
        sms.failNext.set(2);
        Map<String, String> vars = Map.of("name", "Customer", "order", "A-1001");
        Request ship = new Request("ship-A-1001", "customer-1", "order_shipped", vars);
        System.out.println("Queued " + svc.notify(ship));
        System.out.println("Duplicate request queued " + svc.notify(ship)); // a client retry
        svc.processDue();
        now.addAndGet(1_000);
        svc.processDue();
        now.addAndGet(2_000);
        svc.processDue();
        System.out.println("Email: " + email.sent.get(0).body());
        System.out.println("SMS after retries: " + sms.sent.get(0).body());
        check(email.sent.size() == 1 && sms.sent.size() == 1 && push.sent.size() == 1, "one per channel");

        // 2. Preferences: customer-2 muted marketing and transactional, but security always goes through.
        svc.notify(new Request("sale-c2", "customer-2", "sale", Map.of("name", "Shopper")));
        svc.notify(new Request("login-c2", "customer-2", "login_alert", Map.of("device", "a new laptop")));
        svc.processDue();
        System.out.println("customer-2 got: " + email.sent.get(email.sent.size() - 1).body());

        // 3. Rate limit: at most 2 marketing messages per channel per hour.
        for (int i = 0; i < 4; i++)
            svc.notify(new Request("sale-" + i, "customer-1", "sale", Map.of("name", "C")));
        svc.processDue();
        System.out.println("4 sale requests: push sent " + tracker.count(Channel.PUSH, Status.SENT)
                + " total, " + tracker.count(Channel.PUSH, Status.RATE_LIMITED) + " rate limited");
        check(tracker.count(Channel.PUSH, Status.RATE_LIMITED) == 2, "rate limit");

        // 4. A provider that keeps failing: three attempts, then the dead-letter list.
        sms.failNext.set(10);
        svc.notify(new Request("login-admin", "admin", "login_alert", Map.of("device", "an unknown phone")));
        for (int i = 0; i < 3; i++) {
            svc.processDue();
            now.addAndGet(4_000);
        }
        System.out.println("Dead letters: " + svc.deadLetters().size());
        check(svc.deadLetters().size() == 1, "dead letter");
        sms.failNext.set(0);

        // 5. A missing template variable fails before anything is queued.
        try {
            svc.notify(new Request("bad", "customer-1", "order_shipped", Map.of("name", "C")));
        } catch (IllegalArgumentException e) {
            System.out.println("Rejected: " + e.getMessage());
        }

        // 6. Engagement: the shipped email was opened and clicked.
        svc.track(email.sent.get(0).id(), Status.OPENED);
        svc.track(email.sent.get(0).id(), Status.CLICKED);
        System.out.println("Email sent " + tracker.count(Channel.EMAIL, Status.SENT) + ", opened "
                + tracker.count(Channel.EMAIL, Status.OPENED) + ", open rate "
                + tracker.openRate(Channel.EMAIL));

        // 7. Eight threads send 400 requests, each one twice; four workers drain the outbox.
        int emailsBefore = email.sent.size();
        ExecutorService pool = Executors.newFixedThreadPool(8);
        CountDownLatch done = new CountDownLatch(8);
        for (int t = 0; t < 8; t++) {
            int lane = t % 4;
            pool.submit(() -> {
                for (int k = lane; k < 400; k += 4)
                    svc.notify(new Request("order-" + k, "customer-1", "order_shipped",
                            Map.of("name", "C", "order", "B-" + k)));
                done.countDown();
            });
        }
        done.await();
        CountDownLatch drained = new CountDownLatch(4);
        for (int w = 0; w < 4; w++) {
            pool.submit(() -> {
                svc.processDue();
                drained.countDown();
            });
        }
        drained.await();
        pool.shutdown();
        int newEmails = email.sent.size() - emailsBefore;
        System.out.println("Concurrent: 800 requests (400 unique) -> " + newEmails + " emails, pending "
                + svc.pending());
        check(newEmails == 400 && svc.pending() == 0, "exactly once per unique request");
    }
}

Notes

Requirements

The requirements. A caller sends one request: a user, a template and variables. The service fans it out to email, SMS and push, with text written for each channel. It respects the user's preferences, except that security alerts always go out. Marketing is rate-limited per user. Failed sends are retried with backoff and finally parked in a dead-letter list. A caller retrying the same request must not cause a second message. And we track sends, opens and clicks.

Clarify. Notify does not wait for delivery; it queues the work and returns, because providers can take seconds. The guarantee is at least once, with idempotency keys removing duplicate requests. Three categories: transactional, marketing and security. Marketing is limited to two per channel per hour. Three attempts per message, waiting one second and then two. And a template with a missing variable is rejected before anything is queued.

Requirements
  • notify(user, template, vars)
  • channels: email, SMS, push
  • one template, text per channel
  • respect user preferences
  • security alerts always sent
  • rate-limit marketing per user
  • retry failures with backoff,
  • then a dead-letter list
  • a retried request sends once
  • track sent, opened, clicked
Clarify first
  • does notify() wait for delivery?
  • no: queue it, return at once
  • delivery guarantee?
  • at least once, deduped by key
  • which categories?
  • transactional, marketing,
  • security
  • limits?
  • 2 marketing per channel per hour
  • retries? 3 attempts: 1 s, 2 s
  • missing variable? reject early

Entities

The entities. The service sits in the middle. Templates hold text per channel. Preferences say which channels a user accepts and which categories they muted. The rate limiter counts messages per user, channel and category. Idempotency keys remember requests already handled. The outbox holds messages waiting to be sent. Channel senders talk to the providers, and delivery listeners hear about everything that happens.

Class diagram

The model. Channels and categories are enums. A request carries an idempotency key, the user, the template and the variables. Fan-out turns one request into several messages, one per channel, each with its own id, address and rendered body. Every step produces an event with a status: queued, sent, retrying, dead, skipped, rate limited, opened or clicked. All of these are immutable records, so they can be passed between threads freely.

Model
enum Channel { EMAIL, SMS, PUSH }
enum Category { TRANSACTIONAL,
                MARKETING, SECURITY }
record Request(String idempotencyKey,
    String userId, String templateId,
    Map<String, String> vars)
record Message(id, userId, channel,
    to, body)
record Event(messageId, userId,
    channel, Status status, attempt)

Templates

Templates. One template has a body per channel, because an email can be friendly and long while an SMS must be short. Rendering replaces each variable in double braces. All channels are rendered before anything is queued, so a missing variable rejects the whole request instead of sending the email and failing the SMS. In the demo, forgetting the order number is rejected with a clear message naming the template and the variable.

order_shipped
EMAIL: Hi {{name}}, order {{order}}
       has shipped. Track it in the app.
SMS:   Order {{order}} shipped.
PUSH:  Your order {{order}} is on
       its way

render: replace each {{var}};
  missing var -> throw, nothing sent
"order_shipped: missing variable
 order"

Preferences

Preferences. Each user lists the channels they accept and the categories they muted. The rule lives in one method on the preferences record: the channel must be accepted, and the category must not be muted, unless it is security. A sign-in alert must reach you even if you turned off everything else. In the demo, the second customer muted marketing and transactional messages, so the sale is skipped but the login alert still arrives by email.

Preferences
record Preferences(Set<Channel> chans,
                   Set<Category> muted) {
  boolean allows(Channel c, Category k) {
    return chans.contains(c)
      && (k == SECURITY
          || !muted.contains(k));
  }
}
customer-2 muted marketing:
  sale skipped, login alert sent

Strategy: channels

Each channel is a Strategy behind one interface: it names its channel and sends a message, throwing on failure. The service keeps them in an enum map and picks one by the message's channel, so there is no switch statement on channel anywhere in the service. Adding a chat app channel means one enum value and one sender class. In tests, a fake sender records what it sent and can be told to fail its next few calls.

ChannelSender
interface ChannelSender {
  Channel channel();
  void send(Message m); // throws
}
EmailSender: SMTP or an email API
SmsSender:   an SMS gateway
PushSender:  a push provider

Map<Channel, ChannelSender> senders
  = new EnumMap<>(Channel.class)
new channel: one class + one enum

Flow: notify

The notify flow. First add the idempotency key to a concurrent set; if it was already there, this is a retry of a request we have handled, so return zero. Render every channel. Then for each channel: if the user has no address there or the preferences forbid it, record skipped; if the limiter says no, record rate limited; otherwise put a job in the outbox, due now, and record queued. The demo queues three messages, and the identical retry queues none.

notify(request)
if (!seenKeys.add(key)) return 0
render every channel (may throw)
for each channel:
  no address or !prefs.allows
    -> SKIPPED
  !limiter.tryAcquire
    -> RATE_LIMITED
  else outbox.add(job due now)
    -> QUEUED
Queued 3; duplicate queued 0

Rate limits

Rate limits protect users from being spammed. Each category has an hourly limit, and only marketing has one here; a shipping update or a security alert should never be dropped for being too frequent. The key is user, channel and category, and a fixed one-hour window counts messages. The check and the increment happen inside one atomic compute call on a concurrent map, so two threads can never both take the last slot. Four sale requests send two and limit two.

RateLimiter
Map<Category, Integer> perHour
  MARKETING -> 2, others: no limit

key = user|channel|category
windows.compute(key, (k, w) -> {
  new hour? w = {hour, 0}
  ok = w.count < limit; if ok count++
  return w
})
4 sale requests -> 2 sent, 2 limited

Flow: deliver and retry

Delivery. Workers call process-due in a loop. The outbox is a priority queue ordered by due time, so the next job to send is always at the head. A worker takes each due job and sends it. On failure, if attempts remain, the job goes back in with a later due time, one second, then two, then four. After the third attempt it moves to the dead-letter list for a human or a later replay. In the demo the SMS provider fails twice and the third attempt succeeds.

processDue()
while ((j = takeDue()) != null)
  try { sender(j).send(j.msg)
        -> SENT }
  catch (RuntimeException e)
    attempt == 3 -> deadLetters, DEAD
    else re-add with dueAt =
      now + 1000 << (attempt - 1)
      -> RETRYING

outbox: PriorityBlockingQueue by dueAt

Observer: engagement

Engagement tracking is the Observer pattern. The service publishes an event for every status change, and the tracker is just one listener, counting events per channel and status. Opens come from a tracking pixel and clicks from a redirect link, both calling track with the message id. In the demo, four emails were sent and one was opened, a twenty-five percent open rate. An audit log, a metrics exporter or an alert on dead letters are more listeners, with no change to the service.

EngagementTracker
  • listens to every event
  • counts per channel and status
  • track(id, OPENED) from a pixel
  • track(id, CLICKED) from a link
  • Email sent 4, opened 1,
  • open rate 25%
  • other listeners: audit log,
  • metrics, alerting on DEAD

Concurrency

Concurrency. The idempotency set's add is atomic, so when two threads submit the same key, exactly one wins. The limiter uses compute per key. The outbox is a blocking priority queue, but peeking at the head and polling it are two steps, so take-due is synchronized; otherwise two workers could both see the same due job. In the demo, eight threads send eight hundred requests, every key twice, four workers drain the outbox, and exactly four hundred emails go out.

Thread safety
  • seen keys: ConcurrentHashMap set
  • add() is atomic: one winner
  • limiter: compute() per key
  • outbox: PriorityBlockingQueue
  • takeDue(): synchronized peek+poll
  • so two workers never share a job
  • listeners: CopyOnWriteArrayList
  • 8 threads, 800 requests, 400
  • unique -> exactly 400 emails

Extensibility

Extensions. A new channel is one sender class. Quiet hours delay a message to the morning by setting its due time, rather than dropping it. Low-priority messages can be batched into a daily digest. If push fails permanently, fall back to SMS. Give security its own high-priority lane. Store templates per locale. And in production the outbox must be durable, a database table or a message queue, so a restart never loses a message.

Extension points
  • new channel: chat app, webhook
  • quiet hours: delay, do not drop
  • digest: batch low-priority ones
  • channel fallback: push failed?
  • try SMS (chain of strategies)
  • priority lanes: security first
  • per-locale templates
  • durable outbox: a database table
  • or a message queue

Testing

The full code is below the player. Fake senders record messages and fail on command, and a fake clock moves the retry timers. The demo fans one order out to three channels and retries SMS twice, ignores a duplicate request, skips a muted sale but delivers a login alert, rate-limits marketing, dead-letters a message after three failures, rejects a missing variable, computes an open rate, and finally proves exactly-once sending under concurrent duplicate requests.

Run it
Queued 3
Duplicate request queued 0
  [RETRYING] SMS m2 attempt 1
  [RETRYING] SMS m2 attempt 2
SMS after retries: Order A-1001 shipped.
customer-2 got: New sign-in to your..
4 sale requests: 2 rate limited
  [DEAD] SMS m17 attempt 3
open rate 25%
800 requests (400 unique) -> 400

Follow-ups

Expect follow-ups. Exactly once is not possible across a provider, so aim for at least once with idempotency keys and provider-side deduplication. A provider down for an hour? Put a circuit breaker per provider and stop burning retries. A campaign to millions of users? Partition the work through a message queue to many workers. Delivery receipts come back by webhook as more events. And re-check preferences at send time, in case the user unsubscribed while the message waited.

Follow-ups to expect
  • at most once vs at least once?
  • at least once + provider dedupe
  • a provider is down for an hour?
  • circuit breaker per provider
  • millions of users per campaign?
  • partition, queue, many workers
  • did the SMS arrive?
  • delivery receipts by webhook
  • user unsubscribes mid-campaign?
  • check preferences at send time

Say it like this

The short version. Notify dedupes the request by its key, renders every channel up front, then for each channel checks preferences and the rate limit and queues a job in an outbox ordered by due time. Each channel is a sender strategy. Workers send due jobs, retry failures with exponential backoff, and dead-letter after three attempts. Every status change is an event, and engagement tracking is one observer of those events.

In the interview
  • "notify() dedupes by key, renders
  • every channel first, then per
  • channel checks preferences and a
  • rate limit and queues a job. One
  • sender strategy per channel.
  • Workers retry with backoff, then
  • dead-letter. Every status change
  • is an event for observers."

Interview questions

How do you support email, SMS and push without a big switch statement?

Strategy pattern: a ChannelSender interface with one implementation per channel, stored in an EnumMap keyed by Channel. The service looks up the sender by the message channel, so a new channel is one enum value and one class.

How do you avoid sending a notification twice when a caller retries?

Every request carries an idempotency key. The service adds it to a concurrent set before doing any work; if add returns false the request was already handled and is ignored. In production the set lives in a store with an expiry, such as a database unique key or a cache.

How do user preferences work?

Each user has accepted channels and muted categories. A single allows(channel, category) method checks both, with security alerts exempt from muting. Preferences are checked when the message is created and ideally again at send time, in case they changed while it waited.

How do you rate-limit notifications?

Keep an hourly limit per category, applied per user and channel, usually only to marketing. A fixed or sliding window counter keyed by user, channel and category is checked and incremented atomically, for example with ConcurrentHashMap.compute.

How do retries and dead letters work?

Messages wait in an outbox ordered by due time. A failed send is re-queued with an exponentially growing delay; after the maximum attempts it moves to a dead-letter list that operators can inspect and replay, and an alert listener can fire.

Where does the Observer pattern fit?

Every status change, queued, sent, retrying, dead, skipped, rate limited, opened or clicked, is published as an event. Engagement tracking, audit logs and metrics are independent listeners, so adding one never changes the delivery code.

How do multiple workers avoid sending the same message?

The outbox is a thread-safe priority queue, and taking a due job, peeking at the head and polling it, happens inside one synchronized method. With a database outbox the same idea is a row lock, such as SELECT FOR UPDATE SKIP LOCKED.

How would you scale this to millions of notifications?

Make the outbox a durable message queue partitioned by user, run many stateless workers per channel, put a circuit breaker and rate limit in front of each provider, and batch sends where the provider allows it.