Hello! In our previous lesson, we completed our look at the request-response cycle by focusing on robust error handling. You now know how to design a standardized error format that makes your API predictable and easy to use. Today, we'll pivot from this traditional client-initiated model to explore the exciting world of real-time communication.
Many modern applications, from chat apps to live dashboards, require the server to push data to the client without waiting for a request. In a backend interview, being able to articulate the different strategies for achieving this, along with their trade-offs, is a crucial skill. This lesson will equip you to do just that. Our goal is to compare WebSockets, Server-Sent Events, and long polling for real-time communication, so you can confidently choose the right tool for the job in a system design scenario.
The Limits of Traditional HTTP
The standard HTTP protocol, which has been the workhorse of our discussions so far, operates on a strict request-response basis. The client always initiates the conversation, the server responds, and the connection typically closes. This poses a fundamental problem for real-time features: how can a server notify a client about a new message or an updated stock price at the moment it happens?
The most naive solution is short polling, where the client repeatedly asks the server "Anything new?" at a fixed interval (e.g., every 2 seconds).
As you can imagine, this is incredibly inefficient. Most requests will return empty, but each one carries the overhead of HTTP headers and a new TCP connection. At scale, this leads to a massive waste of server resources and network bandwidth. To solve this, engineers developed several more sophisticated patterns.
Let's start by reading a brief overview of the problem and the four main approaches to solve it.
Real-Time Communication: WebSockets, SSE, and Long Polling ...
This article from abstractalgorithms.dev provides a fantastic, in-depth look at real-time communication patterns. We'll use it as our guide for this lesson.
First, read the section The Problem to solidify your understanding of why standard HTTP falls short. Then, review the overview table in the section Four Approaches. This table gives a high-level comparison that we will unpack throughout the lesson.
Simulating Real-Time: Long Polling
Long polling is a clever enhancement of short polling. Instead of responding immediately, the server holds the client's request open until it actually has new data to send.

Here’s the flow:
- The client sends an HTTP request to the server.
- The server doesn't respond right away if there's no new data. It keeps the connection open, waiting. This is often called a "hanging GET".
- When new data becomes available, the server sends it as the response to the open request, and the connection is closed.
- The client immediately makes a new request to the server, and the cycle repeats.
This pattern drastically reduces the number of empty responses and lowers latency, as data is sent almost as soon as it's available. The following video provides a concise explanation.
Long Polling, Websockets, Server Sent Events - Who Wins? | Systems Design with Ex-Google SWE
This video from the "Jordan has no life" channel offers a clear, practical breakdown of these concepts from an ex-Google SWE.
Watch the segment explaining long polling. Pay attention to the discussion of its unidirectional nature and the overhead of re-establishing the connection after each data delivery.
Long polling's main advantage is its simplicity and compatibility. It's built entirely on standard HTTP, so it works everywhere without needing special server configurations or worrying about restrictive firewalls. However, it's not a truly persistent connection. It still incurs the overhead of opening a new connection for every event, and holding many connections open can be resource-intensive on the server.
Persistent Connections: A More Modern Approach
While long polling is a useful "hack," modern applications often turn to protocols designed specifically for persistent connections. These methods establish a single, long-lived connection and amortize the setup cost over many messages.
Server-Sent Events (SSE)
Server-Sent Events provide a unidirectional, persistent connection for the server to push data to the client.
Here's how it works:
- The client makes a single HTTP request to an endpoint, specifying it wants to receive an event stream (
Accept: text/event-stream). - The server responds with a special content type and keeps the connection open indefinitely.
- Whenever the server has a new event, it writes it to the response stream.
This is much more efficient than long polling for frequent updates, as the connection is established only once. A standout feature of SSE is that the browser's EventSource API handles connection management automatically. If the connection drops, the browser will automatically try to reconnect. It even sends the ID of the last event it received, allowing the server to replay any missed messages.
Long Polling vs SSE vs WebSockets vs QUIC | Simply Explained
The "TechPrep" channel provides excellent, concise explanations with code demos. While the code is in Node.js, the principles are universal.
Watch the section on Server-Sent Events. The demo clearly shows the server sending timed events over a single, open connection. Note the advantages mentioned, especially the simplicity and built-in reconnection.
Use Cases: SSE is ideal for features like news feeds, stock tickers, streaming AI responses (like ChatGPT), or live build logs (like GitHub Actions), where the data flow is primarily from server to client.
Key Limitation: SSE is strictly one-way. If the client needs to send data back to the server, it must do so using a separate, standard HTTP request.
WebSockets
WebSockets provide a bidirectional, full-duplex communication channel over a single TCP connection. This is the most powerful and lowest-latency option.
The process starts with an "upgrade handshake":
- The client sends a standard HTTP request with an
Upgrade: websocketheader. - If the server agrees, it responds with a
101 Switching Protocolsstatus code. - The HTTP connection is then replaced by a raw TCP connection, over which both the client and server can send data at any time.
After the handshake, the communication is no longer HTTP-based. Messages are sent as "frames" with very little overhead (as little as 2 bytes per frame), making it extremely efficient for high-frequency updates.
Long Polling, Websockets, Server Sent Events - Who Wins? | Systems Design with Ex-Google SWE
Let's return to the "Jordan has no life" video for the WebSocket explanation.
Watch the segment on WebSockets. The key takeaways are its bidirectional and persistent nature, and the efficiency gain from not sending HTTP headers with every message.
Use Cases: WebSockets are essential for interactive applications like chat (Slack), collaborative editing (Figma, Google Docs), and multiplayer online games, where both the client and server need to send messages in real time.
Key Limitations: WebSockets are more complex to implement and scale. They are not plain HTTP, so they can be blocked by older proxies and firewalls. Also, unlike SSE, they do not have a built-in automatic reconnection mechanism; you must implement that logic yourself or use a library.
Comparison and Decision Framework
Now that you understand the mechanics of each pattern, the crucial interview skill is knowing when to use which. The choice almost always hinges on one primary question: Do you need bidirectional communication?
Let's read a detailed comparison and a practical decision guide.
Real-Time Communication: WebSockets, SSE, and Long Polling ...
This article provides a superb deep dive into the performance trade-offs and a clear decision-making model.
Please read the following sections: Deep Dive: This section analyzes the server-side resource costs and performance metrics of each pattern. Pay attention to the table comparing header overhead, latency, and fan-out complexity. Real-World Applications: These examples from major tech companies will help you connect the patterns to concrete use cases. Trade-offs & Failure Modes: Focus on the comparison table and the description of failure modes. The "Thundering Herd" problem with SSE and WebSocket connection loss are classic interview topics. Decision Guide: Study the flowchart carefully. This is your mental model for answering any "which real-time protocol?" question.
To frame this in a direct interview context, let's look at how you would apply this decision-making process to a typical system design problem.
System Design Mock Interview: Long Polling vs. WebSockets vs. SSE
This Medium article frames the comparison as a mock interview, which is perfect for your goals.
Read the section titled The Trick Question & Deeper Understanding. This walks through a scenario of building a live scoreboard and a chat feature, demonstrating exactly how to apply the decision framework we just learned. Note the discussion of using long polling as a fallback for WebSockets, a sign of a robust design.
Conclusion
You now have a solid framework for analyzing and choosing between the primary real-time communication patterns used in modern backend systems. Being able to discuss not just what they are, but why you'd choose one over the other—considering factors like directionality, latency, efficiency, and operational complexity—is what separates a good interview candidate from a great one.
Key Takeaways:
- The Core Problem: Standard HTTP is a client-initiated, request-response protocol, making it unsuitable for server-pushed, real-time updates.
- Long Polling: A simple, compatible HTTP-based "hack" that simulates real-time pushes. Best used as a fallback or in environments with restrictive firewalls.
- Server-Sent Events (SSE): The ideal choice for unidirectional server-to-client communication. It's efficient, built on HTTP, and features automatic reconnection. Think notifications, live feeds, and data streams.
- WebSockets: The go-to solution for true bidirectional, low-latency communication. Essential for chat, collaborative apps, and gaming, but brings higher implementation and scaling complexity.
- The Decision Framework: Always start by asking if communication needs to be bidirectional. If yes, choose WebSockets. If no, SSE is almost always the simpler, more robust choice.
This lesson concludes our module on API Design and Communication. We've journeyed from the basics of HTTP and REST, through pagination and error handling, to these advanced real-time patterns. In our next lesson, we will begin a new module, "Advanced Database Engineering," by diving deep into how databases use B-tree and hash indexes to execute queries efficiently.