Pushing the limits of PHP (WebEngAc19)

Pushing the limits of PHP (WebEngAc19)

It's 2019 and times have changed – yet PHP is still most often associated with your average product catalog 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 ReactPHP 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 to your toolbox.

D1b6700884ac0ae368918ad171bb6a75?s=128

Christian Lück

January 17, 2019
Tweet

Transcript

  1. 2.

    Agenda - Hello! - PHP, the web of the ‘90s?

    - Enter React - Core components - Examples and demo time - Conclusions 2
  2. 14.

    Who are you? 14 now that you know me… -

    PHP developers? - architecs / engineers?
  3. 15.

    Who are you? 15 now that you know me… -

    PHP developers? - architecs / engineers? - know React?
  4. 17.

    PHP and the web of the ‘90s - traditional LAMP

    stack - Request-Response-Cycle - PHP is too slow? 17 Apache Client PHP MySQL
  5. 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
  6. 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
  7. 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
  8. 21.

    PHP and the web of the ‘90s - traditional LAMP

    stack - Request-Response-Cycle - PHP is too slow? - We sure can improve this… 21 Apache Client PHP MySQL Apache Client FPM MySQL PHP PHP nginx Client FPM MySQL PHP PHP nginx Client FPM memcache PHP PHP MySQL
  9. 26.

    Knock knock! 2019! - Separation of concerns (Frontend↔Backend) - HTTP

    APIs (RESTful) - Integration with 3rd parties - Live-Data (ticker) - CLI tools Who’s there? 26
  10. 32.
  11. 34.
  12. 39.

    39

  13. 42.

    42

  14. 47.
  15. 65.
  16. 66.

    This is React 66 Start multiple I/O operations (non-blocking) Get

    notified when something happens (react) Don’t waste time waiting
  17. 69.

    What React is not React is not black magic /

    vodoo React is not a framework 69
  18. 70.

    What React is not React is not black magic /

    vodoo React is not a framework React is not the new buzz 70
  19. 74.

    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 74
  20. 75.

    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 75 $loop = Factory::create(); // something inbetween // pass the $loop around to all components $loop->run();
  21. 77.

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

    start timers - once - periodic - ticks 77 $loop->addTimer(0.5, function () { echo ‘world’; }); $loop->addTimer(0.3, function () { echo ‘hello’; });
  22. 78.

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

    start timers - once - periodic - ticks - wait for stream resources to become - readable - writable 78 $loop->addTimer(0.5, function () { echo ‘world’; }); $loop->addTimer(0.3, function () { echo ‘hello’; }); $loop->addReadStream($stream, $fn); $loop->addWriteStream($stream, $fn);
  23. 79.

    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) 79
  24. 80.

    Streams - interfaces, events and listeners: 80 $dest->write(‘hello’); $source->on(‘data’, function

    ($data) { var_dump($data); }); $source->on(‘close’, function () { echo ‘stream closed’; });
  25. 81.

    Streams - interfaces, events and listeners: 81 $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);
  26. 82.

    Promises - Placeholder for a single future result - Possible

    states: - pending - fulfilled (successfully resolved) - rejected (Exception occured) 82
  27. 83.

    Promises - no more imperative code flow - instead (tell,

    don’t ask) 83 $a->then($fulfilled = null, $rejected = null); $a->then(‘process’); $a->then(‘process’, ‘var_dump’);
  28. 87.

    Socket server 87 react/socket - THE canonical chat example -

    broadcast all incoming msgs - run example server - connect via telnet: $ telnet clue.engineering 6001
  29. 88.

    88

  30. 89.

    HTTP client 89 clue/buzz-react - Simple HTTP requests - inspired

    by kriswallsmith/buzz - PSR-7 compatible
  31. 90.

    HTTP client 90 clue/buzz-react - Simple HTTP requests - inspired

    by kriswallsmith/buzz - PSR-7 compatible - Promises and Streams - It’s fast…
  32. 91.

    HTTP client 91 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
  33. 92.

    Packagist API clue/packagist-api-react - get information about any Composer package

    - simple, Promise-based - lightweight wrapper between - KnpLabs/packagist-api - clue/buzz-react 92
  34. 93.

    Packagist API clue/packagist-api-react - get information about any Composer package

    - simple, Promise-based - lightweight wrapper between - KnpLabs/packagist-api - clue/buzz-react 93 - see its examples $ php examples/search.php
  35. 94.

    Docker client clue/docker-react - Run apps in isolated containers -

    “build, ship and run, anywhere” - Controlled through HTTP API - Promises and Streams 94
  36. 95.

    Docker client clue/docker-react - Run apps in isolated containers -

    “build, ship and run, anywhere” - Controlled through HTTP API - Promises and Streams 95 - see its promise examples $ php examples/info.php - see its streaming examples $ php examples/benchmark-exec.php
  37. 100.

    HTTP server react/http - Pure PHP, with no additional webserver

    - standard PSR-7 interfaces - Lots of third-party integrations with traditional frameworks (symfony, slim, silex, PIMF etc.) 100
  38. 104.
  39. 105.

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

    dual core i3 => 10k requests/s 36M requests/h
  40. 106.

    Server sent events clue/sse-react - Server sent events (SSE) -

    aka. EventSource (browser API) - Streaming events to browser - limited browser support 106
  41. 107.

    Server sent events clue/sse-react - Server sent events (SSE) -

    aka. EventSource (browser API) - Streaming events to browser - limited browser support 107 - see examples connecting to initial chat $ php examples/chat-server.php - open browser: http://clue.engineering:7000/
  42. 108.

    108

  43. 109.

    Websocket server cboden/ratchet - Async WebSocket server - bidirectional data

    flow between browser and server - better browser support 109
  44. 110.

    Redis client 110 clue/redis-react - Redis is a fast in-memory

    DB - very simple commands - very simple protocol - pipelined, Promise-based
  45. 111.

    Redis client 111 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
  46. 115.

    Redis server 115 clue/php-redis-server - Official Redis is written in

    C - Reimplementation is pure PHP - Very simple to add commands
  47. 116.

    Redis server 116 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…
  48. 117.

    Redis server 117 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
  49. 123.

    Redis framework 123 clue/php-redis-framework - Development preview - Very simple

    to add custom commands - run example server $ php examples/11-beer.php - connect via telnet: $ telnet IP 9000
  50. 124.

    124

  51. 126.

    126

  52. 128.

    Zenity clue/zenity-react - PHP desktop GUI applications - very simple,

    Promise-based 128 - see its simple examples $ php examples/01-dialog.php - see its more realistic examples $ php examples/06-menu.php $ php examples/03-progress-pulsate.php $ php examples/03-progress-random.php
  53. 129.

    129

  54. 130.

    130

  55. 131.

    131

  56. 136.

    no polling 136 jobs instantly picked up thousands of jobs

    per second concurrently processing hundreds or more
  57. 139.
  58. 145.
  59. 146.
  60. 149.
  61. 160.

    Integration with traditional environments 160 integrating async into sync is

    easy - just run the loop until you’re done - see clue/block-react
  62. 161.

    Integration with traditional environments 161 integrating async into sync is

    easy - just run the loop until you’re done - see clue/block-react integrating sync into async is hard
  63. 162.

    Integration with traditional environments 162 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
  64. 164.

    Avoid blocking! - The loop must not be blocked -

    Many functions / lib assume blocking by default - Anything >1ms should be reconsidered 164
  65. 165.

    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 165
  66. 166.

    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 166
  67. 167.

    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 167 Pay attention: - PDO, mysql etc. - file system access - network access - third-party APIs