Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[SydPHP] HTTP2 and Asynchronous APIs

Davey Shafik
September 08, 2015

[SydPHP] HTTP2 and Asynchronous APIs

HTTP/2 (H2) is coming, and along with it a whole new way of communicating over the web. Connection re-use, prioritization, multiplexing, and server push are just some of the features in H2.

In this talk we'll look at the HTTP/2 protocol, and at how we can use asynchronous request now with HTTP/1.x. We will also look at what asychronous requests and H2 mean for your API and clients in the future.

Davey Shafik

September 08, 2015
Tweet

More Decks by Davey Shafik

Other Decks in Programming

Transcript

  1. H T T P/ 2 A N D A SY N C H R O N O U S A P I S

    View full-size slide

  2. D AV E Y S H A F I K
    • Author of Zend PHP 5 Certification
    Study Guide, Sitepoints PHP
    Anthology: 101 Essential Tips, Tricks &
    Hacks & PHP Master: Write Cutting
    Edge Code
    • A contributor to Zend Framework 1 &
    2, phpdoc, & PHP internals
    • Original creator of PHAR/
    PHP_Archive
    • @dshafik

    View full-size slide

  3. h tt p : / /d e v e l o p e r. a k a m a i .co m

    View full-size slide

  4. Let’s start a conversation about
    mental health in tech
    mhprompt.org/support

    View full-size slide

  5. H T T P/ 2
    A KA : H 2

    View full-size slide

  6. N OT: H T T P/ 2 . 0 O R H T T P 2 . 0

    View full-size slide

  7. W H AT I S H T T P/ 2 ?
    CC-BY:

    View full-size slide

  8. R F C 7 5 4 0
    H Y P E RT E XT T R A N S F E R P R OTO CO L V E RS I O N 2

    View full-size slide

  9. R F C 7 5 4 1
    H PA C K - H E A D E R CO M P R E SS I O N FO R H T T P/ 2

    View full-size slide

  10. –J O H N N Y A P P L E S E E D
    CC-BY:
    1991 1996 1999
    HTTP/0.9
    HTTP/1.0
    HTTP/1.1
    2015
    HTTP/2
    2009
    SPDY

    View full-size slide

  11. B I N A R Y I N ST E A D O F T E XT
    CC-BY:

    View full-size slide

  12. F U L LY M U LT I P L E X E D
    CC-BY:
    I N S T E A D O F O R D E R E D A N D B L O C K I N G

    View full-size slide

  13. C A N US E O N E CO N N E CT I O N 

    FO R PA R A L L E L R E Q U E STS
    CC-BY: Alosh Bennett

    View full-size slide

  14. U S E S H E A D E R CO M P R E SS I O N
    CC-BY-SA:
    R E D U C E S O V E R H E A D

    View full-size slide

  15. S E R V E R P U S H
    I S S U P E R CO O L ( N O R E A L LY )
    CC-BY-SA:

    View full-size slide

  16. S E R V E R P U S H
    • Allows the server to proactively push assets like stylesheets and
    images to the client without them needing to parse the HTML page
    and make subsequent requests
    • Done by pushing the assets into the client cache, avoiding the
    roundtrip necessary to pull them up once the client makes the
    request

    View full-size slide

  17. W H AT D O E S H T T P/ 2 M E A N F O R M Y
    A P P L I C AT I O N ?

    View full-size slide

  18. T R A N S PA R E N T
    CC-BY-SA:
    H A N D L E D B Y N G I N X / A PA C H E

    View full-size slide

  19. S O W H AT ’ S T H E P O I N T ?

    View full-size slide

  20. R E M E M B E R T H I S ?
    CC-BY:
    C A N US E O N E CO N N E CT I O N 

    FO R PA R A L L E L R E Q U E STS

    View full-size slide

  21. U P LO A D I N G M U LT I P L E I M A G E S
    CC-BY:

    View full-size slide

  22. S E R I A L U P LO A D S
    ! " 
    $
    $
    $
    $
    $
    $

    View full-size slide

  23. CO N CU R R E N T
    ! " 
    $
    $
    $
    $
    $
    $

    View full-size slide

  24. M U LT I P L E X E D
    ! " 
    $
    $
    $
    $
    $
    $

    View full-size slide

  25. F E TC H I N G A B LO G P O ST + CO M M E N TS
    CC-BY:

    View full-size slide

  26. ! "
    GET /post/1

    View full-size slide

  27. ! "
    GET /post/1
    200 OK application/json

    View full-size slide

  28. {
    "type": "post",
    "id": "1",
    "title": "JSON API paints my bikeshed!",
    "tags": ["json", "api", “relationships"],
    "author": "http://example.com/posts/1/author",
    "comments": "http://example.com/posts/1/comments"
    }

    View full-size slide

  29. ! "
    GET /posts/1/comments
    200 OK application/json

    View full-size slide

  30. [
    "http://example.org/post/1/comment/1",
    "http://example.org/post/1/comment/2",
    "http://example.org/post/1/comment/3",
    "http://example.org/post/1/comment/4"
    ]

    View full-size slide

  31. ! "
    GET /post/example/comments/1

    View full-size slide

  32. ! "
    GET /post/example/comments/
    200 OK application/json
    1

    View full-size slide

  33. 2
    ! "
    GET /post/example/comments/
    200 OK application/json
    1

    View full-size slide

  34. 2
    3
    ! "
    GET /post/example/comments/
    200 OK application/json
    1

    View full-size slide

  35. 2
    3
    4
    ! "
    GET /post/example/comments/
    200 OK application/json
    1

    View full-size slide

  36. {
    "type": "comment",
    "id": "1",
    "title": "FIRST!",
    "author": "http://example.com/post/1/comment/1/author"
    }

    View full-size slide

  37. 6
    requests
    (possibly connections)

    View full-size slide

  38. 14
    requests
    (possibly connections)

    View full-size slide

  39. M U LT I P L E X E D
    ! "
    GET /post/example/comments/1
    GET /post/example/comments/2
    GET /post/example/comments/3
    GET /post/example/comments/4

    View full-size slide

  40. CSS /J S M I N I F I C AT I O N I S U N E C E SS A R Y
    CC-BY:

    View full-size slide

  41. $numRequests = 378;

    View full-size slide

  42. H T T P/ 1 . 1 : SY N C H R O N O US
    $url = 'https://http2.akamai.com/demo/tile-%d.png';
    for ($i = 0; $i <= $numRequests; $i++) {
    $ch = curl_init();
    $conf[CURLOPT_URL] = sprintf($url, $i);
    curl_exec($ch);
    curl_close($ch);
    }

    View full-size slide

  43. 4 7 . 6 7 

    s e co n d s
    CC-BY:

    View full-size slide

  44. H T T P/ 2 : SY N C H R O N O US
    $url = 'https://http2.akamai.com/demo/tile-%d.png';
    for ($i = 0; $i <= $numRequests; $i++) {
    $ch = curl_init();
    $conf[CURLOPT_URL] = sprintf($url, $i);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, HTTP_VERSION_2_0);
    curl_exec($ch);
    curl_close($ch);
    }

    View full-size slide

  45. 6 2 . 1 9 

    s e co n d s
    CC-BY-NC:

    View full-size slide

  46. H T T P/ 1 . 1 : CO N CU R R E N T
    $mh = curl_multi_init();

    $url = 'https://http2.akamai.com/demo/tile-%d.png';

    for ($i = 0; $i <= $numRequests; $i++) {
    $handles[] = $ch = curl_init();
    $conf[CURLOPT_URL] = sprintf($url, ‘%d');
    curl_multi_add_handle($mh, $ch);
    }

    View full-size slide

  47. H T T P/ 1 . 1 : CO N CU R R E N T ( CO N T. )
    do {
    $execReturnValue = curl_multi_exec($mh, $runningHandles);
    } while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);
    while ($runningHandles && $execReturnValue == CURLM_OK) {
    $numberReady = curl_multi_select($mh);
    if ($numberReady != -1) {
    do {
    $execReturnValue = curl_multi_exec($mh, $runningHandles);

    } while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);
    }
    }

    View full-size slide

  48. 8 . 6 6 

    s e co n d s

    View full-size slide

  49. H T T P/ 1 . 1 : CO N CU R R E N T
    $mh = curl_multi_init();

    curl_multi_setopt(

    $mh,

    CURLMOPT_PIPELINING,
    CURLPIPE_MULTIPLEX
    );
    $url = 'https://http2.akamai.com/demo/tile-%d.png';

    for ($i = 0; $i <= $numRequests; $i++) {
    $handles[] = $ch = curl_init();
    $conf[CURLOPT_URL] = sprintf($url, '%d');
    curl_multi_add_handle($mh, $ch);
    }

    View full-size slide

  50. H T T P/ 2 : CO N CU R R E N T ( CO N T. )
    do {
    $execReturnValue = curl_multi_exec($mh, $runningHandles);
    } while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);
    while ($runningHandles && $execReturnValue == CURLM_OK) {
    $numberReady = curl_multi_select($mh);
    if ($numberReady != -1) {
    do {
    $execReturnValue = curl_multi_exec($mh, $runningHandles);

    } while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);
    }
    }

    View full-size slide

  51. 2 . 1 4 

    s e co n d s
    CC-BY:

    View full-size slide

  52. S E R V E R P U S H
    CC-BY:

    View full-size slide

  53. ! "
    GET /post/1

    View full-size slide

  54. ! "
    GET /post/1
    200 OK application/json

    View full-size slide

  55. GET /post/1/comments
    ! "
    GET /post/1
    200 OK application/json

    View full-size slide

  56. GET /post/1/comments
    GET /post/1/comment/1
    ! "
    GET /post/1
    200 OK application/json

    View full-size slide

  57. ! "
    GET /post/1/comments
    GET /post/1/comment/1

    View full-size slide

  58. ! "
    GET /post/1/comment/2
    GET /post/1/comment/3
    GET /post/1/comment/4
    GET /post/1/comments
    GET /post/1/comment/1

    View full-size slide

  59. ! "
    GET /post/1/comment/2
    GET /post/1/comment/3
    GET /post/1/comment/4
    GET /post/1/comment/1/author
    GET /post/1/comment/2/author
    GET /post/1/comment/3/author
    GET /post/1/comment/4/author
    GET /post/1/comments
    GET /post/1/comment/1

    View full-size slide

  60. ! "
    GET /post/1/comment/2
    GET /post/1/comment/3
    GET /post/1/comment/4
    GET /post/1/comment/1/author
    GET /post/1/comment/2/author
    GET /post/1/comment/3/author
    GET /post/1/comment/4/author
    GET /post/1/comment/1/author/avatar.png
    GET /post/1/comment/2/author/avatar.png
    GET /post/1/comment/3/author/avatar.png
    GET /post/1/comment/4/author/avatar.png
    GET /post/1/comments
    GET /post/1/comment/1

    View full-size slide

  61. G U ZZ L E S U P P O RT

    View full-size slide

  62. G U ZZ L E S U P P O RT
    • Some support
    • Doesn’t handle lack of http2 support in libcurl
    • Doesn’t handle multiplexing
    • Untested (but should be OK, as libcurl itself is tested)
    • Details: http://daveyshafik.com/guzzle-http2

    View full-size slide

  63. CO D E
    CC-BY:

    View full-size slide

  64. H T T P/ 2 : SY N C H R O N O US
    use GuzzleHttp\Client;

    $url = 'https://http2.akamai.com/demo/tile-
    %d.png';

    $client = new Client(['version' => 2]);
    for ($i = 0; $i <= $numRequests; $i++) {
    $client->get(sprintf($url, $i));
    }

    View full-size slide

  65. H T T P/ 2 : M U LT I P L E X E D ( P O SS I B L E A P I )
    use GuzzleHttp\Client;

    $url = 'https://http2.akamai.com/demo/tile-%d.png';

    $client = new Client(['version' => 2]);
    for ($i = 0; $i <= $numRequests; $i++) {
    $promises[] = $client->getAsync(

    sprintf($url, $i)

    );
    }
    $results = Promise\unwrap($promises);

    View full-size slide

  66. H T T P/ 2 : M U LT I P L E X E D F I L E U P LO A D
    $stack = \GuzzleHttp\HandlerStack::create();

    $handler = new \Akamai\NetStorage\Handler
    \Authentication();

    $handler->setSigner((new \Akamai\NetStorage
    \Authentication())->setKey($key, $keyName));
    $client = new Akamai\Edgegrid\Open\Client([
    'handler' => $stack, 

    'version' => 2
    ]);

    View full-size slide

  67. H T T P/ 2 : M U LT I P L E X E D F I L E U P LO A D ( CO N T. )
    $url = "http://example.akamaihd.net/cpCode/%s";

    foreach ($_FILES as $file) {
    $fileName = Ramsey\Uuid\Uuid::uuid4();
    $promises = $client->putAsync(

    sprintf($url, $filename), 

    [
    'body' => fopen($file['tmp_name'], 'r+')
    ]
    );
    }

    $results = Promise\unwrap($promises);

    View full-size slide

  68. I N S U M M A R Y

    View full-size slide

  69. H T T P / 2 I S A W E S O M E !
    CC-BY-SA:

    View full-size slide

  70. F E E D B A C K & Q U E S T I O N S
    Twitter:
    Email:
    Slides:
    @dshafik
    [email protected]
    http://daveyshafik.com/slides

    View full-size slide