Important Concepts in Product Architecture - II
Explore critical concepts in product architecture focusing on data-fetching methods such as short polling, long polling, and WebSockets. Understand event-driven protocols like webhooks, SSE, and WebSub for efficient server push communication. Learn how cookies and sessions maintain user state and the importance of idempotency in API design to ensure reliable and consistent server behavior under repeated requests.
Data-fetching patterns
Real-world applications serve millions of data-fetching requests per second, especially in read-heavy systems like Twitter and Quora. Different use cases demand different communication structures: some tolerate delay, while others require near-real-time updates (for example, navigation systems where the server provides directions and the client continuously reports its location). The foundational polling methods for client-server data exchange are short polling and long polling. Beyond these, WebSocket provides a full-duplex protocol that addresses polling limitations for true real-time communication.
Short polling has the client request data at regular intervals (typically less than one minute). The server responds with updated data or an empty message. While it can feel near-real-time at short intervals, choosing the right polling frequency is critical. Short polling works best when the server's update schedule is predictable. Its core problems:
Needless requests when the server has no updates, yielding mostly empty responses.
The server cannot push an update until the client's next scheduled request arrives.
If the server has new data immediately after a response, the client must still wait for the predetermined interval before making its next request. Long polling (also called hanging GET) addresses this by keeping the connection open until the server has new data or an acceptable timeout expires. The server never sends an empty response. This reduces latency for real-time data, but it does not deliver significant performance gains because the client may need to reconnect repeatedly after timeouts.
Problems with long polling include:
Delays while waiting for updates or timeouts.
The server must manage unresolved states for many connections and their timeout details.
The client needs multiple concurrent connections to send new information while an existing connection awaits a response or times out.
Note: The TCP connection used by both polling approaches can be persistent (opened once) or nonpersistent (a new TCP/IP connection initiated per request).
(Select all that apply.) Identify the advantage(s) of using persistent TCP connections from the given options. Multi-select
Reduced network traffic
Increase connection management cost
Decreased latency
Increase memory consumption
WebSocket
To overcome the limitations of both polling approaches, WebSocket provides a persistent
WebSockets’ stateful nature allows reuse of the same open TCP connection, making them ideal for multimedia chat, multiplayer games, and notification systems. A WebSocket connection upgrades from a standard HTTP connection. More detail is available in this course’s lesson on WebSockets.
Consider creating a real-time messaging application for an organization’s communication between multiple international offices. Which approach will be appropriate for this case?
Short polling
Long polling
WebSockets
None of the above
Comparison of data polling approaches
The table below compares short polling, long polling, and WebSockets based on the primary factors for selecting the right approach in a given scenario.
Data Polling Approaches | Low Latency | Efficient Bandwidth Usage | Full Duplex | Browser's Compatibility |
Short polling | No (can lower by it using 0 timeout, but at the expense of poor network use) | No (can be better by increasing timeout but at the expense of higher delay) | No | Yes |
Long polling | Yes (better than short polling) | Yes (better than short polling) | No | Yes |
WebSocket | Yes | Yes | Yes | Yes |
Now that you’ve learned how each data-polling technique works, evaluate your understanding with a series of questions using the AI mentor.
Short polling introduces substantial delay through fixed wait intervals. Long polling reduces this by holding the connection open, but the server and client can still communicate only in half-duplex mode. WebSockets resolve both issues with full-duplex, persistent communication.
Of short polling, long polling, and WebSockets, which data-fetching pattern would be most effective for developing a real-time messaging application that supports communication between multiple international offices within an organization? Discuss the approach you chose and its benefits for efficient data retrieval and real-time communication across distributed locations.
Event-driven architecture protocols
While polling techniques give the client control over when to fetch data, they remain fundamentally request-driven. HTTP-based approaches have inherent limitations for real-time communication: short polling generates excessive, unnecessary requests; long polling requires two separate connections for bidirectional data flow; and HTTP streaming supports only half-duplex communication. Event-driven architecture (EDA) ...