Alex Theedom @readlearncode
Java EE 8:
What Servlet 4.0 and
HTTP/2 mean?
@readlearncode readlearncode.com
Who am I?
Alex Theedom
Author
Trainer
Blogger
Speaker
What’s on?
@readlearncode readlearncode.com
• Why Do We Need HTTP/2
• Workarounds to HTTP1.1
• HTTP Sockets
• Topline HTTP/2 Features
• Servlet 4.0 Features
• Server Support
• Status Update
• Extract Bits
• Q&A
What’s on?
Why Do We Need HTTP/2?
@readlearncode readlearncode.com
• Increase perceived performance of web
• HTTP protocol not suitable
• Since May 2012 web page size increased 250%
• The problem with HTTP/1.1
Source: HTTPArchive.com
Why Do We Need HTTP/2?The Goal of HTTP/2
@readlearncode readlearncode.com
• Requests resources in parallel HTTP 1.0
• One request per TCP connection
• HTTP1.1 Pipelining: multiple requests
• Responds in sequence
• Delay causes head-of-line blocking
How a browser loads a webpage?
open
close
client server
no pipelining
index.html
style_1.css
logo.jpg
open
close
client server
pipelining
time
index.html
style_1.css
logo.jpg
Why Do We Need HTTP/2?
Workarounds
@readlearncode readlearncode.com
• Multiple connections
• Acceptable but has issues
• TCP sockets expensive
• Browser max connections
Workarounds to HTTP1.1Solution to Head-Of-Line Blocking
open
close
client server
connection 1
style_1.css
open
close
client server
connection 2
javaScript_1.js
open
close
client server
connection 3
image_1.png
@readlearncode readlearncode.com
Workarounds to HTTP1.1CSS/JavaScript File Concatenation
background.css
header.css
menu.css
style.css
@readlearncode readlearncode.com
Workarounds to HTTP1.1CSS and JavaScript Inlining
…
@readlearncode readlearncode.com
• Embed image in web page
• Base 64 encoded
• Time spent decoding
• Caching difficult
Workarounds to HTTP1.1Inlined Assets
@readlearncode readlearncode.com
• One image file consists of many smaller images
Workarounds to HTTP1.1Image Sprite Sheet
Image sprites from Amazon, Google and Facebook.
@readlearncode readlearncode.com
Workarounds to HTTP1.1Domain Sharding
web page
y.example.com
x.example.com
domain 2
domain 1
logo.jpg
icon.jpg
header.css
menu.css
HTTP Sockets
@readlearncode readlearncode.com
• Not much specified
• Two maximum open sockets (ignored by browsers)
• Throw away resources
HTTP SocketsWhat HTTP1.1 Says About Sockets
@readlearncode readlearncode.com
• Much is specified
• Scares resources
• Only open one socket
HTTP SocketsWhat HTTP/2 Says About Sockets
Topline HTTP/2 Features
@readlearncode readlearncode.com
• HTTP/2 is comprised of two specifications
• Hypertext Transfer Protocol version 2 - RFC7540
• HPACK - Header Compression for HTTP/2 - RFC7541
• Binary Protocol Based on Frames
Topline HTTP/2 FeaturesWhat’s new
@readlearncode readlearncode.com
• Request/Response Multiplexing
• Binary Framing
• Header Compression
• Stream Prioritization
• Server Push
Topline HTTP/2 FeaturesFeatures
@readlearncode readlearncode.com
• Most important feature• Request and response is multiplexed
• Fully bi-directional communication
• Concepts• Connection - A TCP socket
• Stream – A channel of communication
• Message – A request/response and control message
• Frame –The smallest unit within a communication
• Resolves head-of-line blocking
Topline HTTP/2 FeaturesRequest/Response Multiplexing
@readlearncode readlearncode.com
• Hierarchical structure of logical communication blocks
Topline HTTP/2 FeaturesRequest/Response Multiplexing
connection
stream
frame frame frame
frame frame frame
message
stream
frame frame frame
frame frame frame
message
frame frame frame
frame frame frame
message
stream
frame frame frame
frame frame frame
message
frame frame frame
frame frame frame
message
frame frame frame
frame frame frame
message
@readlearncode readlearncode.com
• Interweave the logical stream over a single TCP
Topline HTTP/2 FeaturesRequest/Response Multiplexing
@readlearncode readlearncode.com
• Decomposition of the frame
• Type fields can be• HEADERS corresponds to the HTTP headers
• DATA corresponds to the HTTP request body
• PUSH_PROMISE server notifies of push intent
• RST_STREAM notifying error, client rejects push
• PRIORITY specifies stream priority
• SETTING, PING, GOAWAY, WINDOW_UPDATE, CONTINUATION
Topline HTTP/2 FeaturesBinary Framing
@readlearncode readlearncode.com
• Mapping the HTTP Request to Frames
Topline HTTP/2 FeaturesHeader Compression
HTTP request Header Frame
GET /index.html HTTP/1.1
Host: example.com
Accept: text/html
HEADERS
+ END_STREAM
+ END_HEADERS
:method: GET
:scheme: http
:path: /index.html
:authority: example.com
accept: text/html
@readlearncode readlearncode.com
• Mapping the HTTP Response to Frames
Topline HTTP/2 FeaturesHeader Compression
HTTP response Frames
HTTP/1.1 200 OK
Content-Length: 11
Content-Type: text/html
May The Force Be With You
HEADERS
- END_STREAM
+ END_HEADERS
:status: 200
content-length: 11
content-type: text/html
DATA
+ END_STREAM
May The Force Be With You
@readlearncode readlearncode.com
Topline HTTP/2 FeaturesHPACK header compression
HTTP Request 1
:method GET
:scheme https
:host example.com
:path /index.html
:authority example.org
:accept text/html
user-agent Mozilla/5.0
HTTP Request 2
:method GET
:scheme https
:host example.com
:path /info.html
:authority example.org
:accept text/html
user-agent Mozilla/5.0
HEADERS frame (Stream 1)
:method GET
:scheme https
:host example.com
:path /index.html
:authority example.org
:accept text/html
user-agent Mozilla/5.0
HEADERS frame (Stream 3)
:path /info.html
@readlearncode readlearncode.com
• Attach priority information to streams
• Priority located in the header frame or the priority frame
• Only a suggestion to the server
Topline HTTP/2 FeaturesStream Prioritization
B D C
A
2 14 10
B C
A
4 8
@readlearncode readlearncode.com
• Eliminate the need for resource inlining
• The sever can proactively send resources to the client
• Client can reject PUSH_PROMISE by responding
RST_STREAM
Topline HTTP/2 FeaturesServer Push
Servlet 4.0 Features
@readlearncode readlearncode.com
Servlet 4.0 FeaturesServer Push
• Most visible improvements in servlets
• Best place to know what resources a request needs
@readlearncode readlearncode.com
Servlet 4.0 FeaturesPushBuilder
@readlearncode readlearncode.com
• Browser requests index.html
• Server discovers need for css and js
• Get PushBuilder from HTTP request
• Set path to css and invoke push
• Set path to js and invoke push
• Then responds with index.html
• The PushBuilder can be reused
Servlet 4.0 FeaturesTypical Journey
@readlearncode readlearncode.com
Servlet 4.0 FeaturesPushBuilder Dive Deeper
• Constructed with request method set to GET by default
• Conditional, range, expectation, authorization and request headers
are removed• Cookies are only added if the maxAge has not expired
• Only required setting is the URI path to the resource• Must be set before every call to push()
@readlearncode readlearncode.com
• Simple usage
Servlet 4.0 FeaturesServlets and ServerPush
@readlearncode readlearncode.com
Servlet 4.0 FeaturesFilters and Server Push
@readlearncode readlearncode.com
• Web framework use case most important
• Dependent on knowing the resources the client requires
• Frameworks best placed to take advantage of server push
• JSF users get server push for free
Servlet 4.0 FeaturesJSF Use Case
@readlearncode readlearncode.com
• SETTINGS_ENABLE_PUSH clients disable server push
• RST_STREAM rejects cached resources
• Servlet containers must honour request to not receive
Servlet 4.0 FeaturesDisable/Reject Server Push
@readlearncode readlearncode.com
• Perfectly backward compatible
• Rework to take advantage of Server Push
• Consider removing frontend workarounds
• JSF developers do nothing
Servlet 4.0 FeaturesBackward Compatible
Server Support
@readlearncode readlearncode.com
• GlassFish 5.0 (nightly)
Reference implementation
• Payara 5.0
Has a branch for Java EE 8 development
• Jetty Stable-9 (9.4.5.v20170502)
org.eclipse.jetty.servlets.PushCacheFilter/PushBuilder
Server ImplementationServlet Support
@readlearncode readlearncode.com
• WildFly 10 (Undertow)
Initial PushBuilder support implemented in Undertow master
• Tomcat 9.0.0.M20
PushBuilder support in thejavax.servlets.http package
• Netty 4.1
HTTP/2 implementation takes full advantage of headline
features
Server ImplementationServlet Support
Status Update
@readlearncode readlearncode.com
• As of 23 May 2017 in Public Review ballot status
• Issue Tracker: github.com/javaee/servlet-spec/issues
• JCP Specification: jcp.org/en/jsr/detail?id=369
• Twitter: @servlet_spec
Status UpdateJSP 369: Servlet Specification
Extra Bits
@readlearncode readlearncode.com
• The goal of HTTP/2 is to improve performance
• Cloudflare HTTP/2 demonstration tool
www.cloudflare.com/http2
• Anthum's HTTP vs HTTPS
www.httpvshttps.com
Performance TestsShow me the performance
@readlearncode readlearncode.com
• Daniel Stenberg book: bagder.gitbooks.io/http2-explained
• Free O’Reilly e-book:
www.oreilly.com/webops-perf/free/HTTP2-high-perf-
browser-networking.csp
• FAQ: http2.github.io/faq
Useful Resources
@readlearncode readlearncode.com
Q & A
Alex Theedom @readlearncode
Java EE 8:
What Servlet 4.0 and
HTTP/2 mean?