Design a Price Alert System
Understanding the Problem
📉 What is a price alert system? A price alert system watches prices for you and tells you when they hit a number you care about. A shopper says "email me when this TV drops below $400," the way CamelCamelCamel or Keepa do for Amazon products. An investor says "notify me if AAPL goes above $250 or below $200," the way every brokerage app does. Both keep a price history so users can see whether today's price is actually a good one.
Product price trackers and stock price alerts are asked as separate questions, but they're the same problem: a stream of price changes, a very large set of user thresholds, and a matcher in between. What differs is where prices come from. Product prices have to be pulled (by crawling or calling a retailer API) on a schedule we choose, while stock prices are pushed to us by a market data feed, at a rate we don't choose. We'll design both and point out where they diverge.
Functional Requirements
Core Requirements
- Users should be able to create an alert on a product ("below $400," or "drops 20% from today") or on a stock ticker ("above $250," "below $200").
- Users should be able to see a price history chart for a product or ticker.
- Users should be notified (push, email) soon after their alert's condition is met, and only once for each time the price crosses their threshold.
Below the line (out of scope)
- Buying the product or trading the stock.
- Alerts on technical indicators (moving average crossovers) or news.
- The browser extension that shows price history on the retailer's page (it's a client of the same history API).
Non-Functional Requirements
Ask your interviewer how fresh alerts need to be, because the two halves have very different answers. A stock alert that fires a minute late is useless. A TV alert that fires an hour after the price drop is fine, as long as it fires.
We'll assume 20M users with 200M product alerts on 50M distinct tracked products, and 50M stock alerts on about 10,000 tickers.
Core Requirements
- Stock alerts should fire within about 5 seconds of the price crossing the threshold, during market hours.
- Product prices should be re-checked often enough that popular products are at most ~15 minutes stale and every tracked product is checked at least daily.
- The system should never miss an alert, and never send the same alert twice for one crossing.
- The system should respect retailers' rate limits and terms, and prefer official APIs or feeds where they exist.
- The system should handle bursts: a market crash or a big sale event can trigger millions of alerts within minutes.
Below the line (out of scope)
- Pre-market and after-hours trading (it's the same pipeline with a different feed).
- Exchange data licensing, though it's a real cost in practice.
The back-of-the-envelope:
- Product fetch budget. We can't refresh 50M products every minute. Suppose retailers' APIs and our crawler give us about 2,000 fetches per second. If the top 1M products (which carry most alerts) are checked every 15 minutes, that's 96M fetches a day, or ~1,100/s. The other 49M products checked once a day is another ~570/s. Total: ~1,700/s, inside the budget.
- Product price changes. If ~10% of fetches find a new price, that's ~15M changes a day, about 170 per second. That's a gentle stream.
- Stock ticks. At the market open, the full quote feed can run to hundreds of thousands of messages a second or more. We only need the last trade price, and we can conflate to at most one update per ticker every 100 ms, which caps us at 10k × 10 = 100k updates/s, and far fewer in practice because most tickers trade rarely.
- Alert storage. 250M alerts × ~100 bytes = 25 GB, plus indexes. Comfortably fits on a sharded database, and the stock subset (50M × ~40 bytes = 2 GB) fits in memory.
- Bursts. If a bad market day triggers 10% of stock alerts, that's 5M notifications within a few minutes, ~17k/s over five minutes.
The main insight: the number of alerts is large, but every price change only affects alerts on one target, and only those whose threshold sits between the old price and the new price. If we index alerts by target and threshold, each change is a small range lookup, not a scan.
Many candidates design "every minute, loop over all alerts and check the current price." Do the math out loud instead: 250M alerts every minute is over 4M checks a second, almost all of them pointless because the price didn't change. Matching should be driven by price changes, not by alerts.