Jetty 9 – The Next Generation Servlet Container

40
Simone Bordet [email protected] Jetty 9 The Next-Generation Servlet Engine

description

Jetty has always been known to be a technology leader in several areas—from Jetty Continuations (later standardized as Servlet 3 asynchronous servlets) to WebSocket to SPDY—delivering exceptional production performance. Jetty 9 is not only a great production server but provides features such as the Jetty Maven plug-in and embedded Jetty that help you in application development and integration testing. This session covers Jetty 9’s scalability, stability, performance, and features such as the new HTTP client and how to configure Jetty for optimal production performance.

Transcript of Jetty 9 – The Next Generation Servlet Container

Page 1: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

Jetty 9

The Next-Generation Servlet Engine

Page 2: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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

Page 3: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

Agenda

Jetty Introduction

Web History and Evolution

Web Protocols

Jetty Features & Performance

The future: HTTP 2.0

Page 4: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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 !

Page 5: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

History&

Evolution

Page 6: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

w3c.org 1996

1 HTML File, 600 Bytes

HTTP 1.0 protocol designed to serve these pages:

Page 7: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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

Page 8: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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

Page 9: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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

Page 10: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

cnn.com 2013

172 Resources, 1.2 MiB

Page 11: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

Web Explosion

The Web Exploded Assumptions, protocols and technologies valid in 1996

are still our best choice ?

WE CAN DO BETTER !

Page 12: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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

Page 13: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

WebSocket

Page 14: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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

Page 15: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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=

Page 16: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

HTTP Performance

Jetty-7.6, CometD-2.4, chat application

HTTP

Page 17: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

WebSocket Performance

Jetty-7.6, CometD-2.4, chat application

WebSocket

Page 18: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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)

Page 19: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

SPDY

Page 20: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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

Page 21: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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

Page 22: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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

Page 23: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

SPDY Multiplexing

Multiplexing is built-in

SYN_STREAM

SYN_REPLY

SYN_STREAM

SYN_REPLY

SYN_STREAM

SYN_REPLY

SYN_STREAM

SYN_REPLY DATA

DATA

Page 24: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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

Page 25: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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

Page 26: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

SPDY Push

index.html

style.cssapplication.js

image1.png

PushCache

index.htmlappliction.jsstyle.cssImage1.png

HTTP/1.1

SPDY/3 + PUSH

Page 27: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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)

Page 28: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

DEMO

Page 29: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

Jetty 9.1

Page 30: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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

Page 31: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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

Page 32: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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);

}

Page 33: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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 {

...

}

}

Page 34: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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()

}

}

Page 35: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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

Page 36: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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.

Page 37: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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

Page 38: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

Conclusions

Page 39: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

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

Page 40: Jetty 9 – The Next Generation Servlet Container

Simone Bordet

[email protected]

Questions&

Answers