The definition and advantages and disadvantages of web front-end polling to obtain data

what is polling

Polling should be a concept in the CPU scheduling algorithm at the beginning. Generally speaking, the CPU asks whether the service is needed every once in a while. The extension of this concept to web services is also similar. The front end requests information from the server at regular intervals.

Why do you need to poll

So why use polling? In fact, what is said here can be regarded as the advantage of polling.
When the front end needs to confirm whether some information has changed every once in a while, polling is needed. After all, if the front end wants to obtain the server status change, it must either actively pull it or let the server push it. The situation of active pull is divided into user behavior triggers and our regular pull. If we want the information to be as fresh as possible, we must not just wait for the user’s behavior to trigger it, but we also need to pull it every once in a while. So we come to the first advantage of polling - the information is relatively new .
In addition, in polling, we can also encapsulate the information requests required by multiple modules of the software into one request, which reduces the number of front-end requests and reduces the resource consumption of multiple front-end network requests. It may also be possible Reduce server load. The second advantage - reduce the number of network requests .

The server actively pushes?

As mentioned above, the server actively pushes the request, can the web page allow the server to actively push it? Aren't they all HTTP requests? In fact, there are many ways to actively push HTTP requests:

  1. The front end requests at a smaller time interval , which seems to be as timely as the server actively pushes, but it consumes a lot of resources
  2. In fact, HTTP/2 also supports active push, which requires additional settings on the server side. When a request is received, other resources are also returned together. However, this kind of configuration needs to be changed, and the service must be restarted after the change. Putting the server configuration together seems inappropriate. In actual development, it is generally separated, and you have no control over the server configuration. Another way is to add the Link command to the response header, and the server will bring the corresponding resources. But in any case, this is not a real active push, you still have to follow the response immediately after the request, but you can put a few more responses

http://www.ruanyifeng.com/blog/2018/03/http2_server_push.html

  1. Based on the HTTP websocket protocol, after the connection is established, the server can push it at any time. This is the real active push, but it also has compatibility issues . Some new APIs are not supported by certain versions of browsers. And this also requires changes in the background to support new protocols and push data to all backgrounds that monitor this key as soon as the data is updated. Will the background overhead here be much more than the traditional request-response? It seems that when the number of users is large and there are many dependencies, the memory consumption will be much higher .

Disadvantages of Polling

  1. Information is only relatively new , but not absolutely new. For example, when a user deletes, adds, or updates a piece of information, if the update of your information status depends on the polling interface, you cannot call the polling interface immediately after the update is successful, because the polling interface may encapsulate For many module requests, the cost of updating immediately is very high, and a lot of information may not be necessary. My approach here is that after the front-end confirms that the information is updated successfully, it first displays fake data to the user (updated data constructed by the front-end). The premise is that the backend informs the frontend that the information is indeed updated successfully, and the next poll must return new data. This backend should still be guaranteed. Of course, you can also ask the back-end students to encapsulate a new interface and return you the data you need separately, but compared to writing fake data, it is clear at a glance which one has low development cost and low maintenance cost.

Summarize

Polling to obtain data is necessary in some scenarios, but it may be necessary to do some compatibility logic because the information is not updated in time.

Guess you like

Origin blog.csdn.net/ZhaoBuDaoFangXia/article/details/106600179