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. pushing the limits of PHP Christian “@another_clue” Lück

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

    - Enter React - Core components - Examples and demo time - Conclusions 2
  3. Hello! 3

  4. $ whoami 4

  5. $ whoami Christian Lück 5

  6. $ whoami Christian Lück 6

  7. $ whoami Christian Lueck 7

  8. $ whoami Christian Lueck 8

  9. $ whoami Christian Lueck @clue 9

  10. $ whoami Christian Lueck @another_clue 10

  11. $ whoami Christian Lueck @another_clue passionate about pushing the limits

    11
  12. $ whoami Christian Lueck @another_clue passionate about pushing the limits

    freelance software engineer 12
  13. Who are you? 13 now that you know me…

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

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

    PHP developers? - architecs / engineers? - know React?
  16. PHP, the web of the ‘90s? 16

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

    stack - Request-Response-Cycle - PHP is too slow? 17 Apache Client PHP MySQL
  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
  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
  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
  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
  22. PHP may not be pretty… 22

  23. PHP may not be pretty… but it gets the job

    done! 23
  24. Knock knock! Who’s there? 24

  25. Knock knock! 2019! Who’s there? 25

  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
  27. Node.js 27

  28. Node.js 28 good fit, huge ecosystem

  29. Node.js 29 good fit, huge ecosystem interesting concepts

  30. Node.js 30 good fit, huge ecosystem interesting concepts npm install…

  31. nodejs? 31

  32. no js! 32

  33. Everybody’s favorite language… 33

  34. PHP 34

  35. PHP 35 gets the job done

  36. PHP 36 gets the job done widespread usage

  37. PHP 37 gets the job done widespread usage if PHP

    can do it…
  38. Enter React! 38

  39. 39

  40. The other React™ 40

  41. The REAL React™ 41

  42. 42

  43. What is React? 43

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

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

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

  47. 100% 47

  48. 100% pure PHP 48

  49. 100% pure PHP no extensions 49

  50. 100% pure PHP no extensions no magic 50

  51. What does that even mean?! 51

  52. the idea 52

  53. the idea calculations are fast 53

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

  55. I/O is everywhere

  56. I/O is everywhere

  57. I/O is everywhere

  58. I/O is everywhere

  59. I/O is everywhere

  60. 60 Source: Latency Numbers Every Programmer Should Know: https://gist.github.com/jboner/2841832 CPU

    vs I/O
  61. I/O is slow!

  62. I/O is slow!

  63. This is React 63

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

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

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

    notified when something happens (react) Don’t waste time waiting
  67. What React is not 67

  68. What React is not React is not black magic /

    vodoo 68
  69. What React is not React is not black magic /

    vodoo React is not a framework 69
  70. What React is not React is not black magic /

    vodoo React is not a framework React is not the new buzz 70
  71. React core components 71

  72. React Core components - Event loop (reactor) - Streams -

    Promises 72
  73. Event loop Consumers - THE core, low-level component 73

  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
  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();
  76. Event loop Implementors - Reactor pattern (hence the name) 76

  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’; });
  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);
  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
  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’; });
  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);
  82. Promises - Placeholder for a single future result - Possible

    states: - pending - fulfilled (successfully resolved) - rejected (Exception occured) 82
  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’);
  84. Examples and demo time! 84

  85. Examples and demo time! All open source with links 85

  86. Socket server 86 react/socket - THE canonical chat example -

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

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

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

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

    by kriswallsmith/buzz - PSR-7 compatible - Promises and Streams - It’s fast…
  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
  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
  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
  94. Docker client clue/docker-react - Run apps in isolated containers -

    “build, ship and run, anywhere” - Controlled through HTTP API - Promises and Streams 94
  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
  96. 700 MiB/s 96

  97. 700 MiB/s 97

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

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

    - standard PSR-7 interfaces 99
  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
  101. 5k requests/s 101

  102. 5k requests/s 102

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

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

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

    dual core i3 => 10k requests/s 36M requests/h
  106. Server sent events clue/sse-react - Server sent events (SSE) -

    aka. EventSource (browser API) - Streaming events to browser - limited browser support 106
  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/
  108. 108

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

    flow between browser and server - better browser support 109
  110. Redis client 110 clue/redis-react - Redis is a fast in-memory

    DB - very simple commands - very simple protocol - pipelined, Promise-based
  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
  112. Redis server 112

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

  114. Redis server 114 clue/php-redis-server - Official Redis is written in

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

    C - Reimplementation is pure PHP - Very simple to add commands
  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…
  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
  118. 50k OP/s 118

  119. 50k OP/s 119

  120. 50k OP/s pure PHP 120

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

  122. Redis framework 122 clue/php-redis-framework - Development preview - Very simple

    to add custom commands
  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
  124. 124

  125. Quassel clue/quassel-react - Quassel IRC core - event-driven IRC chatbots

    - re-using existing identity 125
  126. 126

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

    Promise-based 127
  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
  129. 129

  130. 130

  131. 131

  132. BunnyPHP bunny/bunny - AMQP (RabbitMQ) - job worker processes -

    run slow things in background 132
  133. no polling 133

  134. no polling 134 jobs instantly picked up

  135. no polling 135 jobs instantly picked up thousands of jobs

    per second
  136. no polling 136 jobs instantly picked up thousands of jobs

    per second concurrently processing hundreds or more
  137. many, MANY more third-party projects: https://github.com/reactphp/react/wiki/Users 137

  138. Conclusions 138

  139. PHP 139

  140. PHP faster than you probably thought 140

  141. PHP faster than you probably thought more versatile than you

    probably thought 141
  142. ReactPHP 142

  143. ReactPHP 143 a real deal and here to stay

  144. ReactPHP 144 a real deal and here to stay stable

    & production ready
  145. ReactPHP 145 a real deal and here to stay stable

    & production ready *awesome*
  146. try! 146

  147. try! whenever having to wait 147

  148. try! whenever having to wait whenever accessing network 148

  149. help! 149

  150. help! elaborate documentation on ReactPHP.org 150

  151. help! elaborate documentation on ReactPHP.org tweet @ReactPHP or #reactphp 151

  152. help! elaborate documentation on ReactPHP.org tweet @ReactPHP or #reactphp Talk

    to me 152
  153. help! elaborate documentation on ReactPHP.org tweet @ReactPHP or #reactphp Talk

    to me Did I mention I’m available? 153
  154. // thank you! $loop->stop(); 154 @another_clue – https://lueck.tv/

  155. integration 155

  156. integration non-blocking and blocking don’t mix well 156

  157. integration non-blocking and blocking don’t mix well decide for either

    approach 157
  158. integration non-blocking and blocking don’t mix well decide for either

    approach isolate & communicate 158
  159. Integration with traditional environments 159 integrating async into sync is

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

    easy - just run the loop until you’re done - see clue/block-react
  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
  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
  163. Avoid blocking! - The loop must not be blocked 163

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

    Many functions / lib assume blocking by default - Anything >1ms should be reconsidered 164
  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
  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
  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