Design a system that allows users to subscribe to one or more stocks and set custom rules (alerts) on stock prices.
When the rules are triggered (e.g., stock price crosses a threshold), the system should notify the user in near real-time.
🎯 Functional Requirements:
1.User Subscription
•Users can select one or more stocks to monitor
•Users can set rules (e.g., Uber > $60, Apple < $150)
2.Stock Rules
•Support price-based conditions: greater than, less than, equal to
•Optional: Add time-based or compound conditions (e.g., price > X for 10 mins)
3.Market Data Feed
•Continuously ingest and update live stock prices
•Price updates should trigger rule evaluations
4.Alert Engine
•Evaluate all active rules in real time
•Send notifications (email, push, etc.) when a rule is triggered
5.Notification System
•Ensure notifications are deduplicated and rate-limited (avoid spamming)
•Record all sent notifications (audit trail)
6.Rule Management
•APIs to create, view, update, and delete rules
•Users can toggle rules ON/OFF
⚙️ Non-Functional Requirements:
•Scalable: Handle millions of rules and thousands of stock updates per second
•Low Latency: Alert users within a few seconds of price match
•Fault Tolerance: Notifications must be reliable and retryable
•Security: Ensure only authenticated users can access/update their rules
🧱 Key Components:
ComponentDescription
User Service
Authenticates and manages user profiles
Stock Feed Ingestor
Ingests real-time stock prices (via WebSocket, Kafka, etc.)
Rule Engine
Evaluates each rule on stock price updates
Notification Service
Sends alerts to users (email, SMS, push)
Rule Store
Stores user-defined rules and subscription mappings
Price Store / Cache
Stores the latest prices of all stocks
🛢️ Schema Design (Simplified):
users
1nginx
2CopyEdit
3user_id | name | email | created_at
stocks
1typescript
2CopyEdit
3stock_id | symbol | name | current_price | updated_at
subscriptions
1nginx
2CopyEdit
3user_id | stock_id
rules
1sql
2CopyEdit
3rule_id | user_id | stock_id | condition (>,<,=) | target_price | is_active | last_triggered_at
notifications
1lua
2CopyEdit
3notification_id | rule_id | user_id | message | status | sent_at
🔁 System Flow (Uber > $60):
1.User subscribes to Uber and creates a rule: "Uber > $60"
2.The stock feed ingests live prices → Uber price updates from $58 to $61
3.Rule Engine fetches all active rules for Uber
4.Rule "Uber > $60" is evaluated as true
5.Notification is created and passed to the Notification Service
6.User receives an email/push: "Alert: Uber crossed $60!"
🧰 Suggested Tech Stack:
LayerTools
Backend APIs
FastAPI / Spring Boot / Express.js
Streaming & Feed
Kafka / WebSocket / PubSub
Rule Evaluation
Redis (for caching rules), Kafka Streams, or in-memory evaluators
Notification
Firebase, Twilio, Email API
DB
PostgreSQL / MongoDB
Cache
Redis (for price store, rate limiting)
📈 Scalability Considerations:
•Sharding rules by stock symbol or user ID
•Use Kafka topics per stock for stream-based rule evaluation
•Deduplicate alerts using last_triggered_at or alert hash
•Batch evaluate rules for the same stock to reduce computation
•Push price updates and evaluation logic to stream processors (e.g., Apache Flink, Kafka Streams)
🧠 Advanced Features (Optional):
•Compound rules (Uber > 60 AND Tesla < 700)
•Trailing stop alerts
•Historical price-based conditions (e.g., 5% increase in 1 day)
•Custom frequency (check rule every X seconds/minutes)
•Alert cooldowns (avoid alerting too frequently)