Design a notification service
medium 85 minDesign the classes that tell people things by push, text message and email: routes from personal wishes, quiet hours, retries, fallback to another channel, and protection against repeats.
Video lesson · Chapter 1 of 6
Make a prediction
See it run
One notice on its way: calls travel, fields change, and the matching lines light up.
Operations
Accept
Route, words, key, queue. No vendor is asked.
Step 1 of 5
A code for Asha
The shop says what happened: login-7, a code. It does not say how to send it.
Step 1: A code for Asha
def notify(self, notice): profile = self._profiles[notice.user_id] stops = tuple( Stop(via, profile.addresses[via], self._templates.render(...)) for via in profile.routes(notice.kind) ) with self._lock: if notice.key in self._by_key: return delivery = Delivery(notice, stops) self._by_key[notice.key] = delivery self._push(delivery, self._now())- Cost
- O(log n) for the heap
- Guarantee
- Stored, or an error now
- Sending here
- the buyer waits for a vendor
Everything that can fail comes before the first write.
In the real world
The same notification service doing real jobs. Pick one and step through it.
Noor switches offers off while one waits.
Step 1 of 4
23:00
The offer is accepted. Noor wanted offers.
Step 1: 23:00
Common mistakes
The bugs interviewers watch for, with the fix beside each.
A class for each channel and level
majorThree channels and two levels are six leaves, and the next question doubles them. One event becomes unrelated objects.
Wrongclass UrgentSmsNotification(SmsNotification): ...RightNotice(key, user, Kind.CODE) # urgent is dataStop(Via.SMS, address, message) # the channel is a stopSending under the lock
criticalOne slow vendor stops every accept and every worker.
Wrongwith self._lock: job = heappop(self._due) sender.send(...)Rightwith self._lock: job = heappop(self._due)sender.send(...) # nobody waits for itOne except for everything
criticalA dead number is tried for an hour, a busy server is given up at once.
Wrongexcept Exception: retry_later(job)Rightexcept TemporaryFailure: retry_later(job)except Exception: next_stop(job)Quiet rule only at accept
majorAccepted at 21:59, retried at 22:04: the phone rings in the night.
Wrongdef notify(...): due = profile.earliest(kind, now)Rightdef _push(self, job, at): # every entry passes here due = profile.earliest(kind, at)The service makes the key
majorEach retry of the caller gets a new key, so nothing is ever recognised.
Wrongkey = uuid4()Rightkey = notice.key # order-5-shippedA lease shorter than the send
criticalA healthy send is repeated by the next worker.
Wronglease = 5; send(timeout=10)Rightlease = 30; send(timeout=10)
Interview tips
Open with
- What happened, who, how, when, how often.
- First that works, or all?
- The profile decides.
Draw
- One mutable class: Delivery.
- One interface: Sender.
- Waiting means in the heap once.
Prove it
- A dead address.
- A busy server at seven.
- The same key twice.
Admit
- Accepted is not delivered.
- A crash after the yes can double.
- Choose a side for each kind.