HTTP/2 Server Push
HTTP/2 Server Push is an optional[1] feature of the HTTP/2 and HTTP/3 network protocols that allows servers to send resources to a client before the client requests them. Server Push is a performance technique aimed at reducing latency by loading resources preemptively, even before the client knows they will be needed.[2] In practice, Server Push frequently results in wasted bandwidth because the server rarely knows which resources are already loaded by the client and transmits the same resource multiple times, resulting in slowdowns if the resources being pushed compete for bandwidth with resources which were actually requested.[3]
HTTP/2 Server Push is not a notification mechanism from server to client. Instead, pushed resources are used by the client when it may have otherwise produced a request to get the resource anyway.[4][5]
History
On May 14, 2015, HTTP/2 was standardized by RFC 7540 ratified as a Proposed Standard. The document includes section 8.2 entitled "Server Push" which introduced the concept to the protocol as an optional extension. Google Chrome 40 became the first browser supporting the final standardized HTTP/2 version, including the optional Server Push.[6]
In February 2018, Nginx 1.13.9 was released with optional support for HTTP/2 Server Push.[7]
In October 2022, Google announced intent to remove Server Push from Google Chrome citing the poor performance of the extension in practice. Chrome 106 became the first release disabling Server Push by default.[8]
Basic concept
For example, in the case of a website with three resources: index.html, styles.css and script.js, when a user, through their browser, connects to Wikipedia webserver to get the home page, they automatically retrieve index.html. As the browser parses the HTML text in index.html, it finds instructions that will require styles.css and script.js. At that point, the browser will issue requests to get these other two files. To assemble the complete webpage, the browser will stack such requests as it gradually discovers the site's composition.
With HTTP/2 Push, the server can take the initiative by having rules that trigger content to be sent even before it is requested. In this example scenario, the server knows that anyone requesting index.html will need styles.css and script.js, so it can push them to the client immediately without waiting for the client to request them. If done correctly, by the time the browser finishes parsing index.html, the transfer of styles.css and script.js would have already started, or even completed, removing the latency of having to request these and wait for them to arrive.
How HTTP/2 PUSH works at a protocol level
Push works over HTTP/2, which at its core is a frames protocol, meaning that information is exchanged in groups of bytes called frames. Additionally, frames are part of streams, and streams are identified by a number. The stream number is present in each frame as a binary field. Streams allow matching requests to responses, e.g. the response to request GET /index.html at stream 3 must also be at stream 3.
There are different types of frames, and each has a different function. HTTP/2 has only a few of these types, and we don't need all of them to explain the basics. Here are the ones interesting for this description:
- HEADERS frame. As its name implies, this type of frame carries HTTP headers. When sent by the browser to the server, it signals that a request is being made. When sent by the server to the browser, it signals that a response to a previous request or push promise is being sent.
- PUSH_PROMISE frame. This frame is sent by the server to the browser to start pushing a resource. It also contains HTTP headers. However, the kind of headers present in a PUSH_PROMISE frame are headers that would normally be present in a request. This is different from the response headers that a server would normally send. The request URL, for example, is present in the PUSH_PROMISE frame as the HTTP/2-specific :path pseudo-header, as is the :authority pseudo-header to indicate a host. Other headers that may be present in a PUSH_PROMISE and that some browsers use are cache headers, for example, if-none-match.
- DATA frames. These frames are sent in either direction to carry the actual content of a resource or the contents that the browser POSTs or PUTs to the server.
- RST_STREAM frames. These frames serve many purposes. One of them is having the browser signal to the server that a pushed stream is not needed.
When the server wants to push a resource, it prepares a PUSH_PROMISE frame, architecting it in the best way possible to seduce the browser into using the pushed contents. Then the server annexes the PUSH_PROMISE frame to the response part of a normal, browser-initiated stream. However, the actual data for the pushed resource is sent in a fresh stream started by the server — and thus with an even number.
The browser holds the pushed data in a temporary "quarantine" zone until it decides to use it. Later, every time the browser is going to make an actual request, it examines the contents of any received push promises to see if it is similar enough to the request it wants to make. However, the server need not wait until that moment to start sending data for the promised resource. After the PUSH_PROMISE frame has been sent on the browser-initiated stream, the server can send what would be response headers using a HEADERS frame in the new server-initiated stream, and later it can send the data of the resource using DATA frames. And at any point in time, the browser can interrupt any transfer by using RST_STREAM.
Here is how this would work in the previous example. If the server is HTTP/2 PUSH ready, when it receives a request to index.html it can forecast that requests to styles.css and script.js are following close in tail. So it issues push promises to get a bit ahead of events. Here is how things could look, in order of occurrence and making up the stream numbers:
- Server receives HEADERS frame asking for index.html in stream 3, and it can forecast the need for styles.css and script.js.
- Server sends a PUSH_PROMISE for styles.css and a PUSH_PROMISE for script.js, again in stream 3. These frames are roughly equivalent to a browser's request.
- Server sends a HEADERS frame in stream 3 for responding to the request for index.html.
- Server sends DATA frame(s) with the contents of index.html, still in stream 3.
- Server sends HEADERS frame for the response to styles.css in stream 4 — notice the even stream number — and then for the response to script.js in stream 6.
- Server sends DATA frames for the contents of styles.css and script.js, using their respective stream numbers.
Push promises are sent as early as possible so that the browser will have them well ahead of any discoveries. Notice that HTTP headers (specifically Link with the 'preload' keyword[9]) can reveal URLs that the browser needs to fetch, and an eager browser would start asking for the resources upon seeing those headers. Therefore, push promises are best sent before even the response headers of the stream where they are attached.[10]
Implementations
Software | First supporting version | Last supporting version | |
---|---|---|---|
Nginx | Server | 1.13.9 (February 2018)[7] | 1.25.1 (June 2023)[11] |
LiteSpeed | Server | 5.2 | Still supported, but deprecated[12] |
Google Chrome | Client | Yes | 106 (September 2022)[3][8][13] |
Firefox | Client | Yes | Still supported |
According to the Google Chrome team, Server Push in HTTP/2 and gQUIC is rarely used, and the pushed resources are more often unused than used. They have proposed to remove the feature from Chrome and Chromium.
References
- Belshe, M.; Peon, R.; Thomson, M. (May 2015). Thomson, M (ed.). "Hypertext Transfer Protocol Version 2 (HTTP/2)". doi:10.17487/RFC7540.
A client can request that server push be disabled
{{cite journal}}
: Cite journal requires|journal=
(help) - Staff, Ars (2015-02-18). "HTTP/2 finished, coming to browsers within weeks". Ars Technica. Retrieved 2023-01-12.
- "Intent to Remove: HTTP/2 and gQUIC server push". Google Groups. 2020-11-11. Archived from the original on 2021-11-28. Retrieved 2022-04-12.
- "HTTP/2 server configurations". HTTP/2 Space. Archived from the original on 2022-03-27. Retrieved 2019-03-30.
- "Server Push". Hypertext Transfer Protocol Version 2 (HTTP/2). Internet Engineering Task Force. May 2015. p. 60. sec. 8.2. doi:10.17487/RFC7540. RFC 7540. Archived from the original on 2022-04-04. Retrieved 2015-05-06.
- "Google announces SPDY's coming demise as HTTP/2 approaches". Ars Technica. 2015-02-10. Retrieved 2023-07-30.
- "Changes with nginx 1.14.2". Nginx. 2018-12-04. Archived from the original on 2022-04-07.
- "Removing HTTP/2 Server Push from Chrome". Chrome Developers. 2022-08-18. Retrieved 2023-07-30.
- "Preload". GitHub. World Wide Web Consortium. 2022-03-29. Archived from the original on 2022-04-05. Retrieved 2022-04-12.
- "A closer look to HTTP/2 Push". ShimmerCat. 2018-04-25. Archived from the original on 2021-03-04.
- "Changes with nginx 1.25.1". Nginx. 2023-06-13. Retrieved 2023-07-12.
- "Page Optimization". LiteSpeed Documentation. Retrieved 2023-07-21.
- "Remove HTTP/2 push". chromestatus.com. 2022-10-31. Retrieved 2023-07-12.