Notifications: Scale, Limits and Quiet Hours
Scale in a notification system means the design still works when a big event multiplies the normal traffic. This lesson takes the deep dive step, into scale, provider limits and quiet hours.
Where the peak goes
At the peak, the channel split decides how much traffic each group of workers faces. The printout divides the peak rate across the channels.
peak = 23000
split = [("push", 70), ("email", 20), ("text message", 10)]
total = 0
for channel, percent in split:
count = peak * percent // 100
total += count
print(channel, count, "per second at peak")
print("total", total, "per second")Priority sets the send order
Messages wait with a priority number, and the lowest number sends first. The printout shows a password reset jumping ahead of a marketing offer.
messages = [
("marketing offer", 3),
("new like", 2),
("password reset", 1),
("security alert", 1),
]
ordered = sorted(messages, key=lambda m: m[1])
for name, priority in ordered:
print("priority", priority, "-", name)Quiet hours hold the routine
The clock is passed in as plain data, set to a night hour. Urgent messages send, and the routine ones wait for the morning.
hour = 23
messages = [
("password reset", True),
("new like", False),
("weekly digest", False),
]
held = []
for name, urgent in messages:
if urgent or 7 <= hour < 22:
print(name, "sends now")
else:
held.append(name)
print(name, "is held for the morning")
print("held:", len(held))Example
now_hour = 23
messages = [
("weekly digest", 3, False),
("password reset", 1, True),
("marketing offer", 3, False),
("new like", 2, False),
]
ordered = sorted(messages, key=lambda m: m[1])
held = []
print("sending now, in order:")
for name, priority, urgent in ordered:
if urgent:
print(" ", name)
else:
held.append((name, priority))
print("held until morning, in order:")
for name, priority in held:
print(" ", name)Key points
- Peak: a big event can push traffic to about 23,000 notifications per second.
- Provider limit: each outside provider accepts only a stated number of sends per second.
- Rate limiting: the tool taught in the Rate Limiter problem caps sends to a provider at its limit, so the provider never cuts the system off for flooding.
- Priority: an ordering where an urgent message, like a password reset, jumps ahead of a routine one, like a marketing offer.
- Quiet hours: the night hours when non-urgent notifications are held until morning.
- Quiet window: this design holds non-urgent messages from 22:00 until 07:00.
In this example, the clock sits inside quiet hours and the queue holds four messages of different priorities. The password reset sends at once, and the rest wait for the morning in priority order.
Note
urgent messages ignore quiet hours, so a password reset still arrives at night.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
A marketing offer arrives during quiet hours. What does the design do?