Real-Time Web Communication and Network Protocol Fundamentals
System DesignModel answerLast updated
Evaluate trade-offs between real-time communication approaches and demonstrate understanding of network protocol layers and modern transport mechanisms.
You're architecting real-time capabilities for a web platform. This discussion explores your networking fundamentals across multiple protocol layers, focusing on practical engineering decisions rather than theoretical knowledge.
Address each section independently. Emphasize communication flow, protocol mechanics, performance characteristics, and production considerations. Avoid generalizations—specificity matters.
Context & Constraints
- Target environment: contemporary browsers, cloud infrastructure with reverse proxies and load balancers fronting stateless application servers
- "Real-time" means low-latency message delivery, not hard real-time systems guarantees
- All traffic over HTTPS; TLS is mandatory
- Consider behavior across both reliable wired connections and unreliable mobile networks
Questions to Explore First
- What's the message flow pattern: server-push only, client-send only, or full bidirectional?
- What scale are we targeting: concurrent connections and per-client message frequency?
- What network intermediaries exist: CDNs, corporate proxies, firewalls that might buffer, timeout, or block certain protocols?
- Which client platforms matter: browsers exclusively, native mobile apps, embedded devices? Any legacy compatibility requirements?
- What guarantees do we need: fire-and-forget or reliable delivery with resumption?
Section 1: Real-Time Communication Approaches
Analyze WebSocket, Server-Sent Events, and long polling across these dimensions:
- Communication patterns (unidirectional vs. bidirectional)
- Underlying transport and connection lifecycle
- Client/browser compatibility landscape
- Reconnection handling and message resumption
- Infrastructure implications: load balancer affinity, proxy buffering, connection pooling
- Representative use cases
Provide a decision framework for selecting among these three approaches.
Section 2: Network Layer Models
Describe each layer of the OSI reference model: its role and a concrete protocol or technology example. Then explain how OSI's seven layers correspond to the TCP/IP model's four layers, and why practitioners typically work with the simplified model.
Section 3: Transport Protocol Evolution
Compare TCP and QUIC on:
- Execution environment (kernel vs. userspace)
- Connection setup overhead and handshake steps
- TLS integration approach
- Stream multiplexing and head-of-line blocking behavior
- Connection portability across network changes
- Scenarios favoring QUIC (like HTTP/3) and arguments against adopting it
Include the practical deployment and operational trade-offs, not just feature comparisons.
Hints
Hint 1
Consider what happens when a load balancer must route subsequent messages from a long-lived connection
Hint 2
Think about the difference between blocking at the transport layer versus blocking at the application protocol layer
Hint 3
Modern protocols like QUIC encrypt more of the packet, which affects both security and operational visibility
Model answer
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Model Answer
A strong response demonstrates depth by connecting protocol mechanics to operational impact, distinguishing between layers precisely, and reasoning about trade-offs rather than listing features.
Section 1: Real-Time Communication Approaches
WebSocket provides full-duplex communication over a single TCP connection. The client initiates an HTTP Upgrade handshake; after a 101 Switching Protocols response, both sides can send frames independently. This bidirectionality makes it ideal for chat applications, collaborative editing, or multiplayer games where both client and server need low-latency messaging. Browser support is universal in modern clients.
The persistent connection creates infrastructure challenges: load balancers must maintain session affinity (sticky sessions) since the connection is stateful. Proxies that aggressively timeout idle connections can break the link. Reconnection logic must be implemented manually, including message buffering and deduplication. Scaling requires managing many concurrent TCP connections—roughly 1–2 KB memory per connection—which constrains vertical scaling on a single server.
Server-Sent Events (SSE) is unidirectional: server-to-client only over standard HTTP. The client opens a connection; the server responds with Content-Type: text/event-stream and sends events as text. Clients use the EventSource API, which handles reconnection automatically and sends the Last-Event-ID header to resume from the last received event.
SSE works through most HTTP infrastructure since it's regular HTTP, though some proxies may buffer the response, delaying delivery. For client-to-server communication, you still need standard HTTP requests (POST/PUT). This split can be simpler than managing WebSocket reconnection logic but requires two types of communication channels. Use SSE for dashboards, live feeds, notifications, or stock tickers where the server broadcasts to clients and occasional client requests suffice.
Long polling is the compatibility fallback: the client makes an HTTP request that the server holds open until data is available or a timeout occurs. After receiving a response, the client immediately issues another request. This creates a unidirectional server-to-client channel using only standard HTTP.
Long polling is the most resource-intensive approach—each client maintains an open HTTP connection that ties up a server thread or async handler, and reconnection is continuous. It works everywhere, even through aggressive proxies, but scales poorly. Reserve it for environments where WebSocket and SSE are blocked or unsupported.
Decision framework:
- Bidirectional low-latency messaging → WebSocket
- Server-push with occasional client requests → SSE (simpler reconnection, better proxy compatibility)
- Broad compatibility in restricted networks → Long polling
- Consider fallback strategies: attempt WebSocket, fall back to SSE, finally long polling
Section 2: Network Layer Models
OSI Model (7 layers):
- Physical: Transmission of raw bits over a medium (voltage levels, timing). Example: Ethernet physical layer, fiber optics
- Data Link: Framing, MAC addressing, error detection on a local link. Example: Ethernet (802.3), Wi-Fi (802.11)
- Network: Routing packets across networks via logical addressing. Example: IP (IPv4/IPv6), ICMP
- Transport: End-to-end reliability, flow control, segmentation. Example: TCP, UDP
- Session: Managing and maintaining connections, though often absorbed into application protocols. Example: NetBIOS, RPC session management
- Presentation: Data format translation, encryption, compression. Example: TLS/SSL, JPEG, ASCII/EBCDIC conversion
- Application: End-user protocols and services. Example: HTTP, DNS, SMTP
TCP/IP Model (4 layers):
- Link Layer: Combines OSI Physical + Data Link (L1–L2)
- Internet Layer: Maps to OSI Network (L3)
- Transport Layer: Maps to OSI Transport (L4)
- Application Layer: Combines OSI Session, Presentation, Application (L5–L7)
Engineers favor TCP/IP because it reflects how protocols actually stack in practice. Real implementations blur OSI's upper layers—TLS straddles presentation and session, HTTP handles presentation concerns. OSI is valuable for troubleshooting ("is this a Layer 2 or Layer 3 issue?") and education, but TCP/IP matches deployed systems. The seven-layer model is pedagogically clean; the four-layer model is operationally accurate.
Section 3: TCP vs. QUIC
Execution environment: TCP lives in the kernel, requiring OS updates to evolve. QUIC runs in userspace over UDP, enabling rapid protocol iteration and deployment without kernel changes. This accelerates innovation but shifts processing overhead to userspace.
Connection establishment: TCP requires a three-way handshake (SYN, SYN-ACK, ACK) before data flows, followed by a separate TLS 1.2+ handshake—typically 2–3 round trips total. QUIC combines transport and TLS 1.3 into a single handshake (1-RTT for new connections, 0-RTT for resumed connections), reducing setup latency significantly. TLS 1.3 is mandatory and integrated into QUIC, not layered atop.
Multiplexing and head-of-line blocking: Both HTTP/2 (over TCP) and HTTP/3 (over QUIC) multiplex streams. However, TCP is a byte-stream protocol: if one packet is lost, TCP blocks delivery of all subsequent data until retransmission completes, even if that data belongs to independent streams. This is transport-layer head-of-line blocking. QUIC uses independent stream-level flow control—packet loss affects only the streams contained in that packet, allowing others to proceed. HTTP/3 eliminates this blocking.
Connection migration: TCP connections are identified by a 4-tuple (source IP, source port, destination IP, destination port). Network changes (switching from Wi-Fi to cellular) break the connection. QUIC uses connection IDs, allowing the connection to survive IP/port changes seamlessly. This matters for mobile users.
When QUIC is preferred: HTTP/3 leverages QUIC's benefits: faster handshakes, better multiplexing, mobility resilience. Video streaming, mobile-heavy applications, and latency-sensitive services benefit.
Arguments against QUIC:
- UDP processing is more CPU-intensive than kernel TCP, increasing server load
- Corporate firewalls and some ISPs block or rate-limit UDP, forcing fallback to TCP
- Observability suffers: more of the packet is encrypted, complicating network troubleshooting and middlebox inspection
- Maturity: TCP tooling, monitoring, and tuning are decades-refined; QUIC ecosystems are younger
Choose QUIC when latency, mobility, and modern client bases matter. Stick with TCP where UDP is blocked, observability is critical, or infrastructure tooling isn't QUIC-ready yet.
Common mistakes to avoid:
- Confusing application-layer head-of-line blocking (HTTP/1.1 request queuing) with transport-layer HOL blocking
- Claiming SSE is bidirectional or that WebSocket can't work through proxies (it can, with proper configuration)
- Placing TLS ambiguously in OSI—it spans presentation/session
- Overstating QUIC benefits without acknowledging UDP blocking and CPU costs