Design a Stock Market Push Notification System

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2025-05-22

Problem Statement

Design a system that can monitor stock prices in real time and push notifications to users when specific alerts or thresholds are met (e.g., price crosses ₹1000, % change exceeds 5%, or volume spikes).


🎯 Functional Requirements:

1.User Management
•Register/login
•Manage alert preferences
2.Alert Creation
•Set price-based, percentage-based, or volume-based triggers
•Example alerts:
•"Notify me when TCS > ₹4000"
•"Alert if NIFTY50 drops 2% in a day"
3.Market Data Ingestion
•Consume real-time stock/market price feed (NSE/BSE)
•Process updates per stock symbol (tick-level or interval)
4.Alert Matching Engine
•Continuously evaluate user-defined conditions
•Trigger alerts as soon as conditions are met
5.Notification Delivery
•Push notifications (via FCM/APNs)
•Email/SMS (optional)
6.Notification Management
•Avoid duplicate alerts
•Allow snoozing, muting alerts
•Track alert history


⚙️ Non-Functional Requirements:

•Handle high throughput (millions of price ticks per second)
•Low latency alert generation (sub-second)
•High availability and reliability
•Scalability to millions of users and alerts
•Exactly-once delivery of alerts
•Idempotent alerting (no spam)


🧱 Key HLD Components:

1.API Gateway
•Rate limiting, auth, routing
2.User Service
•Handles registration, alert preferences
3.Market Data Ingestion Service
•Subscribes to real-time stock feeds (Kafka/PubSub/WebSocket)
•Parses and normalizes tick data
4.Alert Engine / Rule Evaluator
•Matches user rules with incoming data
•Efficiently checks millions of alert conditions in-memory
5.Notification Service
•Sends push/email/SMS notifications
•Deduplicates and logs deliveries
6.Alert Management Service
•Store, edit, delete alerts
•Maintains metadata: triggers, thresholds, expiry
7.Queue & Pub/Sub System
•Decouples components
•Kafka / RabbitMQ for alert events
8.Data Store
•SQL DB for alerts and user data
•Redis/ElasticCache for hot alert rules
•Time series DB (optional) for storing historical prices


🗂️ Data Models (Simplified):


Alert

1json 2CopyEdit 3{ 4 "alert_id": "uuid", 5 "user_id": "u123", 6 "symbol": "TCS", 7 "condition": "price > 4000", 8 "triggered": false, 9 "expiry_time": "2025-07-30T15:00:00Z" 10}


PriceUpdate

1json 2CopyEdit 3{ 4 "symbol": "TCS", 5 "timestamp": "2025-07-23T12:00:00Z", 6 "price": 4032.45, 7 "volume": 100000 8}


🔁 Sequence Flow (Alert Trigger):

1.Real-time price feed → Market Ingestion Service
2.Feed forwarded to Alert Engine via Kafka
3.Alert Engine:
•Looks up active alerts for the stock
•Matches condition
•Sends alert event to Notification Service
4.Notification Service sends push/email
5.Marks alert as triggered (or reset if it's a recurring alert)


🧠 Scalability Considerations:

•Partition alert rules by stock symbol for parallel processing
•Use in-memory alert matching (e.g., Redis, Flink)
•Batch alerts where applicable
•Rate-limit per user/device


Solution

Comments