Scheduled Jobs and Proactive Outreach: Time Zones, Quiet Hours, Daily Caps, a Notification Provider Abstraction
Implement a scheduled job that proactively messages users: handle time-zone conversion, quiet hours, and a daily send cap correctly, and abstract the notification channel behind a swappable provider.
Today's Goals
- Correctly compute the local "time to send" per user's time zone
- Implement quiet-hours and daily-send-cap limiting logic
- Abstract sending a notification behind a provider interface that supports swapping channels
Yesterday (D19) connected the multi-agent service to mini-koda with user-level tokens, with the endpoints working and idempotency handled, and the whole system is still "the user speaks and the system does one thing." Today it learns to speak first — and to keep quiet before it does.
Plain-Language Walkthrough
Why building management does not knock at midnight
A building's management office deals with residents in two situations. In the first you go downstairs and ask: what happens now my parcel was returned? You are waiting for an answer, and whatever they say you take in. In the second, management decides to tell you something: the water goes off tomorrow, the lift is being serviced, the door fobs are being replaced. Nobody is waiting for that one; it barges in.
The same sentence is a service in the first situation and possibly an intrusion in the second. The difference is not content, it is who spoke first.
Back to the system. D13 finished "can send on schedule": a central scheduler wakes on a cron expression, publishes the task into the message bus, and the idempotency key is anchored to the scheduled minute, so multi-replica duplicate triggers still leave one row. That chain does not change one line today; today adds the layer that grows on top of it — whether it should send at all.
A user-initiated message and a system-initiated one are two different things in three respects:
| User-initiated | System-initiated | |
|---|---|---|
| Who is waiting | the user is watching the screen | nobody |
| On failure | must surface an error to the user | usually should quietly postpone or drop |
| On what basis | the user spoke | you have to state a reason yourself |
The third row is this chapter's thesis: the default answer for a proactive message is do not send. For every proactive message about to go out you must answer three questions — why now, why this user, and why this content is worth interrupting them. Fail any one and it should not be sent.
That is not a statement of values, it is arithmetic. Users' tolerance for proactive messages is very low: after a few inconsequential pushes in a row they will not argue about the content, they will simply turn notifications off — and once off, even the genuinely important message cannot reach them. What you spent was not this one instance of their attention, it was a quota that cannot be recovered once used.
So the engineering form of proactive care is not how to get a message out, it is how to have the vast majority of candidates stopped before they go out. This course makes that three gates, with the same rules as the management office: compute by the resident's own routine rather than the office's working hours (time zones), do not knock at midnight (quiet hours), and post at most one notice a day (a daily cap).
Of the three, the first is the easiest to get wrong and the most overlooked question is which step the gates are installed at.
Interview questions
How does a system-initiated message differ from a user-triggered one, from a system design point of view?系统主动发给用户的消息,和用户自己触发的消息,在系统设计上有什么不同?
Common in ChinaCommon overseasBasic#proactive-messaging#system-design#product-engineering🔒 Unlock this chapter to see the answer keys.
Building a scheduled push service for users worldwide, what timezone pitfalls would you hit, and how do you handle the DST switchover day?做一个面向全球用户的定时推送服务,时区上你会踩到哪些坑?夏令时切换那天怎么处理?
Common in ChinaCommon overseasDeep dive#timezone#scheduling#correctness🔒 Unlock this chapter to see the answer keys.
How would you implement quiet hours and a per-user daily cap, and where in the pipeline should they be evaluated?quiet hours 和每日发送上限这两条规则你会怎么实现?它们应该放在链路的哪一步判断?
Common in ChinaCommon overseasIntermediate#rate-limiting#quiet-hours#cost-control🔒 Unlock this chapter to see the answer keys.
You have abstracted model calls, payments and notification channels behind providers. How does the notification interface differ from the other two?模型调用、支付、通知渠道你都做过 provider 抽象。通知这一份接口和另外两份有什么不同?
Common in ChinaCommon overseasIntermediate#provider-abstraction#api-design#retry-semantics🔒 Unlock this chapter to see the answer keys.
