Polling and Streaming – Concept & Scenarios
In systems, clients and servers communicate together in which client requests and server responds back to the respective client with requested data. Now there can be choices on at what frequency do I need to update the data? Well, it all depends on your purpose and your system. Here we are going to discuss two basic concepts Polling and Streaming.
It is defined as the process when a client requests a particular piece of data at regular intervals (maybe every x seconds) and the server reverts with a usual response with the required data.
In these scenarios, where the client needs to update the data (get the data from the server) regularly at instant mode, polling may not benefit your architecture. Ex: You are building a chat app where you have many clients that are meant to communicate with each other in real-time. As a System Design Expert, you need to make sure that the client gets the updates instantaneously (here updates means chats, txt, messages). But Polling is not suitable everywhere as an example of Chatbox, you need to get the instantaneous message as the other end sends it, but due to set interval of x seconds you are unable to get the instant feel of messages and your messages would feel a lot of delays
The best suitability of Polling can be getting temperature updates maybe 30sec/1min of regular intervals.
Streaming is done through sockets, you can learn about sockets in detail here. In Layman terms, sockets are a file that your computer can write/read from in a long width connection with another computer, an open connection till one machine turns it off.
Ex: Designing a Chat Application just like WhatsApp/Instagram and many others
Here you might think of a choice of reducing the set intervals and use Polling instead of Streaming, that is, you may think of reducing the set intervals to 1sec/0.5sec than within 10 seconds you are requesting up to 20 requests for a single client, and for millions of clients, this would create an issue for our server to handle these requests at the same time. Here you need to notice that you may get the instant experience in messages but this is not optimal as it would create much more load to a server.
Key Point: In Polling, for a response of a server, each request is sent, but in streaming, the client listens openly with no external request of data from the server. On the server-side, for streaming, it would not wait for data to send for every request but will push the data as it notices any changes. Through streaming, you can get instant experience of chatting without any delay nor sending 10-20 requests per second per client.
When the client-side processing is slower than different scenarios may occur in the case of Polling and Streaming.
In Streaming the updates would form a long queue on the client-end and as soon as it receives the first event the server will send the next and then next continuously till the end, there would be a delay due to poor processing but there won’t be any sort of sporadic in the data.
In Polling the processes would take some time to update. As soon as it finishes one, it would ask for another and answer it immediately. The server always tries to be robust and to keep streaming connections working; if it can’t write, it will just wait and possibly filter updates.
Note: Streaming is not better than Polling, Polling might be better than streaming, It all depends on your use-case and system.
General Rule: If you need to get your data updated instantly (live updates) then you want to use streaming, and if you are building a dashboard than monitors stock prices or have such a use-case here you may prefer polling as there is no real need of open connection and every 30 sec you can get the data updated.