Lesson 2 · Minutes 10-20

Notification System: First Design

Problem

Anyone who has read about this problem has a queue on the board by minute eleven. You do not, because you have nothing to point at yet that needs one. What goes up in minutes ten to twenty is the direct version: a request comes in, you call the provider, and you answer with what it said. It serves the estimate, and it comes with a thread count you can say out loud.

This lesson draws it, follows one sign-in code through it, shows the sum that makes it fair, and names where it stops. The quiz at the end holds the four pushes it invites, in the order the next lesson takes them.

A Request That Waits for the Provider

A load balancer spreads the calling services over a tier of notification API servers, which keep no state of their own. Behind them is one relational database with the users' tokens and addresses, their preferences, and a row for each notification.

The API does everything inside the request. It reads the user's token or address, checks the preference, and calls the provider for the named channel. It writes a row with the outcome and answers the caller, who has been waiting all along.

A call that times out comes back to the caller as an error, and the caller may try again, even though the provider may have accepted the first try. So the arrow to each provider is at-least-once, and you write that on the arrow now, where the interviewer can see that you chose it.

The first design
Calling servicesname a user ID Push provider100 ms a call Load balancerany API server Email provider100 ms a call Notification API200 threads Text provider3 seconds a call One databasetokens, choices, rows and waits gets the outcome read, then write a row in the request at-least-once: the caller retries readwrite

One Sign-In Code, There and Back

A login service posts a request ID, a user ID, the sign-in template with its code, the critical class and the text channel. The load balancer picks a server, and the server reads the user's phone number. A critical message cannot be muted, so the preference does not stop it.

The server calls the text provider and holds a thread for three seconds. The provider accepts. The server writes a row saying sent, and the login service hears sent. The caller has a true answer: the provider took the code. Whether it reached the phone is more than this reply can say.

Now the failure. The provider says nothing for ten seconds, and the server gives up. It writes a row saying timed out, and the login service gets an error. It retries with the same request ID, and a second call goes out. If the first one got through after all, the user has two codes. The request ID is in the row, and nothing reads it yet.

Fair at 196 of 200

Nothing functional is missing here, and the caller gets a true outcome: sent or not. Walk the numbers. Assume 200 request threads across the tier, and about 100 milliseconds for a push or email call.

The steady services send 38 million push and email a day, which is 380 a second, and 760 at peak. At 100 milliseconds, those hold about 76 threads. Forty texts a second at three seconds hold 120. That is 196 of 200, so it serves the steady estimate while every provider is healthy, with no headroom left. A small product ships exactly this. The rows put a date on it. Forty million a day at 500 bytes is 20 GB a day, so an assumed 2 TB node fills in about 100 days, and you say that before anyone quotes Scope's 17 nodes back at you.

Five Limits You Leave Standing

Every caller waits on the slowest provider. All channels share one pool of threads, so a slow text holds up a push. A campaign is 50 million calls to this API, and at the push provider's 5,000 a second those calls alone want 500 threads. A crash in the middle of a call leaves no record of what was sent, because the row comes afterwards. And the preference is read once, which is fine only while nothing waits. All five stay on the board for now, on purpose.

Volunteer the sharpest one yourself, and put a number on it: "Healthy providers take 196 of my 200 threads at peak. If the text provider's three seconds becomes a ten-second timeout, text alone wants 400, and I'd see every caller stall, even the ones sending push."

The marketing text's duplicate rule stays parked from Scope, and quiet hours join it. Say so in as many words: "Preferences are one read for now. Caps and quiet hours wait until the queues are in."

Interview Pitfalls
  • Drawing the queue first because every notification answer has one. Asked what it is for, you have no slow provider to point at yet.
  • Calling the first design a straw man when it serves the estimate at 196 of 200 threads. The interviewer stops trusting whatever you draw next.
  • Leaving the guarantee off the provider arrow, so the duplicate probe arrives as a surprise.
What Interviewers Listen For
  • One notification walked from caller to provider and back, with the failure case named.
  • The thread arithmetic used to say why the design is correct today.
  • Four hundred threads wanted against 200 held, raised before any provider misbehaves.

Knowledge Check

"The text-message provider takes three seconds and sometimes times out, and every caller waits on it. What has to move?"

  • "Give the notification API more threads so that slow provider calls have room."
  • "Accept the request into a queue per channel and let workers call the provider."
  • "Set a one-second timeout on the provider call and answer the caller with an error."
  • "Start a background thread in the API for each send and answer the caller at once."

"Marketing drops a campaign of fifty million into your queue, and a sign-in code lands right behind it. How long does that user wait?"

  • "Not long, because workers can scan the queue and take high-priority messages first."
  • "About three hours, so throttle marketing to half of the provider's limit from now on."
  • "A few seconds, since more workers during a campaign drain it as fast as we need."
  • "About three hours, so each class of message gets its own queue and its own workers."

"A worker crashes after the provider accepted a message and before it marked it sent. The queue hands that message to another worker. Now what?"

  • "Reuse a stable ID as the provider's key, and let each channel set its own rule."
  • "Put the provider call and the status update inside one database transaction."
  • "Acknowledge the queue before calling the provider, so that no message can go twice."
  • "Turn on the broker's exactly-once mode, which carries through to the provider."

"A user mutes marketing at nine in the evening. A campaign message for them has been waiting in the queue since 8:55. Do they get it?"

  • "Yes, because their preferences were already checked when it entered the queue."
  • "No, because a mute deletes all of that user's waiting messages from every queue."
  • "No, because each worker reads the user's preferences again just before sending."
  • "Yes, since a mute only ever promises to apply to campaigns that start after it."

You got correct