#20 Polling vs. Push/Reactive
Price Alert System
Notify users the moment a stock or coin crosses the price they set, with millions of alerts and prices changing constantly.
What is being asked
Users set alerts such as "tell me when BTC goes above 70,000". Design the system that watches a fast price stream and fires each matching alert once, quickly.
Requirements
Functional
- Create, edit and delete alerts: instrument, direction, threshold.
- Fire a notification when the condition is met; one-shot or repeating.
Non-functional
- Thousands of price ticks per second across instruments.
- Tens of millions of alerts, mostly idle.
- Fire within about a second, and not twice.
Design
- Alerts are stored durably, and also indexed in memory per instrument as two sorted structures: "above" thresholds ascending and "below" thresholds descending.
- Price ticks arrive on a stream partitioned by instrument, so one matcher sees all ticks for an instrument in order.
- When the price moves from p1 to p2, the matcher takes every "above" threshold between p1 and p2 with a range lookup. It does not scan all alerts.
- Matched alerts are marked fired and sent to a queue; the notification service delivers them.
Key decisions and trade-offs
- Push, not polling. Checking every alert against the price on a timer is wasteful and slow. Reacting to each tick and looking up only the crossed range is proportional to the alerts that fire.
- Crossing, not level. Fire when the price crosses the threshold, comparing with the previous price; otherwise an alert fires on every tick above it.
- Hot instruments. A popular coin can hold millions of alerts and tick many times a second. Give it its own partitions and coalesce ticks within a few milliseconds.
- Recovery. After a restart the matcher rebuilds its index from the alert store and replays the price stream from its last committed offset.
Follow-up questions
- How do you guarantee an alert fires once if the matcher crashes after matching?
- How would you support "moves 5% within an hour"?
- What do you do if the market data feed stalls?
- How do you rate-limit a price that oscillates around a threshold?