$30 off During Our Annual Pro Sale. View Details »

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.

Christian Lück

January 17, 2019
Tweet

More Decks by Christian Lück

Other Decks in Programming

Transcript

  1. pushing the limits of PHP
    Christian “@another_clue” Lück

    View Slide

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

    View Slide

  3. Hello!
    3

    View Slide

  4. $ whoami
    4

    View Slide

  5. $ whoami
    Christian Lück
    5

    View Slide

  6. $ whoami
    Christian Lück
    6

    View Slide

  7. $ whoami
    Christian Lueck
    7

    View Slide

  8. $ whoami
    Christian Lueck
    8

    View Slide

  9. $ whoami
    Christian Lueck
    @clue
    9

    View Slide

  10. $ whoami
    Christian Lueck
    @another_clue
    10

    View Slide

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

    View Slide

  12. $ whoami
    Christian Lueck
    @another_clue
    passionate about pushing the limits
    freelance software engineer 12

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  17. PHP and the web of the ‘90s
    - traditional LAMP stack
    - Request-Response-Cycle
    - PHP is too slow?
    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

    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

    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

    View Slide

  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

    View Slide

  22. PHP may not be pretty…
    22

    View Slide

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

    View Slide

  24. Knock knock!
    Who’s there?
    24

    View Slide

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

    View Slide

  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

    View Slide

  27. Node.js
    27

    View Slide

  28. Node.js
    28
    good fit, huge ecosystem

    View Slide

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

    View Slide

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

    View Slide

  31. nodejs?
    31

    View Slide

  32. no js!
    32

    View Slide

  33. Everybody’s favorite language…
    33

    View Slide

  34. PHP
    34

    View Slide

  35. PHP
    35
    gets the job done

    View Slide

  36. PHP
    36
    gets the job done
    widespread usage

    View Slide

  37. PHP
    37
    gets the job done
    widespread usage
    if PHP can do it…

    View Slide

  38. Enter React!
    38

    View Slide

  39. 39

    View Slide

  40. The other React™
    40

    View Slide

  41. The REAL React™
    41

    View Slide

  42. 42

    View Slide

  43. What is React?
    43

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  47. 100%
    47

    View Slide

  48. 100%
    pure PHP
    48

    View Slide

  49. 100%
    pure PHP
    no extensions
    49

    View Slide

  50. 100%
    pure PHP
    no extensions
    no magic
    50

    View Slide

  51. What does that even mean?!
    51

    View Slide

  52. the idea
    52

    View Slide

  53. the idea
    calculations are fast
    53

    View Slide

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

    View Slide

  55. I/O is everywhere

    View Slide

  56. I/O is everywhere

    View Slide

  57. I/O is everywhere

    View Slide

  58. I/O is everywhere

    View Slide

  59. I/O is everywhere

    View Slide

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

    View Slide

  61. I/O is slow!

    View Slide

  62. I/O is slow!

    View Slide

  63. This is React
    63

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  67. What React is not
    67

    View Slide

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

    View Slide

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

    View Slide

  70. What React is not
    React is not black magic / vodoo
    React is not a framework
    React is not the new buzz
    70

    View Slide

  71. React core components
    71

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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();

    View Slide

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

    View Slide

  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’;
    });

    View Slide

  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);

    View Slide

  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

    View Slide

  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’;
    });

    View Slide

  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);

    View Slide

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

    View Slide

  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’);

    View Slide

  84. Examples and demo time!
    84

    View Slide

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

    View Slide

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

    View Slide

  87. Socket server
    87
    react/socket
    - THE canonical chat example
    - broadcast all incoming msgs
    - run example server
    - connect via telnet:
    $ telnet clue.engineering 6001

    View Slide

  88. 88

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

  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

    View Slide

  96. 700 MiB/s
    96

    View Slide

  97. 700 MiB/s
    97

    View Slide

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

    View Slide

  99. HTTP server
    react/http
    - Pure PHP, with
    no additional webserver
    - standard PSR-7 interfaces
    99

    View Slide

  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

    View Slide

  101. 5k requests/s
    101

    View Slide

  102. 5k requests/s
    102

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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/

    View Slide

  108. 108

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  112. Redis server
    112

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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…

    View Slide

  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

    View Slide

  118. 50k OP/s
    118

    View Slide

  119. 50k OP/s
    119

    View Slide

  120. 50k OP/s
    pure PHP
    120

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  124. 124

    View Slide

  125. Quassel
    clue/quassel-react
    - Quassel IRC core
    - event-driven IRC chatbots
    - re-using existing identity
    125

    View Slide

  126. 126

    View Slide

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

    View Slide

  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

    View Slide

  129. 129

    View Slide

  130. 130

    View Slide

  131. 131

    View Slide

  132. BunnyPHP
    bunny/bunny
    - AMQP (RabbitMQ)
    - job worker processes
    - run slow things in background
    132

    View Slide

  133. no polling
    133

    View Slide

  134. no polling
    134
    jobs instantly picked up

    View Slide

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

    View Slide

  136. no polling
    136
    jobs instantly picked up
    thousands of jobs per second
    concurrently processing hundreds or more

    View Slide

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

    View Slide

  138. Conclusions
    138

    View Slide

  139. PHP
    139

    View Slide

  140. PHP
    faster than you probably thought
    140

    View Slide

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

    View Slide

  142. ReactPHP
    142

    View Slide

  143. ReactPHP
    143
    a real deal and here to stay

    View Slide

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

    View Slide

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

    View Slide

  146. try!
    146

    View Slide

  147. try!
    whenever having to wait
    147

    View Slide

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

    View Slide

  149. help!
    149

    View Slide

  150. help!
    elaborate documentation on ReactPHP.org
    150

    View Slide

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

    View Slide

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

    View Slide

  153. help!
    elaborate documentation on ReactPHP.org
    tweet @ReactPHP or #reactphp
    Talk to me
    Did I mention I’m available? 153

    View Slide

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

    View Slide

  155. integration
    155

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide