Real-Time Systems in System Architecture
advance · System Architecture
Real-Time Communication: Delivering Data in Sub-Milliseconds In traditional web applications, the client initiates every request, and the server responds. Real-time systems invert this model, allowing the server to "push" data to the client the instant an event occurs. This is the foundation of live chat, collaborative editing, and real-time dashboards. 1. Server-Sent Events (SSE) SSE is a standard that allows servers to push data to web pages over a single, long-lived HTTP connection. How it works: The client opens a connection to the server, and the server keeps that connection open, streaming data as a sequence of events. Key Characteristic: It is unidirectional (Server $\rightarrow$ Client). Best Use Case: Live news feeds, stock tickers, or notification systems where the client only needs to receive updates, not send them back frequently. 2. WebSockets WebSockets provide a full-duplex, bidirectional communication channel over a single, long-lived TCP connection. How it works: The client and server perform a "handshake" to upgrade an HTTP connection to a WebSocket connection. Once established, both sides can send messages to each other simultaneously without the overhead of HTTP headers. Key Characteristic: Bidirectional . It is significantly more efficient than constant HTTP polling. Best Use Case: Real-time chat applications, collaborative document editing (like Google Docs), and multi-player gaming. 3. WebRTC (Web Real-Time Communication) WebRTC is the standard for high-performance, peer-to-peer (P2P) communication, bypassing the server entirely for the media stream. How it works: It uses complex protocols like STUN/TURN servers to help two browsers find each other's IP addresses and "punch" through firewalls to establish a direct connection. Key Characteristic: Low Latency . Because data goes directly from Client A to Client B (instead of routing through your server), it is the only viable way to stream high-quality audio and video. Best Use Case: Video conferencing (Zoom-style), VoIP, and real-time screen sharing. 4. Real-Time Messaging Real-time messaging at scale is not just about the protocol; it is about infrastructure. Pub/Sub Integration: To handle thousands of users in a chat room, you need an intermediary. A publisher (e.g., User A) sends a message to a Redis Pub/Sub or Kafka topic; the backend service then pushes that message to all connected WebSocket clients subscribed to that channel. State Management: You must track which server a specific user is connected to (using a "Connection Registry" in a database like Redis) so that when a message arrives, your system knows exactly which server needs to push the message to that user's device. Real-Time Communication Matrix Protocol Direction Latency Complexity Primary Use Case SSE Server $\rightarrow$ Client Low Low Live feeds, dashboards. WebSockets Bidirectional Very Low Moderate Chat, gaming, collaboration. WebRTC Peer-to-Peer Ultra-Low High Video/Audio conferencing.