Return to site

How to use HTTP2 protocol in Spring Boot applications

How do I Enable HTTP2 in spring boot?

· Java,java app development

Technology: HTTP2 is the major release of the HTTP network protocol used by WWW (world wide web). It is the recent new version of HTTP Protocol as HTTP 1.1 was released in 1997. It was derived from SPDY protocol, originally developed by Google. All the major browsers like Chrome, Opera, Firefox, Safari, Edge browsers are supporting this protocol.

broken image

Advantages of HTTP2 over HTTP1.1

 

1. Multiplexing and Concurrency: For rendering the one HTML it may need some JS, CSS, Images files need to get from server for better look and feel, if we use HTTP 1.1 protocol after getting the HTML response client will request for each JS, CSS, Image files separately, which needs the one TCP connection for each of the request which was very costly. It was improved in HTTP2 protocol, instead of the client is requesting for several resources needed in a HTML Page, a server will push the response to the client so that with single TCP connection all content needed will be available to a client. This allows the server to supply the data needed for a web browser to render a web page without waiting for the first responders.

Add paragraph text here.

broken image

​2. Stream Prioritization: The client can indicate which if the resources are more important than others to a server. For providing the prioritization HTTP2 standards has a feature to provide associated weight and dependency (if the stream is depending on another stream).

  1. Each stream may assign an integer weight between 1-256.
  2. Each stream may provide explicit dependency on other streams.
  3. The combination of both dependencies and weights will allow a client to construct a prioritization tree that expresses how the client wants to receive the preferences. The server will use the information to prioritize the stream processing and controlling the system resources like CPU, memory and other resources, once the response is available then it will allocate the bandwidth to ensure the delivery of the high priority responses to the client. To hasten the load time of the page, all new browsers prioritize requests based on type of asset, their location on the page, and even learned priority from previous visit exp, if the rendering was blocked on a certain asset in a previous visit, then the same asset may be prioritized raised in the future.
3. Header Compression: Each HTTP transfer carries a set of headers that will describe the transferred resources and its properties. In HTTP1.1 all header values will have sent in a plain-text format which will take 500-800 bytes per each request, the size will increase if HTTP cookies are being used. HTTP2 provides a better solution for this overhead transfer of the data by using HPACK compression format using below technique.
  1. It allows the transmitted header fields to be encoded via a static Huffman code, which reduces their individual transfer size.

It also requires both server and client to maintain and update the indexed list of previously sent header fields which is then used as a reference to efficiently encode previously transmitted values. Huffman coding provides the specific values to be compressed when transferred, and the indexed list of before transferred values enables us to encode duplicate values by transferring index values that can be used to efficiently look up and rebuild the full header keys and values.

broken image

4. Server Push: The server will push all dependent resources which are not requested the client.

broken image

5. Flow Control: Flow control is the mechanism to prevent the sender from overwhelming with data to a receiver, it may not able to process the data because of many reasons like a receiver is busy, it is under heavy load, or it may not have allocated sufficient buffer space t process the data.

HTTP2 is addressing all these issues, by providing the receive window (rwnd), which communicates the size of transferring data between sender and receiver. When the connection is first established it will use default settings. If the server is streaming a large amount of data to a client in case of download client received window may become the limiting factor, similarly in case of the client send a large amount of data to the server, in this case, server receiving window will be limiting factor. In any case, a smaller window will be a limiting factor.

If the window reaches zero, it is considered as a hint that not sending more data without clearing the existing data in the buffer by app level. This workflow is available for every TCP Continues throughout the lifetime of the connection: Each ASK packet has the latest RW value for each side, dynamically switches data flow rates on the dynamics of the sender and receiver and the speed of the process.

broken image

HTTP/2 no longer supports HTTP 1.1's chunked shift encoding mechanism, as it gives its own, more effective, mechanisms for data streaming.

We will not need to change/update any of applications/API to work on HTTP2 properly, but our Java application development will work better and consume less resources on both client and server.

Enabling HTTP2 in Tomcat Server:

HTTP2 protocol is supported only after Tomcat 9 version.

Add below lines in conf/server.xml file:

<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol" maxThreads="150" SSLEnabled="true">

<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol"/>

<SSLHostConfig honorCipherOrder="false">

<Certificate certificateKeyFile="conf/ca.key" certificateFile="conf/ca.crt"/>

</SSLHostConfig>

</Connector>

Enable HTTP2 Support in Spring Boot:

You can allow HTTP2 in spring boot with the following property if the server has support for it.

server.http2.enabled=true

Conclusion: In the current blog we learned what is HTTP2 protocol, what are advantages over HTTP1.1 and how to enable to Tomcat and Spring boot applications.