http/2 comes to java

52
Devoxx UK June 2015 David Delabassee @delabassee Oracle Copyright © 2015, Oracle and/or its affiliates. All rights reserved. HTTP/2 Comes to Java 1

Upload: david-delabassee

Post on 03-Aug-2015

4.736 views

Category:

Software


0 download

TRANSCRIPT

Page 1: HTTP/2 Comes to Java

Devoxx  UK  June  2015  

David  Delabassee  -­‐  @delabassee  Oracle  

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

HTTP/2  Comes  to  Java

1

Page 2: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. @delabassee#HTTP/2

Agenda

Why  HTTP/2?  

HTTP/2  

HTTP/2  and  Java  EE  

HTTP/2  and  Java  SE  

Summary

Page 3: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. @delabassee#HTTP/2

Agenda

Why  HTTP/2?  

HTTP/2  

HTTP/2  and  Java  SE  

HTTP/2  and  Java  EE  

Summary

Page 4: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 4 @delabassee#HTTP/2

Data  Never  Sleeps  2.0

https://www.domo.com/learn/data-­‐never-­‐sleeps-­‐2

Page 5: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 5 @delabassee#HTTP/2

HTTP  1.x

1991   1996   1999   2009   2015  

HTTP/0.9  HTTP/1.0  

HTTP/1.1  SPDY  

HTTP/2.0  

Page 6: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 6 @delabassee#HTTP/2

Page 7: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 7 @delabassee#HTTP/2

Page 8: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 8 @delabassee#HTTP/2

Page 9: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 9 @delabassee#HTTP/2

Page 10: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 10 @delabassee#HTTP/2

Page 11: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 11 @delabassee#HTTP/2

Today

index.html

style1.css

style2.css.  .  .  

script1.js

script9.js

pic1.jpg

pic8.jpg

.  

.  

.  

photo1.png

photo2.png

.  

.  

.  

Page 12: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 12 @delabassee#HTTP/2

google.fr  (1st)  • 3  HTTP  Requests  • 21.355  bytes  

- 1  HTML  

- 1  image  

- 1  .js

Top  .FR  examples

lefigaro.fr  (15th)  • 140  HTTP  Requests  • 2.736.562  bytes  

- 1  HTML  

- 130  images  

- 6  .js  - 2  .css  

Page 13: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 13 @delabassee#HTTP/2

• If  a  page  takes  longer  than  4  sec  to  load,  1/4  people  abandons  that  page  (1)  • 57%  of  consumers  will  abandon  a  page  that  takes  longer  than  3  sec  to  load  (2)  

• Page  load  slowdown  of  1  sec  could  cost  Amazon  $1.6  billion  in  sales  a  year  (1)  

• Slowing  search  results  by  0.4  sec,  Google  could  lose  8  million  searches  per  day  (1)

Latency  Vs.  Conversion  RateTime  is  Money!

(1)  http://www.fastcompany.com/1825005/how-­‐one-­‐second-­‐could-­‐cost-­‐amazon-­‐16-­‐billion-­‐sales  (2)  RadView  Spring  2015  State  of  the  Union:  Ecommerce  Page  Speed  &  Web  Performance

Page 14: HTTP/2 Comes to Java

@delabassee#HTTP/2 Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

HTTP  1.1Head-­‐of-­‐Line  blocking

14

Client Server

index.htmlindex.html

style1.css  style2.css  script1.js  

...

style1.css  style2.css  script1.js  

...

Page 15: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 15 @delabassee#HTTP/2

HTTP  1.1

Page 16: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 16 @delabassee#HTTP/2

File  Concatenation  and  Image  Sprites

• Modern  web  page  consists  of  +90  resources  fetched  from  15  distinct  hosts  (http://httparchive.org)  • TCP  Efficiency  Improves  with  Larger  Files  • Shoving  more  than  one  logical  file  into  one  physical  file

Page 17: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 17 @delabassee#HTTP/2

File  Concatenation  and  Image  Sprites

Page 18: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 18 @delabassee#HTTP/2

HTTP  1.1Workaround  -­‐  Domain  Sharding

Page 19: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 19 @delabassee#HTTP/2

Asset  inlining

…  <img  src="data:image/gif;base64,R0lGODlhEAAOexs3eeALMAAOazToeHh0tLS/7LZv0jvb29tf3Ubge8WSLrhf3kdbW1mxsjkhfkjfhGHhcjGDSHJUYgJvhgtyrHgfGfHyt56HGfGH56ge8WSLf6GGHhfkjfhhfkjfhpBREzxvt6QAA4lsjkhfkjfhdxwqBnuIoYtyGhBKoOjJj6GGHvvhdsbxus38GV3pBREzxvt6QAA4lsjkhfkjfhdxwqBnuIoYtyGhBKoOjJj6GGHvvhdsbxus38GV3vvhdsbxusbasbPmfyH5BAAAjAAAALAAfhGHhcjAAAAQAA4lsjkhfkjfhGHhcjGDSHJUYgJvhgtyrHgfhfkjfhpBREzxvt6QAA4lsjkhfkjfhdxwqBnuIoYtyGhBKoOjJj6GGHvvhdsbxus38GV3GfHyt56HGfGH56ge8WsjkhfkjfhGHhcjGDSHJUYgJvhgtyrHgfGfHyt56HGfGH56gebxus38G8WSLf6GGHvvhdsbxusbaSLf6GGHvvhdsbxusbaOiQA4lsjkhfkjf4lsjkhfkjf4lsjbxus38GkhfkjfhfkjfhpBREzxvt6QAA4lsjkhfkjfhdxwqBnuIoYtyGhBKoOjJj6GGHvvhdsbxus38GV3DcPjjBceXsplojj…”  />  …

Page 20: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 20 @delabassee#HTTP/2

• HTTP  uses  TCP  poorly  - HTTP:  short  and  bursty  flows  Vs.  TCP:  optimized  for  long-­‐lived  flows  

• Solutions  - Sprites  - Domain  sharding  - Assets  Inlining  - File  concatenations  - …

HTTP/1.1  circa  1999Problems  Vs  Solutions

1991   1996   1999   2009   2015  

HTTP/0.9  HTTP/1.0  

HTTP/1.1  SPDY  

HTTP/2.0  

Page 21: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. @delabassee#HTTP/2

Agenda

Why  HTTP/2?  

HTTP/2  

HTTP/2  and  Java  SE  

HTTP/2  and  Java  EE  

Summary

Page 22: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. @delabassee#HTTP/2

HTTP/2

• Jan  2015  • Feb  2015  • May  2015

22

Enabled  by  default  in  FireFox  (35)  and  Chrome  (40)  IESG  approved  HTTP/2  HTTP/2  in  10%  of  all  HTTP  responses  (FireFox)  HTTP/2  used  in  18%  of  global  traffic  (Google)  RFC  7540    “Hypertext  Transfer  Protocol  Version  2”  RFC  7541    “HPACK:  Header  Compression  for  HTTP/2”    

#HTTP/2

Page 23: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 23 @delabassee#HTTP/2

Browsers  support…  as  of  June  16  2015

http://caniuse.com/http2  

Page 24: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.   @delabassee#HTTP/2 24

HTTP/2  Features

• Binary  Framing  over  single  TCP  connection  • Request/Response  multiplexing  • Stream  Prioritization  • Server  Push  • Upgrade  from  HTTP  1.1  • Header  Compression

Page 25: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 25 @delabassee#HTTP/2

HTTP/2

• Fully  bi-­‐directional  – ConnectionA  TCP  socket  –Message   A  logical  HTTP  message,  such  as  a  request  or  a  response    – Stream A  bi-­‐directional  “channel”  within  a  connection,  carry  one  or  more  message  – FrameThe  smallest  unit  of  communication  in  HTTP/2

Lets  you  do  more  things  with  a  single  TCP  connection

Page 26: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 26 @delabassee#HTTP/2

HTTP/2Connections,  Streams,  Messages,  Frames

Page 27: HTTP/2 Comes to Java

@delabassee#HTTP/2 Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

Binary  Frames

• Frames  - HEADERS,  DATA,  PRIORITY,  RST_STREAM,  SETTINGS,  PUSH_PROMISE,  PING,  GOAWAY,  WINDOW_UPDATE,  CONTINUATION  - Prioritisation,  Flow  Control,  Server  Push,  …    

• Single  TCP  Connection

27

POST  /upload  HTTP/1.1  Host:  www.test.com  Content-­‐Type:  application/json  Content-­‐Length:  15  

{“name”:“duke”}

HTTP  1.1 HTTP/2

HEADERS  frame

DATA  frame

Page 28: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 28 @delabassee#HTTP/2

HTTP/2  Binary  Framing

GET  /index.html  HTTP/1.1Host:  example.comAccept:  text/html

Example  1

HEADERS        +  END_STREAM        +  END_HEADERS                :method:  GET                :scheme:  http                :path:  /index.html                :authority:  example.org                accept:  text/html

Page 29: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 29 @delabassee#HTTP/2

HTTP/2  Binary  Framing

HTTP/1.1  200  OKContent-­‐Length:  11Content-­‐Type:  text/html Hello  World

Example  2

HEADERS        -­‐  END_STREAM        +  END_HEADERS                :status:  200                content-­‐length:  11                content-­‐type:  text/html DATA        +  END_STREAM Hello  World

Page 30: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 30 @delabassee#HTTP/2

Multiplexing

Page 31: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 31 @delabassee#HTTP/2

Stream  Prioritization

• Stream  Dependency  in  HEADERS  Frame  • PRIORITY  frame  type  • An  additional  40  bytes  - Stream  id  (31)  - Weight  (8):  [1,  256]    - Exclusive  bit  (1)  

• Only  an  advice

A

B C

4 12

A

B CD

4 16 12

exclusive  =  0

A

B C

D

4 12

16

exclusive  =  1

Page 32: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. @delabassee#HTTP/2

Server  Push

/index.html      stream  1  /style.css                stream  2  /script.js                  stream  4

Client Server

stream  1  HEADERS

stream  1  DATA

stream  2        PROMISE

stream  4  PROMISE

• No  corresponding  JavaScript  API  • Can  be  combined  with  SSE

Page 33: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 33 @delabassee#HTTP/2

Header  CompressionHPack

Page 34: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 34 @delabassee#HTTP/2

Upgrade  from  HTTP  1.1

• HTTP  - Port  80  - HTTP  Upgrade  to  “h2c”  (101  Switching  Protocol)  

• HTTPS  (*)  - Application  Layer  Protocol  Negotiation  (ALPN)  - Next  Protocol  Negotiation  (NPN)  

(*)  TLS  is  not  mandatory

Page 35: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. @delabassee#HTTP/2

Agenda

Why  HTTP/2?  

HTTP/2  

HTTP/2  and  Java  SE  

HTTP/2  and  Java  EE  

Summary

Page 36: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. @delabassee#HTTP/2

Java  9  Support  for  HTTP/2• JEP  110  - http://openjdk.java.net/jeps/110  

• Supports  both  HTTP  1.1  and  2  • Easy  to  use  API  • Covers  only  the  most  common  use  cases  • Synchronous  &  Asynchronous

36#HTTP/2

Page 37: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. @delabassee#HTTP/2

Java  9//  HttpRequest  builder  from  the  default  HttpClient  HttpResponse  response  =  HttpRequest.create(new  URI("http://www.abc.be"))                                                                        .send(HttpRequest.noBody());  

String  responseBody  =  response.body(HttpResponse.asString());  

response  =  HttpRequest.create(new  URI("http://www.abc.be"))                                              .body(fromString("param1=Abc"))                                              .post()                                              .send();  …

37

Page 38: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. @delabassee#HTTP/2

Java  9

HttpResponse  response  =  HttpRequest.create(new  URI("http://www.abc.be"))                                                                        .header("Foo-­‐header",  "Bar")                                                                        .body(noBody())                                                                        .get()                                                                        .send();              

int  responseCode  =  response.responseCode();  SSLParameters  sslPar  =  response.sslParameters();  

String  responseBody  =  response.body(asString());

38

Page 39: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. @delabassee#HTTP/2

Java  9String[]  myFiles  =  {"foo.txt",  "bar.txt",  "blah.txt"};  CompletableFuture<Path>  futures[]  =  new  CompletableFuture[myFiles.length];  URI  base  =  new  URI("http://abc.be/resources")  for  (int  i=0;  i<myFiles.length;  i++)  {                  String  filename  =  filenames[i];                  URI  uri  =  base.resolve(filename);                  futures[i]  =  HttpRequest.create(uri)                                                  .body(noBody())                                                  .sendAsync()                                                  .thenApply((HttpResponse  resp)  -­‐>  {                                                          return  resp.body(asFile(“/tmp/"  +  filename));                                                  });  }  …

39

Page 40: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. @delabassee#HTTP/2

Java  9MultiResponseProcessor  multi;  HttpRequest.create(new  URI(“http://abc.be/"))            .body(noBody())            .sendMulti(                        (multi  =  HttpResponse.multi(                                  (HttpRequest  initiator,  HttpRequest  pushPromise)  -­‐>  {                                            if  (pushPromise  ==  null)  {  //  main  request                                                      return  HttpResponse.asFile("/mainResponse");                                            }  else  {  //  Push  Promise(s)                                                      String  path  =  pushPromise.uri().getPath();                                                      return  HttpResponse.asFile("/extra/"  +  path);                                            }                                  })                        )            );  multi.allActivity().join();

40

Page 41: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. @delabassee#HTTP/2

Agenda

Why  HTTP/2?  

HTTP/2  

HTTP/2  and  Java  SE  

HTTP/2  and  Java  EE  

Summary

Page 42: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 42 @delabassee#HTTP/2

• Request/Response  multiplexing  • Binary  Framing  • Stream  Prioritization  • Server  Push  • Header  Compression  • Upgrade  from  HTTP  1.1  – ALPN  or  (NPN)  – 101  Switching  Protocols

HTTP/2

Page 43: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 43 @delabassee#HTTP/2

• Request/Response  Multiplexing  • Binary  Framing  • Stream  Prioritization  • Server  Push  • Header  Compression  • Upgrade  from  HTTP  1.1  – ALPN  or  (NPN)  – 101  Switching  Protocols

Features  to  be  Exposed  in  the  Servlet  API

HTTP/2

Page 44: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 44 @delabassee#HTTP/2

• New  Priority  class  – boolean  exclusive  – int  streamId  – int  weight  

• New  methods  to  HttpServletRequest  – int  getStreamId()  – Priority  getPriority()  

• New  methods  to  HttpServletResponse  – int  getStreamId()  – Priority  getPriority()  – void  setPriority(Priority  p)  

• Dependency?

Servlet  4.0  -­‐  Stream  Prioritization

Page 45: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 45 @delabassee#HTTP/2

• Push  resource  to  client  for  a  given  url  and  headers  • May  add  callback  for  completion  or  error  of  a  push  • Not  a  replacement  for  WebSocket

Servlet  4.0  -­‐  Server  Push

Page 46: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 46 @delabassee#HTTP/2

Server  Push

private  void  pushResource(String  relativeResourcePath)  {  

final  Request  jettyRequest  =  (Request)  getRequest();  final  PushBuilder  pushBuilder  =  jettyRequest.getPushBuilder();  

pushBuilder.setQueryString(…)                  .push(relativeResourcePath);  

           

//…  

}

Jetty’s  PushBuilder  ?

Page 47: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. @delabassee#HTTP/2

Agenda

Why  HTTP/2?  

HTTP/2  

HTTP/2  and  Java  SE  

HTTP/2  and  Java  EE  

Summary

Page 48: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. @delabassee#HTTP/2

HTTP/2

• Address  the  Limitations  of  HTTP  1.x  - Improve  ressources  utilization,  performance,  reduce  latency  

• “Compatible”  with  HTTP  1.1  - Retain  semantics  of  HTTP  1.1  

- Define  interaction  with  HTTP  1.1  - Undo  your  HTTP  1.1  tricks!!  

• “TLS  not  mandatory”

48

Hypertext  Transfer  Protocol  version  2  &  HPACK

Page 49: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. @delabassee#HTTP/2

HTTP/2  and  Java

• JEP  110  brings  HTTP/2  to  Java  SE  • Servlet  4.0  brings  HTTP/2  to  Java  EE  - 100%  compliant  implementation  of  HTTP/2  - Expose  key  features  to  the  API  • Server  Push  • Stream  Prioritization  • HTTP  1.1  upgrade  

• TBC!

49

Plans

Page 50: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. @delabassee#HTTP/2

Resources  (and  credits)

• http://http2.github.io  • http://chimera.labs.oreilly.com/books/1230000000545/ch12.html  • https://java.net/projects/servlet-­‐spec/  • http://glassfish.org/adoptajsr  • http://openjdk.java.net/jeps/110

50

Page 51: HTTP/2 Comes to Java

Safe  Harbor  Statement

The  preceding  is  intended  to  outline  our  general  product  direction.  It  is  intended  for  information  purposes  only,  and  may  not  be  incorporated  into  any  contract.  It  is  not  a  commitment  to  deliver  any  material,  code,  or  functionality,  and  should  not  be  relied  upon  in  making  purchasing  decisions.  The  development,  release,  and  timing  of  any  features  or  functionality  described  for  Oracle’s  products  remains  at  the  sole  discretion  of  Oracle.

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 51

Page 52: HTTP/2 Comes to Java

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved. 52