Design a notification service
medium 45 minOne service that every team calls to reach a user by push, email, SMS or inside the app.
See it run
Every flow, one hop at a time: a push, a one-time code, the lanes, retries, a campaign and the life of a device token.
Operations
Push: order shipped
A transactional push, from the producer's call to the status event.
Step 1 of 14
The producer sends a request
template
order_shipped
priority
high
user
u_12345
The order service names a user, a template and some data. Why: it does not need to know about push or email.
Step 1: The producer sends a request
def send(req): auth(req); limit(req.producer); check_shape(req) prefs = preferences.get(req.user) # cached 5 min if not prefs.allows("push", req.category): return 202, "dropped" body = render(req.template, req.data, prefs.locale) log.publish("push.high", req.user, body, acks="all") return 202, req.iddef worker(msg): if not preferences.allows(msg): return for device in registry.active(msg.user): if claims.take(msg.key, "push", device): # put if absent status = apns.send(device.token, msg) events.publish(msg.id, device, status)- Latency
- about 2 s
- Load
- 2,300/s high
- Producer waits
- about 50 ms
- Target
- < 30 s
Everything before the 202 must be quick and certain. Everything after it may be slow and may be repeated.
In the real world
The same notification service doing real jobs. Pick one and step through it.
Codes must still arrive.
Step 1 of 4
Errors rise
- errors
- 50%
Half of the calls to provider A fail.
Step 1: Errors rise
Common mistakes
The bugs interviewers watch for, with the fix beside each.
One queue with a priority field
criticalA log is read in order, so the code waits for everything in front of it.
Wronglog.publish("notifications", msg, priority="critical")Rightlog.publish(f"{channel}.critical", msg)# with workers and quota that only this lane usesSending before claiming
criticalTwo workers can hold the same message, and a crash after the send repeats it.
Wrongprovider.send(msg)db.mark_sent(msg.id)Rightif claims.put_if_absent(key, lease=60): provider.send(msg) claims.extend(key, hours=24)Letting the caller set the bypass
majorSomeone will set it on an advert.
Wrongif req.bypass_preferences: send(req)Rightif req.template in REVIEWED_SKIP_LIST: audit(req) send(req)Retrying a code for hours
majorIt expired long ago, and an old code confuses the user.
Wrongretry(msg, delays=[60, 300, 1800, 7200, 43200])Rightwhen = now + delayif when >= msg.deadline: give_up(msg)Unsubscribing on GET
majorMail scanners open links. People lose mail they wanted.
WrongGET /unsubscribe?t=... # takes effectRightList-Unsubscribe-Post: List-Unsubscribe=One-ClickPOST /unsubscribe?t=... # takes effect
Interview tips
Start with urgency
- "A code must never wait behind marketing."
- Name four priorities with a time for each.
- Critical is only 2%, so reserve capacity.
Show independence
- Email can fail while push works.
- Lanes by channel and priority.
- Different retry rules for each channel.
Mention the user
- Opt-out, quiet hours, caps, digest.
- One-click unsubscribe.
- Preferences are checked twice.
Fail well, and see it
- Breaker, second provider, dead letters.
- Deadlines on retries.
- Alarms on lane lag and error rate.