Breaking

Wednesday, May 6, 2015

HTTP/2: A jump-start for Java developers Part-1

How the next-generation internet communication protocol supports extremely responsive Java internet applications


HTTP/2 was approved in Gregorian calendar month 2015 because the successor to the first internet communication protocol. whereas it's within the last stages of closing, the quality has already been enforced by early adopters like groyne and Netty, and can be incorporated into Servlet four.0. ascertain however HTTP/2 renovates HTTP's text-based protocol for higher latency, then see techniques like server push, streaming, multiplexing, and header compression enforced in a very client-server example exploitation groyne.

High-speed browser networking

In the period of time of the net, net affiliation information measure was the foremost necessary limiting issue for a quicker browsing expertise. That has modified within the years since, and of late several shoppers use broadband technologies for net access. As of 2014, Akamai's State of the web report showed that the typical affiliation speed for purchasers within the us exceeded eleven Mbit/s.

As net affiliation speeds have increased , the importance of latency to internet application performance has become additional apparent. once the net was new, the delay in causing asking and looking forward to a response was abundant but the full time to transfer all of the response knowledge, however these days that's now not the case. "High information measure equals high speed" is not any longer a sound maxim, however that does not mean we are able to ignore the importance of information measure. to be used cases that need bulk knowledge transfer like video streaming or massive downloads, information measure remains a roadblock. In distinction to web content, these forms of content use long-running connections, that stream a relentless flow of knowledge. Such use cases ar information measure sure generally.

Bandwidth determines how briskly knowledge may be transferred over time. it's the number of knowledge which will be transferred per second. you'll equate information measure to the diameter of a water pipe: with a bigger diameter additional water may be carried. For simply this reason information measure is incredibly necessary for media streaming and bigger downloads.

Latency is that the time it takes knowledge to travel between a supply and destination. Given associate empty pipe, latency would live the time taken for water to travel through the pipe from one finish to the opposite.

Downloading an internet page is like moving water through a duplex empty pipeline. In fact, you're passing knowledge through a network affiliation, wherever the request knowledge travels through the tip user's aspect of the affiliation to the server's aspect. Upon receiving the request the server sends response knowledge through a similar duplex affiliation. the full latency time it takes for knowledge to travel from one finish of the affiliation to the opposite and back once more is termed the round-trip time (RTT).

Latency is affected  by the speed of sunshine. as an example, the gap between metropolis and Paris is more or less 7900 km/4900 miles. The speed of sunshine is sort of three hundred km/ms. this suggests you'll ne'er get a stronger RTT than ~50 milliseconds for a affiliation between metropolis and Paris while not dynamical the laws of physics. In observe you'll get round-trip times that ar abundant higher thanks to the refraction effects of the glass fiber cable and therefore the overhead of alternative network elements. in line with Akamai's network performance comparison monitor, the RTT for the general public transatlantic link between metropolis and Paris in August 2014 was ~150 ms. (Please note, however, that this does not embrace the last-mile latencies.)

What will latency mean for associate application user? From a usability perspective, associate application can feel instant if user input is provided among a hundred ms. Responses among one second will not interrupt the user's flow of thought generally, however they're going to notice the delay. A delay longer than ten seconds are perceived as a non-responsive or broken service.

This means extremely responsive applications ought to have a latency of but one second. for immediate responsiveness you must aim for a latency among a hundred milliseconds. within the period of time of the web web-based applications were aloof from being extremely responsive.

Latency within the HTTP protocol
HTTP 0.9


The original HTTP version zero.9, outlined in 1991, failed to think about latency an element in application responsiveness. so as to perform associate HTTP zero.9 request you had to open a replacement communications protocol affiliation, that was closed by the server once the response had been transmitted. to determine a replacement affiliation, communications protocol uses a trilateral handclasp, which needs an additional network roundtrip before knowledge may be exchange. That further handclasp roundtrip would double the minimum latency of the Dallas-Paris link in my previous example.

HTTP 0.9 could be a terribly easy text-based protocol as you'll see below. In Listing one, I actually have used telnet on the client-side to question an internet page self-addressed by http://www.1and1.com/web-hosting. The telnet utility could be a program that permits you to determine a affiliation to a far off server and to transfer raw network knowledge.

Listing 1. HTTP 0.9 request-response exchange.

$ telnet www.1and1.com 80
Trying 74.208.255.133...
Connected to www.1and1.com.
Escape character is '^]'.

GET /web-hosting
<html&gt
<head&gt
<title&gtThe page is temporarily unavailable</title&gt
<style&gt
body { font-family: Tahoma, Verdana, Arial, sans-serif; }
</style&gt
</head&gt
<body bgcolor="white" text="black"&gt
<table width="100%" height="100%"&gt
<tr&gt
<td align="center" valign="middle"&gt
The page you are looking for is temporarily unavailable.<br/&gt
Please try again later.
</td&gt
</tr&gt
</table&gt
</body&gt
</html&gt
Connection closed by foreign host.





An HTTP 0.9 request consists of the word GET, a space, and the document address terminated by a CR LF (carriage return, line feed) pair. The response to the request is a message in HTML, terminated by the closing of the connection by the server.

HTTP 1.0

Released in 1996, HTTP 1.0 expanded HTTP 0.9 with extended operations and richer meta-information. The HEAD and POST methods were added and the concept of header fields was introduced. The HTTP 1.0 header set also included the Content-Length header field, which noted the size of the entity body. Instead of indicating the end of a message by terminating the connection, you could use the Content-Length header for that purpose. This was a beneficial update for at least two reasons: First, the receiver could distinguish a valid response from an invalid one, where the connection would break down while the entity body was streaming. Second, connections did not necessarily need to be closed.
In Listing 2 the response message includes a Content-Length field. Additionally, the request message includes a User-Agent header field, which is typically used for statistical purposes and debugging.

Listing 2. HTTP/1.0 request-response exchange

$ telnet www.google.com 80
Trying 173.194.113.20...
Connected to www.google.com.
Escape character is '^]'.

GET /index.html HTTP/1.0
User-Agent: CERN-LineMode/2.15 libwww/2.17b3

HTTP/1.0 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: http://www.google.de/index.html?gfe_rd=cr&ei=X2knVYebCaaI8QfdhIDAAQ
Content-Length: 268
Date: Fri, 10 Apr 2015 06:10:39 GMT
Server: GFE/2.0
Alternate-Protocol: 80:quic,p=0.5

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.de/index.html?gfe_rd=cr&ei=X2knVYebCaaI8QfdhIDAAQ">here</A>.
</BODY></HTML>

Connection closed by foreign host.


In contrast to HTTP 0.9 the response of a message begins with a status line. The response header fields allow the server to pass additional information about the response. The entity body is separated from the header by an empty line.
Even though the functionality became much more powerful with HTTP 1.0, it didn't do much for better latency. HTTP 1.0 still required a new TCP connection for each request, so each request added the cost of setting up a new TCP connection.

HTTP/1.1

With HTTP/1.1 persistent connections became the default, removing the need to initiate a new TCP connection for each request. The HTTP connection in Listing 3 remains open after receiving a response and can be re-used for the next request. (The last line "Connection closed by foreign host" is missing.)

Listing 3. HTTP/1.1 request-response exchange

$ telnet www.google.com 80
Trying 173.194.112.179...
Connected to www.google.com.
Escape character is '^]'.

GET /index.html HTTP/1.1
User-Agent: CERN-LineMode/2.15 libwww/2.17b3
Host: www.google.com:80

HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: http://www.google.de/index.html?gfe_rd=cr&ei=hW4nVYy_D8OH8QeKloG4Bg
Content-Length: 268
Date: Fri, 10 Apr 2015 06:32:37 GMT
Server: GFE/2.0
Alternate-Protocol: 80:quic,p=0.5

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.de/index.html?gfe_rd=cr&ei=hW4nVYy_D8OH8QeKloG4Bg">here</A>.
</BODY></HTML> 

Making persistent connections the norm in HTTP/1.1 does much to improve latency. Re-using persistent connections to the same server makes succeeding request-response exchanges much cheaper. Re-using open connections also removes the overhead of the TCP handshake. The HTTP/1.1 protocol enablWeb eb application developers to call the same server multiple times within a singWeb eb session, especially fWeb eb pages featuring linked resources such as images.

Challenges in HTTP/1.1

Upon receivingWeb eb page,Web eb browser starts to load the embedded page elements. Typically, the browser will load linked resources in parallel to reduce the total latency of page loading. The browser has to use multiple connections in parallel because connections cannot be re-used before a response is received. In order to improve the total web-page loading time the browser must use quite a few connections in parallel.
Using parallel persistent connections is not engough to improve latency, however, because connections are not free. A dedicated connection consumes significant resources on both the client and server side. Each open connection can consume up to a dedicated thread or process on the server side, depending on the HTTP server in use. For this reason popular browsers do not allow more than eight connection in the same domain.

HTTP/1.1 attempts to resolve this issue via HTTP pipelining, which specifies that the next request can be sent before the response has been received. This is not a perfect solution, however. Because the server must send responses in the same order that requests are received, large or slow responses can block others responses behind it.

Introducing HTTP/2

HTTP/2 addresses latency issues by providing an optimized transport mechanism for HTTP semantics. A major goal of HTTP/2 was to maintain high-level compatibility with HTTP/1.1. Most of HTTP/1.1's high-level syntax -- such as methods, status codes, and header fields -- is unchanged. HTTP/2 does not obsolete HTTP/1.1's message syntax, and it uses the same URI schemes as HTTP/1.1. Because the two protocols share the same default port numbers you can use HTTP/1.1 or HTTP/2 over the same default port.

The raw network protocol for HTTP/2 is completely different from the protocol for HTTP 1.1. HTTP/2 is not a text-based protocol. Instead, it defines a binary, multiplexed network protocol. Telnet-based debugging will not work for HTTP/2. Instead you could use the popular command-line tool curl or another HTTP/2-compatible client.

The basic protocol unit of HTTP/2 is a frame. In HTTP/2, frames are exchanged over a TCP connection instead of text-based messages. Before being transmitted an HTTP message is split into individual HTTP/2 frames. HTTP/2 provides different types of frames for different purposes, such as HEADERS, DATA, SETTINGS, or GOAWAY frames.
When establishing an HTTP connection the server has to know which network protocol to use. There are two ways to inform the server that it should use HTTP/2.

1. Server upgrade to HTTP/2

The first way to initiate an HTTP/2 protocol response is to use the HTTP Upgrade header. In this case the client would begin by making a clear-text request, which would later be upgraded to the HTTP/2 protocol version.

Listing 4. Upgrade HTTP request

GET /index.html HTTP/1.1
Host: server.example.com
Connection: Upgrade, HTTP2-Settings
Upgrade: h2c
HTTP2-Settings: <base64url encoding of HTTP/2 SETTINGS payload>

An HTTP/2-compatible server would accept the upgrade with a Switching Protocols response. After the empty line terminating the 101 response, the server would begin sending HTTP/2 frames.

Listing 5. Switching Protocols HTTP response

HTTP/1.1 101 Switching Protocols
Connection: Upgrade
Upgrade: h2c
[ HTTP/2 connection ...

2. ALPN

The second way to establish an HTTP/2 connection is to work with prior knowledge. For Transport Layer Security or TLS-based connections you could use the Application-Layer Protocol Negotiation (ALPN) extension. ALPN allows a TLS connection to negotiate which application-level protocol will be running across it.

After establishing a new HTTP/2 connection each endpoint has to send a connection preface as a final confirmation and to establish the initial settings for the HTTP/2 connection. For instance, both the client and the server will send a SETTING frame that includes control data such as the maximum frame size or header-table size.

In Listing 6 I have used Jetty's low-level HTTP/2 client to create a Session instance that represents the client-side endpoint of an HTTP/2 connection to a server.

Source

No comments:

Post a Comment