Pull vs Push: Messaging System Design Options
This lesson explores real-world system design choices between pull and push technologies.
AWS SQS: Pull-Based with Long Polling
AWS SQS is a pull-based system supporting both short and long polling.
When designing applications, long polling is preferred.
Long polling reduces empty responses and lowers operational cost.
Apache Kafka: Protocol-Driven, Not HTTP-Based
Kafka uses a custom TCP protocol, not HTTP.
Short and long polling are not natively applicable.
However, an HTTP-based proxy can be introduced in front of a Kafka cluster.
Clients communicate with the proxy over HTTP.
The proxy connects to Kafka over TCP.
Polling over TCP can mimic short and long polling behaviors.
- Clients can request messages immediately
- Or wait for new messages to accumulate
This behavior mirrors short and long polling.
Kafka offers rich configuration properties to control polling.
AWS Kinesis: HTTP-Based, Short Polling Model
Kinesis operates over HTTP.
The Kinesis Client Library uses short polling.
It retrieves a batch of messages, processes them, then makes a new request.
If no messages are available, the client sleeps for one second before retrying.
Push-Based Systems: WebSockets Support
RabbitMQ and Apache ActiveMQ support web sockets.
Web sockets enable bidirectional communication.
Real-Time Chat Applications
Web sockets are a natural choice.
Clients send messages to the server.
The server pushes new messages to clients.
Long polling and Server-Sent Events (SSE) are also viable.
Both deliver messages from server to client.
Message sending from client to server is straightforward.
Clients can use regular HTTP POST requests.
Early Facebook Messenger used long polling.
Long polling supports all browser versions.
It works when web sockets or SSE are unavailable.
A basic chat app can be built with SSE in under one hour.
News Feed Updates: Pull or Push?
News feeds (e.g., Facebook, Twitter, LinkedIn) can use either pull or push.
Clients can pull data periodically.
Or the server can push updates when new content is published.
Feeds typically contain dozens to hundreds of messages.
High-Frequency, Small-Payload Scenarios
Examples include stock price dashboards.
Prices require frequent updates.
This is high-frequency messaging with small payloads.
Short and long polling are suboptimal.
HTTP headers dominate message size.
Header overhead exceeds message payload.
In such cases, use either WebSockets or Server-Sent Events.
These technologies minimize overhead and enable efficient real-time delivery.
WebSocket in Browser Games
A browser game can use WebSocket for real-time, bidirectional communication between clients and servers.
Collaborative Code Editors
Many collaborative code editors—commonly used in coding interviews—require real-time updates.
Recommended Technology
For such editors, WebSocket is a preferred option.
Similar to Online Chat Apps
The use case resembles that of an online chat application, where real-time messaging is essential.
WebSocket Appears Universal
WebSocket looks like a solution to every problem in real-time systems.
Logical Foundation
This is logical because both polling and server-sent events can be implemented with WebSockets.
Protocol Requirement
This assumes both client and server support the WebSocket protocol.
Critical Caveat
There is a key difference: what can be done versus what should be done.
Directional Use Case
If only one-way message pushing is needed—such as server-to-client—server-sent events may suffice.
WebSocket as Overkill
WebSocket becomes overkill in such cases due to unnecessary complexity.
System Design Challenge
System design is difficult because many technologies exist, and choosing the right one is challenging.
Interview Strategy
Mentioning multiple options shows breadth of knowledge.
True Engineering Value
True value lies in selecting the right tool for the specific requirement.
Core Principle
Engineers must evaluate pros and cons, prioritize simplicity, and avoid unnecessary engineering.
Self-Exploration
Investigate technologies used by favorite websites.
Reverse Engineering
Web browsers enable reverse engineering via developer tools.
Chrome Developer Tools
In Chrome, access the network tab to inspect communication.
Gmail Example
Gmail uses polling, not WebSocket, for updates.
YouTube Example
YouTube does not use WebSocket to play videos in a web browser on a PC.
Hands-On Verification
Try it yourself—some websites will surprise you.
Questions
- What technologies would you choose for a real-time chat application?
- How would you implement a news feed update on the client side?
- What should you use for a dashboard with stock prices that require frequent updates?
- What technology would you recommend for a collaborative code editor like those used in coding interviews?
- What are the viable options for delivering messages from a server to a client in a system that doesn't support web sockets?
Key takeaways
- WebSocket appears to solve many problems, but it may be overkill for one-way message delivery.
- Long polling and server-sent events can deliver messages from server to client without requiring web sockets.
- A client can send messages to a server using a regular HTTP POST request, even when using long polling.
- For high-frequency, small-message systems, HTTP polling overhead becomes significant due to headers.
- Both short and long polling are not ideal when message payloads are small compared to header size.
- The choice between technologies isn-–t just about what's possible—it's about what's appropriate for the use case.
- Real-world systems may use different technologies even if multiple options exist; for example, Gmail uses polling, YouTube does not use WebSocket for video playback.
- Engineering decisions should balance simplicity, performance, and compatibility—avoiding over-engineering when simpler solutions suffice.