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

T3DD16 pushing the limits with React PHP

T3DD16 pushing the limits with React PHP

It's 2016 and times have changed - yet PHP is still most often associated with your average product catalogue or blogging platform. In this talk you will learn that PHP's huge ecosystem has way more to offer and PHP is not inferior at all to its evil cousin Node.js.

You will learn about the core concepts of async PHP and why you too should care about React PHP being a real thing. The talk has a strong focus on sparking the idea that PHP can be way faster and more versatile than you probably thought. Bring along an open mind and through lots of examples and demos learn why what sounds crazy at first might soon be a valuable addition in your toolbox.

Christian Lück
PRO

September 03, 2016
Tweet

More Decks by Christian Lück

Other Decks in Programming

Transcript

  1. pushing the limits with React PHP
    why React PHP is awesome and why YOU should care

    View Slide

  2. Agenda
    - Hello!
    - PHP, the web of the ‘90s?
    - Enter React
    - Core components
    - Examples and demo time
    - Conclusions
    - Short break
    - Workshop!
    2

    View Slide

  3. Hello!
    3

    View Slide

  4. $ whoami
    - Christian Lück
    4

    View Slide

  5. $ whoami
    - Christian Lück → Christian Lueck
    5

    View Slide

  6. $ whoami
    - Christian Lück → Christian Lueck → @clue
    6

    View Slide

  7. $ whoami
    - Christian Lück → Christian Lueck → @clue → @another_clue
    7

    View Slide

  8. $ whoami
    - Christian Lück → Christian Lueck → @clue → @another_clue
    - Team Coach / Software Engineering, working professionally for 10 years
    - Masters degree in Information Systems Engineering
    8

    View Slide

  9. $ whoami
    - Christian Lück → Christian Lueck → @clue → @another_clue
    - Team Coach / Software Engineering, working professionally for 10 years
    - Masters degree in Information Systems Engineering
    - Started coding 15 years ago and could never stop :)
    - Passionate about pushing the limits!
    - Open source contributor and maintainer (React, Docker, Graphs and more)
    9

    View Slide

  10. $ whoami
    - Christian Lück → Christian Lueck → @clue → @another_clue
    - Team Coach / Software Engineering, working professionally for 10 years
    - Masters degree in Information Systems Engineering
    - Started coding 15 years ago and could never stop :)
    - Passionate about pushing the limits!
    - Open source contributor and maintainer (React, Docker, Graphs and more)
    - Quit my job in April to focus on thrilling things ahead…
    - Freelance job: Bigger momentum, higher impact, spread the word!
    10

    View Slide

  11. Who are you?
    11
    now that you know me…

    View Slide

  12. Who are you?
    12
    now that you know me…
    - PHP developers?
    - architecs / engineers?

    View Slide

  13. Who are you?
    13
    now that you know me…
    - PHP developers?
    - architecs / engineers?
    - know React?

    View Slide

  14. Who are you?
    14
    now that you know me…
    - PHP developers?
    - architecs / engineers?
    - know React?
    - know the other React?

    View Slide

  15. PHP, the web of the ‘90s?
    15

    View Slide

  16. PHP and the web of the ‘90s
    - traditional LAMP stack
    - Request-Response-Cycle
    Apache
    Client PHP MySQL
    16

    View Slide

  17. PHP and the web of the ‘90s
    - traditional LAMP stack
    - Request-Response-Cycle
    - PHP is too slow?
    - We sure can improve this…
    17
    Apache
    Client PHP MySQL

    View Slide

  18. PHP and the web of the ‘90s
    - traditional LAMP stack
    - Request-Response-Cycle
    - PHP is too slow?
    - We sure can improve this…
    18
    Apache
    Client PHP MySQL
    Apache
    Client
    FPM
    MySQL
    PHP
    PHP

    View Slide

  19. PHP and the web of the ‘90s
    - traditional LAMP stack
    - Request-Response-Cycle
    - PHP is too slow?
    - We sure can improve this…
    19
    Apache
    Client PHP MySQL
    Apache
    Client
    FPM
    MySQL
    PHP
    PHP
    nginx
    Client
    FPM
    MySQL
    PHP
    PHP

    View Slide

  20. PHP and the web of the ‘90s
    - traditional LAMP stack
    - Request-Response-Cycle
    - PHP is too slow?
    - We sure can improve this…
    20
    Apache
    Client PHP MySQL
    Apache
    Client
    FPM
    MySQL
    PHP
    PHP
    nginx
    Client
    FPM
    MySQL
    PHP
    PHP
    nginx
    Client
    FPM
    memcache
    PHP
    PHP
    MySQL

    View Slide

  21. PHP may not be pretty…
    21

    View Slide

  22. PHP may not be pretty…
    but it gets the job done!
    22

    View Slide

  23. Knock knock!
    Who’s there?
    23

    View Slide

  24. Knock knock!
    2016!
    Who’s there?
    24

    View Slide

  25. Knock knock!
    2016!
    - Separation of concerns
    (Frontend↔Backend)
    - HTTP APIs (RESTful)
    - Integration with 3rd parties
    - Live-Data (ticker)
    - CLI tools
    Who’s there?
    25

    View Slide

  26. node js?
    26

    View Slide

  27. node js!
    27

    View Slide

  28. Enter React!
    28

    View Slide

  29. What is React?
    29

    View Slide

  30. What is React?
    non-blocking I/O
    30

    View Slide

  31. What is React?
    non-blocking I/O
    event-driven
    31

    View Slide

  32. What is React?
    non-blocking I/O
    event-driven
    async
    32

    View Slide

  33. 33

    View Slide

  34. 100%
    34

    View Slide

  35. 100%
    pure PHP
    35

    View Slide

  36. 100%
    pure PHP
    no extensions
    36

    View Slide

  37. 100%
    pure PHP
    no extensions
    no magic
    37

    View Slide

  38. What does that even mean?!
    38

    View Slide

  39. the idea
    39

    View Slide

  40. the idea
    calculations are fast
    40

    View Slide

  41. the idea
    calculations are fast
    I/O is slow
    41

    View Slide

  42. I/O is everywhere
    42

    View Slide

  43. I/O is everywhere
    third party HTTP APIs (RESTful, SOAP, you name it…)
    43

    View Slide

  44. I/O is everywhere
    third party HTTP APIs (RESTful, SOAP, you name it…)
    mysql, postgres
    44

    View Slide

  45. I/O is everywhere
    third party HTTP APIs (RESTful, SOAP, you name it…)
    mysql, postgres
    filesystem I/O (session files)
    45

    View Slide

  46. I/O is everywhere
    third party HTTP APIs (RESTful, SOAP, you name it…)
    mysql, postgres
    filesystem I/O (session files)
    redis, memcache 46

    View Slide

  47. 47
    Source: Latency Numbers Every Programmer Should Know: https://gist.github.com/jboner/2841832
    CPU vs I/O

    View Slide

  48. I/O is slow!
    48

    View Slide

  49. I/O is slow!
    So why wait?
    49

    View Slide

  50. This is React
    50

    View Slide

  51. This is React
    51
    Start multiple I/O operations (non-blocking)

    View Slide

  52. This is React
    52
    Start multiple I/O operations (non-blocking)
    Get notified when something happens (react)

    View Slide

  53. This is React
    53
    Start multiple I/O operations (non-blocking)
    Get notified when something happens (react)
    Don’t waste time waiting

    View Slide

  54. What React is not
    54

    View Slide

  55. What React is not
    - React is not black magic / vodoo
    - Does not make your code faster magically
    55

    View Slide

  56. What React is not
    - React is not black magic / vodoo
    - Does not make your code faster magically
    - React is not a framework
    - Indepentent components (libraries)
    56

    View Slide

  57. What React is not
    - React is not black magic / vodoo
    - Does not make your code faster magically
    - React is not a framework
    - Indepentent components (libraries)
    - React is not a replacement for your favorite framework
    - Symfony is here to stay
    57

    View Slide

  58. What React is not
    - React is not black magic / vodoo
    - Does not make your code faster magically
    - React is not a framework
    - Indepentent components (libraries)
    - React is not a replacement for your favorite framework
    - Symfony is here to stay
    - React is not the new buzz
    - React is here to stay as well (started in 2012)
    58

    View Slide

  59. React core components
    59

    View Slide

  60. React Core components
    - Event loop (reactor)
    - Promises
    - Streams
    60

    View Slide

  61. Event loop
    Consumers
    - THE core, low-level component
    61

    View Slide

  62. Event loop
    Consumers
    - THE core, low-level component
    - Create an instance
    - Just use the Factory
    - Additional extensions for bigger payloads
    - something inbetween…
    - just pass the $loop around
    - Start running
    - keeps running forever
    - unless stopped or done
    62

    View Slide

  63. Event loop
    Consumers
    - THE core, low-level component
    - Create an instance
    - Just use the Factory
    - Additional extensions for bigger payloads
    - something inbetween…
    - just pass the $loop around
    - Start running
    - keeps running forever
    - unless stopped or done
    63
    $loop = Factory::create();
    // something inbetween
    // pass the $loop around to all components
    $loop->run();

    View Slide

  64. Event loop
    Implementors
    - Reactor pattern (hence the name)
    64
    -

    View Slide

  65. Event loop
    Implementors
    - Reactor pattern (hence the name)
    - wait for stream resources to become
    - readable
    - writable
    - start timers
    - once
    - periodic
    - ticks
    65
    -

    View Slide

  66. Event loop
    Implementors
    - Reactor pattern (hence the name)
    - wait for stream resources to become
    - readable
    - writable
    - start timers
    - once
    - periodic
    - ticks
    66
    $loop->addTimer(0.5, function () {
    echo ‘world’;
    });
    $loop->addTimer(0.3, function () {
    echo ‘hello’;
    });

    View Slide

  67. Event loop
    Implementors
    - Reactor pattern (hence the name)
    - wait for stream resources to become
    - readable
    - writable
    - start timers
    - once
    - periodic
    - ticks
    67
    $loop->addTimer(0.5, function () {
    echo ‘world’;
    });
    $loop->addTimer(0.3, function () {
    echo ‘hello’;
    });
    $loop->addReadStream($stream, $fn);
    $loop->addWriteStream($stream, $fn);

    View Slide

  68. Promises
    - Placeholder for a single future result
    - Possible states:
    - pending
    - fulfilled (successfully resolved)
    - rejected (Exception occured)
    68

    View Slide

  69. Promises
    - no more imperative code flow
    - instead (tell, don’t ask)
    69
    $a->then($fulfilled = null, $rejected = null);
    $a->then(‘process’);
    $a->then(‘process’, ‘var_dump’);

    View Slide

  70. Streams
    - Process large strings in chunks as they happen (think downloads)
    - Types
    - Readable (e.g. STDIN pipe)
    - Writable (e.g. STDOUT pipe)
    - Duplex (e.g. TCP/IP connection)
    70

    View Slide

  71. Streams
    - interfaces, events and listeners:
    71
    $dest->write(‘hello’);
    $source->on(‘data’, function ($data) {
    var_dump($data);
    });
    $source->on(‘close’, function () {
    echo ‘stream closed’;
    });

    View Slide

  72. Streams
    - interfaces, events and listeners:
    72
    $dest->write(‘hello’);
    $source->on(‘data’, function ($data) {
    var_dump($data);
    });
    $source->on(‘close’, function () {
    echo ‘stream closed’;
    });
    $source->pipe($gunzip)->pipe($badwords)->pipe($dest);

    View Slide

  73. Avoid blocking!
    - The loop must not be blocked
    73

    View Slide

  74. Avoid blocking!
    - The loop must not be blocked
    - Many functions / lib assume blocking by default
    - Anything >1ms should be reconsidered
    74

    View Slide

  75. Avoid blocking!
    - The loop must not be blocked
    - Many functions / lib assume blocking by default
    - Anything >1ms should be reconsidered
    - Alternatives
    - Single result: Promises
    - Evented: Streams
    75

    View Slide

  76. Avoid blocking!
    - The loop must not be blocked
    - Many functions / lib assume blocking by default
    - Anything >1ms should be reconsidered
    - Alternatives
    - Single result: Promises
    - Evented: Streams
    - Need a blocking function?
    - Fork off!
    - Use IPC
    76

    View Slide

  77. Avoid blocking!
    - The loop must not be blocked
    - Many functions / lib assume blocking by default
    - Anything >1ms should be reconsidered
    - Alternatives
    - Single result: Promises
    - Evented: Streams
    - Need a blocking function?
    - Fork off!
    - Use IPC
    77
    Pay attention:
    - PDO, mysql etc.
    - file system access
    - network access
    - third-party APIs

    View Slide

  78. Examples and demo time!
    78

    View Slide

  79. Socket server
    79
    react/socket
    - THE canonical chat example
    - broadcast all incoming msgs

    View Slide

  80. Socket server
    80
    react/socket
    - THE canonical chat example
    - broadcast all incoming msgs
    - run example server
    - connect via telnet:
    $ telnet IP 8000

    View Slide

  81. HTTP client
    81
    clue/buzz-react
    - Simple HTTP requests
    - inspired by kriswallsmith/buzz
    - PSR-7 compatible

    View Slide

  82. HTTP client
    82
    clue/buzz-react
    - Simple HTTP requests
    - inspired by kriswallsmith/buzz
    - PSR-7 compatible
    - Promises and Streams
    - It’s fast…

    View Slide

  83. HTTP client
    83
    clue/buzz-react
    - Simple HTTP requests
    - inspired by kriswallsmith/buzz
    - PSR-7 compatible
    - Promises and Streams
    - It’s fast…
    - benchmarks in following slides about
    clue/docker-react

    View Slide

  84. Packagist API
    clue/packagist-api-react
    - get information about any
    Composer package
    - simple, Promise-based
    - lightweight wrapper between
    - KnpLabs/packagist-api
    - clue/buzz-react
    84

    View Slide

  85. Packagist API
    clue/packagist-api-react
    - get information about any
    Composer package
    - simple, Promise-based
    - lightweight wrapper between
    - KnpLabs/packagist-api
    - clue/buzz-react
    85
    - see its examples
    $ php examples/search.php

    View Slide

  86. Docker client
    clue/docker-react
    - Run apps in isolated containers
    - “build, ship and run, anywhere”
    - Controlled through HTTP API
    - Promises and Streams
    86

    View Slide

  87. Docker client
    clue/docker-react
    - Run apps in isolated containers
    - “build, ship and run, anywhere”
    - Controlled through HTTP API
    - Promises and Streams
    87
    - see its promise examples
    $ php examples/info.php
    - see its streaming examples
    $ php examples/benchmark-exec.php

    View Slide

  88. 700 MiB/s
    88
    dockerd maxed out
    PHP not

    View Slide

  89. HTTP server
    react/http
    - Pure PHP, with
    no additional webserver
    - Under active development
    89

    View Slide

  90. HTTP server
    react/http
    - Pure PHP, with
    no additional webserver
    - Under active development
    - Lots of third-party integrations
    with traditional frameworks
    (symfony, slim, silex, PIMF etc.)
    90

    View Slide

  91. 5k requests/s
    91

    View Slide

  92. 5k requests/s
    92
    this is a local single core benchmark!

    View Slide

  93. 5k requests/s
    93
    this is a local single core benchmark!
    dual core i3 => 10k requests/s

    View Slide

  94. 5k requests/s
    94
    this is a local single core benchmark!
    dual core i3 => 10k requests/s
    36M requests/h

    View Slide

  95. Server sent events
    clue/sse-react
    - Server sent events (SSE)
    - aka. EventSource (browser API)
    - Streaming events to browser
    - limited browser support
    95

    View Slide

  96. Server sent events
    clue/sse-react
    - Server sent events (SSE)
    - aka. EventSource (browser API)
    - Streaming events to browser
    - limited browser support
    96
    - see examples connecting to initial chat
    $ php examples/chat-server.php
    - open browser: http://IP.:8001/

    View Slide

  97. Websocket server
    cboden/ratchet
    - Async WebSocket server
    - bidirectional data flow between
    browser and server
    - better browser support
    97

    View Slide

  98. Redis client
    98
    clue/redis-react
    - Redis is a fast in-memory DB
    - very simple commands
    - very simple protocol
    - pipelined, Promise-based

    View Slide

  99. Redis client
    99
    clue/redis-react
    - Redis is a fast in-memory DB
    - very simple commands
    - very simple protocol
    - pipelined, Promise-based
    - see its examples
    $ php examples/incr.php

    View Slide

  100. Redis server
    100

    View Slide

  101. Redis server
    101
    - Official Redis is written in C

    View Slide

  102. Redis server
    102
    clue/php-redis-server
    - Official Redis is written in C
    - Reimplementation is pure PHP

    View Slide

  103. Redis server
    103
    clue/php-redis-server
    - Official Redis is written in C
    - Reimplementation is pure PHP
    - Very simple to add commands

    View Slide

  104. Redis server
    104
    clue/php-redis-server
    - Official Redis is written in C
    - Reimplementation is pure PHP
    - Very simple to add commands
    - How fast could PHP possibly be?
    Let’s see…

    View Slide

  105. Redis server
    105
    clue/php-redis-server
    - Official Redis is written in C
    - Reimplementation is pure PHP
    - Very simple to add commands
    - How fast could PHP possibly be?
    Let’s see…
    - see its bin
    $ php bin/redis-server.php
    - test via clue/redis-react
    - test via official redis CLI
    - run official redis benchmark during talk:
    - official server: ~90k OP/s
    - PHP server: ~50k OP/s

    View Slide

  106. 50k OP/s
    pure PHP
    who needs native code anyway?
    106

    View Slide

  107. Redis framework
    107
    clue/php-redis-framework
    - Development preview
    - Very simple to add custom
    commands

    View Slide

  108. Redis framework
    108
    clue/php-redis-framework
    - Development preview
    - Very simple to add custom
    commands
    - run example server
    $ php examples/03-beer.php
    - connect via telnet:
    $ telnet IP 9000

    View Slide

  109. UPnP
    clue/ssdp-react
    - Univeral Plug and Play (UPnP)
    - Simple Service Discovery
    Protocol (SSDP)
    - dicover UPnP-enabled devices
    - printers
    - routers
    - multi media devices
    109

    View Slide

  110. UPnP
    clue/ssdp-react
    - Univeral Plug and Play (UPnP)
    - Simple Service Discovery
    Protocol (SSDP)
    - dicover UPnP-enabled devices
    - printers
    - routers
    - multi media devices
    110
    - see its examples
    $ php examples/search.php

    View Slide

  111. Zenity
    clue/zenity-react
    - PHP desktop GUI applications
    - very simple, Promise-based
    111

    View Slide

  112. Zenity
    clue/zenity-react
    - PHP desktop GUI applications
    - very simple, Promise-based
    112
    - see its simple examples
    $ php examples/dialog.php
    - see its more realistic examples
    $ php examples/menu.php
    $ php examples/progress-*.php

    View Slide

  113. many, MANY more third-party projects:
    https://github.com/reactphp/react/wiki/Users
    113

    View Slide

  114. Conclusions
    114

    View Slide

  115. Conclusions
    115
    - React is a real deal and here to stay
    - React is stable and production ready
    - predictable releases
    - but possibly feature-incomplete (check open issues)

    View Slide

  116. Conclusions
    116
    - React is a real deal and here to stay
    - React is stable and production ready
    - predictable releases
    - but possibly feature-incomplete (check open issues)
    - React does not make your code faster magically

    View Slide

  117. Conclusions
    117
    - React is a real deal and here to stay
    - React is stable and production ready
    - predictable releases
    - but possibly feature-incomplete (check open issues)
    - React does not make your code faster magically
    - Consider using React whenever you’re having to wait
    - Consider using React whenever you’re accessing the network

    View Slide

  118. PHP is faster than you probably thought
    118

    View Slide

  119. PHP is more versatile than you probably thought
    119

    View Slide

  120. Integration with traditional environments
    120
    integrating async into sync is easy

    View Slide

  121. Integration with traditional environments
    121
    integrating async into sync is easy
    - just run the loop until you’re done
    - see clue/block-react

    View Slide

  122. Integration with traditional environments
    122
    integrating async into sync is easy
    - just run the loop until you’re done
    - see clue/block-react
    integrating sync into async is hard

    View Slide

  123. Integration with traditional environments
    123
    integrating async into sync is easy
    - just run the loop until you’re done
    - see clue/block-react
    integrating sync into async is hard
    - often requires async rewrite
    - consider forking instead

    View Slide

  124. Need help? Want to help?
    - check each component’s README
    - check open issues
    124

    View Slide

  125. Need help? Want to help?
    - check each component’s README
    - check open issues
    - join #reactphp on irc.freenode.org
    - tweet @ReactPHP or #reactphp
    125

    View Slide

  126. Need help? Want to help?
    - check each component’s README
    - check open issues
    - join #reactphp on irc.freenode.org
    - tweet @ReactPHP or #reactphp
    - Talk to me
    126

    View Slide

  127. Need help? Want to help?
    - check each component’s README
    - check open issues
    - join #reactphp on irc.freenode.org
    - tweet @ReactPHP or #reactphp
    - Talk to me
    Did I mention I’m available?
    127

    View Slide

  128. // thank you!
    $loop->stop();
    128
    @another_clue – https://lueck.tv/

    View Slide