Detailed Component Design
3.1 Real-Time Event Flow
Step 1 — Data providers push live events
We assume:
- events are timely
- accurate
- delivered through some push API
Step 2 — Ingestion Service normalizes
- Normalizes sports event formats
- Adds metadata
- Publishing every event into a central message bus topic
Reasoning:
- decouples push from ingest
- acts as buffering in case of temporary slowdowns
- ensures no backpressure on ingestion
Step 3 — Event Router fans out
- Real-time fan-out to WebSocket servers
- Separate background workers store the same events to SQL DB
Latency target:
- < 200 ms end-to-end
- streaming via message bus ensures low overhead and horizontal scaling
3.2 Delivering Events to Users
Push Layer (WebSocket servers)
Responsibilities:
- Authenticate user
- Maintain WebSocket session
- Store user’s subscribed interests in local memory
- Receive events from message bus
- Determine which sessions care about the event
- Push message over WebSocket
These servers are stateless, aside from:
- memory of which users are connected
- their subscription list
Because they are stateless:
- scaling horizontally is simple
- failure of one server only affects its open WebSocket connections
3.3 Handling Disconnects / Missed Events
WebSocket alone does not guarantee delivery if the user disconnects.
So we handle this by:
When user reconnects
- Client calls: GET /games/{game_id} or /state-sync
- Server returns latest game state
- Small gap in messages is corrected
This guarantees eventual consistency for users even if they temporarily lose the connection.
3.4 User Data Management
User metadata + follow lists are stored in NoSQL:
- scalable
- low write/read latency
- document-oriented structure
This DB is used by:
- User Service (REST)
- Push Layer (to know who to notify)
3.5 Storage of Historical Data
All long-term storage is done in PostgreSQL:
- Every event is appended to game_events table
- Periodic aggregations update:
- final score
- player stats
- team stats
This provides:
- ACID correctness
- referential integrity
- ability to query:
- “What are last 5 games for team X?”
- “Show full timeline for game Y”
This meets consistency and persistence requirements.