Pushing the limits of PHP (SymfonyLive Berlin)

Pushing the limits of PHP (SymfonyLive Berlin)

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 interesting examples and live demos learn why what sounds crazy at first might soon be a valuable addition in your toolbox.

D1b6700884ac0ae368918ad171bb6a75?s=128

Christian Lück

September 27, 2019
Tweet

Transcript

  1. 2.

    pushing the limits of PHP why ReactPHP is awesome and

    why YOU should care @another_clue
  2. 3.

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

    - Enter React - Core components - Examples and demo time - Conclusions 3
  3. 15.

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

    PHP developers? - architects / engineers?
  4. 16.

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

    PHP developers? - architects / engineers? - know React?
  5. 21.

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

    Request-Response-Cycle 21 Apache Client PHP MySQL
  6. 22.

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

    Request-Response-Cycle PHP is too slow? 22 Apache Client PHP MySQL
  7. 23.

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

    Request-Response-Cycle PHP is too slow? 23 Apache Client MySQL FPM PHP PHP
  8. 24.

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

    Request-Response-Cycle PHP is too slow? 24 nginx Client MySQL FPM PHP PHP
  9. 25.

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

    Request-Response-Cycle PHP is too slow? 25 nginx Client FPM PHP PHP memcache MySQL
  10. 30.

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

    APIs (RESTful) - Integration with 3rd parties - Live-Data (ticker) - CLI tools Who’s there? 30
  11. 36.
  12. 38.
  13. 43.

    43

  14. 46.

    46

  15. 51.
  16. 62.

    I/O is everywhere third party HTTP APIs (RESTful, SOAP, you

    name it…) mysql, postgres filesystem I/O (session files) 62
  17. 63.

    I/O is everywhere third party HTTP APIs (RESTful, SOAP, you

    name it…) mysql, postgres filesystem I/O (session files) redis, memcache 63
  18. 69.
  19. 70.

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

    notified when something happens (react) Don’t waste time waiting
  20. 73.

    What React is not React is not black magic /

    vodoo React is not a framework 73
  21. 74.

    What React is not React is not black magic /

    vodoo React is not a framework React is not the new buzz 74
  22. 78.

    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 78
  23. 79.

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

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

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

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

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

    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) 83
  27. 84.

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

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

    Streams - interfaces, events and listeners: 85 $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);
  29. 86.

    Promises - Placeholder for a single future result - Possible

    states: - pending - fulfilled (successfully resolved) - rejected (Exception occured) 86
  30. 87.

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

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

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

    broadcast all incoming msgs - run example server - connect via telnet: $ telnet live.clue.engineering 8000
  32. 92.

    92

  33. 93.

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

    by kriswallsmith/buzz - PSR-7 compatible
  34. 94.

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

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

    HTTP client 95 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
  36. 96.

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

    - simple, Promise-based - lightweight wrapper between - KnpLabs/packagist-api - clue/buzz-react 96
  37. 97.

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

    - simple, Promise-based - lightweight wrapper between - KnpLabs/packagist-api - clue/buzz-react 97 - see its examples $ php examples/search.php
  38. 98.

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

    “build, ship and run, anywhere” - Controlled through HTTP API - Promises and Streams 98
  39. 99.

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

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

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

    - standard PSR-7 interfaces - Lots of third-party integrations with traditional frameworks (Symfony, Slim etc.) 104
  41. 108.

    11k requests/s 108 this is a local, single core benchmark!

    quad core i7 + HT => ~88k requests/s
  42. 109.

    11k requests/s 109 this is a local, single core benchmark!

    quad core i7 + HT => ~88k requests/s 40M requests/h (single core)
  43. 110.

    11k requests/s 110 this is a local, single core benchmark!

    quad core i7 + HT => ~88k requests/s 40M requests/h (single core) ~300M requests/h
  44. 111.

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

    aka. EventSource (browser API) - Streaming events to browser - limited browser support 111
  45. 112.

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

    aka. EventSource (browser API) - Streaming events to browser - limited browser support 112 - see examples connecting to initial chat $ php examples/chat-server.php - open browser: http://live.clue.engineering:8080/
  46. 113.

    113

  47. 114.

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

    flow between browser and server - better browser support 114
  48. 115.

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

    DB - very simple commands - very simple protocol - pipelined, Promise-based
  49. 116.

    Redis client 116 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
  50. 120.

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

    C - Reimplementation is pure PHP - Very simple to add commands
  51. 121.

    Redis server 121 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…
  52. 122.

    Redis server 122 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: ~150k OP/s
  53. 128.

    Redis framework 128 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
  54. 129.

    129

  55. 131.

    131

  56. 133.

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

    Promise-based 133 - 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
  57. 134.

    134

  58. 135.

    135

  59. 136.

    136

  60. 141.

    no polling 141 jobs instantly picked up thousands of jobs

    per second concurrently processing hundreds or more
  61. 146.

    YES 146 But you might be missing the point… Can

    I use ReactPHP to make my website 1000x faster?
  62. 147.
  63. 153.
  64. 154.
  65. 157.
  66. 168.

    Integration with traditional environments 168 integrating async into sync is

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

    Integration with traditional environments 169 integrating async into sync is

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

    Integration with traditional environments 170 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
  69. 172.

    Avoid blocking! - The loop must not be blocked -

    Many functions / lib assume blocking by default - Anything >1ms should be reconsidered 172
  70. 173.

    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 173
  71. 174.

    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 174
  72. 175.

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