AlgoViz

Design a notification service

medium 85 min

Design 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

0:00 / 4:50

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

shopcallerserviceNotificationServdueemptykeysnonelockfreeashaProfilewantspush, smsquietnonenotify(login-7)

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

serviceNotificationServdueoffer 07:00keysoffer-9lockfreenoorProfilewantsoffers: pushquiet22 to 7deliveryDeliverystop1 of 1tries0statuswaiting

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

    major

    Three channels and two levels are six leaves, and the next question doubles them. One event becomes unrelated objects.

    Wrong
    class UrgentSmsNotification(SmsNotification): ...
    Right
    Notice(key, user, Kind.CODE)      # urgent is dataStop(Via.SMS, address, message)   # the channel is a stop
  • Sending under the lock

    critical

    One slow vendor stops every accept and every worker.

    Wrong
    with self._lock:    job = heappop(self._due)    sender.send(...)
    Right
    with self._lock:    job = heappop(self._due)sender.send(...)               # nobody waits for it
  • One except for everything

    critical

    A dead number is tried for an hour, a busy server is given up at once.

    Wrong
    except Exception:    retry_later(job)
    Right
    except TemporaryFailure:    retry_later(job)except Exception:    next_stop(job)
  • Quiet rule only at accept

    major

    Accepted at 21:59, retried at 22:04: the phone rings in the night.

    Wrong
    def notify(...):    due = profile.earliest(kind, now)
    Right
    def _push(self, job, at):       # every entry passes here    due = profile.earliest(kind, at)
  • The service makes the key

    major

    Each retry of the caller gets a new key, so nothing is ever recognised.

    Wrong
    key = uuid4()
    Right
    key = notice.key     # order-5-shipped
  • A lease shorter than the send

    critical

    A healthy send is repeated by the next worker.

    Wrong
    lease = 5; send(timeout=10)
    Right
    lease = 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.