Jetty 9 – The Next Generation Servlet Container
-
Upload
codemotion -
Category
Technology
-
view
4.423 -
download
0
description
Transcript of Jetty 9 – The Next Generation Servlet Container
Simone Bordet
Simone Bordet
Simone Bordet [email protected] @simonebordet
Open Source Contributor Jetty, CometD, MX4J, Foxtrot, LiveTribe, JBoss, Larex
Lead Architect at Intalio/Webtide Jetty's SPDY and HTTP client maintainer
CometD project leader Web messaging framework
JVM tuning expert
Simone Bordet
Agenda
Jetty Introduction
Web History and Evolution
Web Protocols
Jetty Features & Performance
The future: HTTP 2.0
Simone Bordet
Jetty
The Jetty Servlet Container Implemented the Servlet Specification since 1.0
Jetty 7 → Servlet 2.5
Jetty 8, 9 → Servlet 3.0
Jetty 9.1 → Servlet 3.1
Mostly known for its component-based architecture Very easy to embed Very easy to use for automated tests
Great for production too !
Simone Bordet
w3c.org 1996
1 HTML File, 600 Bytes
HTTP 1.0 protocol designed to serve these pages:
Simone Bordet
Evolution – I/O
Technologies evolved over the years
Scalability limited by blocking I/O
Advent of NIO (asynchronous I/O) Servers became more scalable, C10K now possible Jetty 6 – one of the first NIO Servlet containers
Jetty innovates: Jetty Continuations Portable way to suspend/resume requests
Jetty spun off the CometD project at http://cometd.org Scalable Web Messaging Framework
Servlet 3.0 incorporated Jetty Continuations concepts
Servlet 3.1 expands to asynchronous I/O
Simone Bordet
Evolution – CPU
Multicore CPUs everywhere
JDK enhanced with atomic primitives
Rethink locking for “mechanical sympathy” Jetty 9 core rewrite taking that in account Keep thread context switching at minimum Reduce/Eliminate false sharing Use concurrent data structures Avoid synchronized in favor of atomic state machines
Jetty 9.1 is 30+% faster than Jetty 8 And implements more features
Simone Bordet
w3c.org 2013
1 HTML, 33.5 KiB - 4 CSS, 12 KiB – 2 JS 31 KiB - 27 Images, 61.5 KiB
34 Resources, 138 KiB
Simone Bordet
Web Explosion
The Web Exploded Assumptions, protocols and technologies valid in 1996
are still our best choice ?
WE CAN DO BETTER !
Simone Bordet
HTTP Limits
HTTP is an old protocol Not bidirectional No multiplexing No resource correlation
Today's Web content forces too many hacks Comet techniques to fake bidirectionality Browsers to open multiple connections
Due to HTTP's lack of multiplexing TCP slow start gets in the way
Domain sharding imposes additional overhead on servers Would like to use pipelining, but cannot
Does not work reliably
Simone Bordet
WebSocket
Effort Initiated by Browser Vendors to Bidirectional low latency communications Replace Comet HTTP “hacks” Implement rich web applications within the browser
Standardized IETF – RFC 6455 wire protocol W3C – HTML 5 Javascript API JCP – JSR 356 Java API
Jetty 7 was one of the first Servlet containers to support WebSocket (2009)
Jetty 9.1 supports JSR 356
Simone Bordet
WebSocket Upgrade
Runs on port 80 (or 443 for wss)
Uses HTTP/1.1 Upgrade mechanism REQUEST
GET / HTTP/1.1 Host: localhost:8080 Origin: http://localhost:8080 Connection: Upgrade Upgrade: websocket Sec-WebSocket-Key: SbdIETLKHQ1TNBLeZFZS0g== Sec-WebSocket-Version: 13
RESPONSE
HTTP/1.1 101 Switching Protocols Connection: Upgrade Upgrade: websocket Sec-WebSocket-Accept: y4yXRUolfnFfo3Jc5HFqRHNgx2A=
Simone Bordet
WebSocket Performance
Jetty-7.6, CometD-2.4, chat application
WebSocket
Simone Bordet
WebSocket Deployment
Browsers Firefox 11 (Android 15) Internet Explorer 10 PP5 Chrome 16 (Android 18) Safari 6 (iOS 6) Opera 12.1 (Mini N/A) Blackberry 7.0 Jetty Client 7, 8, 9
Java Servers Jetty 7, Jetty 8, Jetty 9 Jetty 9.1 (JSR 356) Glassfish Resin 4 Tomcat 7 (alpha) Tomcat 8 (JSR 356)
73% browser coverage (mobile too)(http://caniuse.com/websockets)
Simone Bordet
SPDY is LIVE!
SPDY is a live experiment to improve HTTP Addresses HTTP 1.1 limits Designed to be faster and better than HTTP
Already widely deployed! Google, Twitter, Facebook, WordPress, Webtide, etc. Chrome & Firefox & IE 11
The SPDY protocol replaces HTTP on the wire But it's transparent for applications
Simone Bordet
SPDY & Jetty
Jetty 7 & Jetty 8 SPDY added by mocking HTTP wire protocol Multiplexing requires mock protocol per channel
Jetty 9 Re-architected to separate wire protocol from semantics HTTP semantics shared between HTTP, SPDY & FastCGI HTTP, SPDY, WebSocket, TLS 1st class citizens
Use SPDY now! ZERO changes to your existing web applications Your use case can make Jetty, SPDY & HTTP 2.0 better
Simone Bordet
SPDY Protocol Initiation
SPDY uses TLS (aka SSL) connection on port 443 TLS extended with Next Protocol Negotiation
New framing protocol over TLS Intermediaries don't know it is not HTTP wrapped in TLS
Transports existing HTTP Semantics
(GET, POST, HEAD etc) Client and Server application don't know it is not HTTP
Simone Bordet
SPDY Multiplexing
Multiplexing is built-in
SYN_STREAM
SYN_REPLY
SYN_STREAM
SYN_REPLY
SYN_STREAM
SYN_REPLY
SYN_STREAM
SYN_REPLY DATA
DATA
Simone Bordet
SPDY Multiplexing
Multiplexing Allows to make better use of TCP connections
Reduces TCP slow start
Uses less resources on the server Responses may be sent out of order
Without waiting for slow responses
Key point: reducing round trip waits Caused by limited connections and lack of multiplexing
Simone Bordet
SPDY Push
SPDY Push Server pushes secondary resources that are associated
to a primary resource Works in collaboration with browser's cache for a better
user experience
Jetty is one of the first SPDY servers that provides automated push functionalities Totally transparent for applications Based on the “Referrer” header
To associate primary and secondary resources
Using “If-Modified-Since” header to avoid unnecessary pushes
Simone Bordet
SPDY Push
index.html
style.cssapplication.js
image1.png
PushCache
index.htmlappliction.jsstyle.cssImage1.png
HTTP/1.1
SPDY/3 + PUSH
Simone Bordet
SPDY Clients
Desktop Firefox 13 Chrome 4 Opera 12.1 Jetty Client 7, 8, 9
Mobile Android Browser 4.1 Chrome for Android 18.0 Firefox Android 15.0
55% browser coverage (mobile too)(http://caniuse.com/spdy)
Simone Bordet
Jetty 9.1
Polyglot (HTTP, WebSocket, SPDY, FastCGI)
Servlet 3.1 ready
Great as a production server too Not a monolithic server
Pick only the modules you need
Tons of configuration possibilities Easy to customize
“Jetty was one of the first servers to ...” Early adopters find the edge features they want in Jetty Late adopters benefit of the experience Jetty gathered
Simone Bordet
Jetty 9.1 Performance
Great performance Easy to push it to 70k or more requests/s
These are Servlet requests not just http ones
Easy to get it to 250k connections
Servlet 3.1 on par with Servlet 3.0 But with less threads
Intalio/Webtide commercial support
Simone Bordet
Servlet 3.1
public void service(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
AsyncContext asyncContext =
request.startAsync(request, response);
asyncContext.setTimeout(0);
ReadListener reader = new AsyncReader(asyncContext);
ServletInputStream input = request.getInputStream();
input.setReadListener(reader);
}
Simone Bordet
Servlet 3.1
public class AsyncReader implements ReadListener {
private final AsyncContext asyncContext;
...
public void onDataAvailable() throws IOException {
ServletInputStream input =
asyncContext.getRequest().getInputStream();
...
}
public void onAllDataRead() throws IOException {
...
}
}
Simone Bordet
Servlet 3.1
public class AsyncWriter implements WriteListener {
private final AsyncContext asyncContext;
...
public void onWritePossible() throws IOException {
ServletOutputStream output =
asyncContext.getResponse().getOutputStream();
...
if (finishedWriting)
asyncContext.complete()
}
}
Simone Bordet
Servlet 3.1
public class SyncServlet extends HttpServlet { protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { byte[] buffer = new byte[BUFFER_SIZE]; while (true) { int read = request.getInputStream().read(buffer, 0, BUFFER_SIZE); if (read < 0) break; response.getOutputStream().write(buffer, 0, read); } } }
public static class AsyncServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { AsyncContext asyncContext = request.startAsync(request, response); asyncContext.setTimeout(0); Echoer echoer = new Echoer(asyncContext); request.getInputStream().setReadListener(echoer); response.getOutputStream().setWriteListener(echoer); }
private class Echoer implements ReadListener, WriteListener { private final byte[] buffer = new byte[BUFFER_SIZE]; private final AsyncContext asyncContext; private final ServletInputStream input; private final ServletOutputStream output; private boolean complete;
private Echoer(AsyncContext asyncContext) throws IOException { this.asyncContext = asyncContext; this.input = asyncContext.getRequest().getInputStream(); this.output = asyncContext.getResponse().getOutputStream(); }
@Override public void onDataAvailable() throws IOException { while (input.isReady()) { int read = input.read(buffer);
output.write(buffer, 0, read);
if (!output.isReady()) return; }
if (input.isFinished()) { complete = true; asyncContext.complete(); } }
@Override public void onAllDataRead() throws IOException { }
@Override public void onWritePossible() throws IOException { if (input.isFinished()) { if (!complete) asyncContext.complete(); } else { onDataAvailable(); } }
@Override public void onError(Throwable failure) { failure.printStackTrace(); } } }
Echo Servlet Async I/OEcho Servlet Blocking I/O
Simone Bordet
Jetty 9.1
Jetty HttpClient High Performance HttpClient with fluent API
70% faster than Jetty 8's HttpClient
Pluggable transports (HttpClient can speak SPDY)
HTTP Proxy
SPDY Proxy
Jetty FastCGI Jetty can serve PHP / Python / Ruby / etc.
Simone Bordet
HTTP 2.0
HTTP 2.0 work has started IETF HTTPbis working group rechartered
Two proposals received: Google - SPDY as the basis Microsoft – WebSocket framing with HTTP semantics
SPDY Adopted as the starting point Unlikely to require TLS or NPN Will define precise interactions with proxies
Early draft available http://tools.ietf.org/html/draft-ietf-httpbis-http2-06
Simone Bordet
Conclusions
The Web is evolving Web Protocols are evolving Servers must keep the pace with new Web Protocols
Try WebSocket today Production ready
Try SPDY today Check your application is ready for the future Check the future is ready for your application