Uploaded by Vijay Kulakarni

Networking Monolith and Microservices Class 2

advertisement
HLD - High Level System Design
Summary of Networking, Monolith and Microservices Class 2:
Short Polling
Short polling is a client-server communication technique where the client periodically sends
requests to the server to check for new data. It's called "short" polling because the server
responds immediately whether or not it has new data available. If no data is available, the
server sends an empty response. The client then waits for a predetermined amount of time
before it sends another request. Advantages of Short Polling: It's simple to implement. The
client always has the most recent data that the server can offer.
Disadvantages of Short Polling:
It can be inefficient. If the data changes infrequently, the server must handle many unnecessary
requests. It may create an unnecessary load on the network and server. There's a delay
between when data is available and when the client receives it, because the client must wait to
make its next request.
Long Polling
Long polling is also a client-server communication technique and is a variation of the traditional
short polling. In long polling, the client sends a request to the server and the server holds the
request open for a set period of time. If a notification is received within that time, a response
containing the new data is sent to the client. If no new data is available within the timeout
period, the server sends a response to terminate the open request. Advantages of Long Polling:
More efficient than short polling. No need for repeated requests in absence of new data.
Updates are real-time and instantaneous because the connection remains open for a period of
time.
Disadvantages of Long Polling:
Requires more sophisticated server-side programming to handle long-lived connections and
potential concurrency issues. Might still have to send repeated requests if the timeout period
expires without receiving new data.
Server-Sent Events (SSE)
Server-Sent Events (SSE) allow a server to push updates to a client whenever it wants. The
client initiates a connection with the server and keeps the connection open. The server uses this
connection to send messages to the client. Messages are sent as "events", which can be
named, and the client can choose to listen for specific event names. Advantages of SSE: Unlike
long polling, the connection is left open indefinitely, not just for a specific period of time. More
efficient than polling as updates are pushed from server to client as they happen. Built into the
browser with the Event Source API.
Disadvantages of SSE:
It's a one-way communication protocol. If the client needs to send data to the server, it will have
to be done separately, often via an AJAX request. Not all browsers fully support it (particularly
Internet Explorer).
WebSockets
WebSockets provide full-duplex communication channels over a single TCP connection. This
means that data can flow in both directions between client and server simultaneously. The
WebSocket protocol enables interaction between a web browser (or other client application) and
a web server with lower overheads, facilitating real-time data transfer from and to the server.
WebSockets are ideal for applications that require real-time communication. When the
WebSocket connection is established, it stays open until the client or server decides to close
this connection.
Advantages of WebSockets: Allows real-time bidirectional communication. It's part of the
HTML5 specification and supported by all modern browsers. More efficient than polling, as it
reduces the amount of data that needs to be transferred for each message. Disadvantages of
WebSockets: Requires more system resources because the connection must remain open for a
longer time. May be blocked by some corporate proxies and firewalls. Handling WebSocket
connections is more complex than handling HTTP requests. Each of these methods has its
advantages and disadvantages, and the choice between them depends on the specific needs of
your application.
Short Polling
Use case: Short polling can be used for applications where real-time updates aren't crucial. It
is often used in scenarios where the data changes infrequently. Real-life application: An online
auction site like eBay could use short polling to periodically update the current bids on an item.
The application doesn't require constant real-time data as users don't expect the price to change
every second.
Long Polling
Use case: Long polling is used in applications where you want to minimize the latency
between an event happening on the server and the client learning about it. It's also useful when
the event frequency is low, which helps reduce unnecessary network traffic. Real-life
application: A weather application could use long polling to deliver immediate updates when
weather conditions change. The server would maintain the connection until there is a change to
report.
Server-Sent Events (SSE)
Use case: SSE is ideal for applications that need real-time updates in a single direction from
the server to the client. It's a good choice for applications that require constant, fast updates.
Real-life application: A stock market application would be a perfect use case for SSE. The
server could push real-time updates for stock prices to the client, providing a dynamic
experience for users.
WebSockets
Use case: WebSockets are ideal for applications that require real-time, bi-directional
communication between the client and the server. They're most useful for applications that
involve real-time interactive content. Real-life application: An online multiplayer game like
Fortnite or a chat application like Slack could use WebSockets. These applications require the
server and client to exchange information in real-time. In a chat application, for instance, as
soon as you send a message, the server pushes that message to all other connected clients.
Download