Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Hello! 3

Slide 4

Slide 4 text

$ whoami 4

Slide 5

Slide 5 text

$ whoami Christian Lück 5

Slide 6

Slide 6 text

$ whoami Christian Lück 6

Slide 7

Slide 7 text

$ whoami Christian Lueck 7

Slide 8

Slide 8 text

$ whoami Christian Lueck 8

Slide 9

Slide 9 text

$ whoami Christian Lueck @clue 9

Slide 10

Slide 10 text

$ whoami Christian Lueck @another_clue 10

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Who are you? 13 now that you know me…

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

PHP, the web of the ‘90s? 16

Slide 17

Slide 17 text

PHP and the web of the ‘90s - traditional LAMP stack - Request-Response-Cycle - PHP is too slow? 17 Apache Client PHP MySQL

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

PHP may not be pretty… 22

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Knock knock! Who’s there? 24

Slide 25

Slide 25 text

Knock knock! 2019! Who’s there? 25

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Node.js 27

Slide 28

Slide 28 text

Node.js 28 good fit, huge ecosystem

Slide 29

Slide 29 text

Node.js 29 good fit, huge ecosystem interesting concepts

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

nodejs? 31

Slide 32

Slide 32 text

no js! 32

Slide 33

Slide 33 text

Everybody’s favorite language… 33

Slide 34

Slide 34 text

PHP 34

Slide 35

Slide 35 text

PHP 35 gets the job done

Slide 36

Slide 36 text

PHP 36 gets the job done widespread usage

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

Enter React! 38

Slide 39

Slide 39 text

39

Slide 40

Slide 40 text

The other React™ 40

Slide 41

Slide 41 text

The REAL React™ 41

Slide 42

Slide 42 text

42

Slide 43

Slide 43 text

What is React? 43

Slide 44

Slide 44 text

What is React? non-blocking I/O 44

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

100% 47

Slide 48

Slide 48 text

100% pure PHP 48

Slide 49

Slide 49 text

100% pure PHP no extensions 49

Slide 50

Slide 50 text

100% pure PHP no extensions no magic 50

Slide 51

Slide 51 text

What does that even mean?! 51

Slide 52

Slide 52 text

the idea 52

Slide 53

Slide 53 text

the idea calculations are fast 53

Slide 54

Slide 54 text

the idea calculations are fast I/O is slow 54

Slide 55

Slide 55 text

I/O is everywhere

Slide 56

Slide 56 text

I/O is everywhere

Slide 57

Slide 57 text

I/O is everywhere

Slide 58

Slide 58 text

I/O is everywhere

Slide 59

Slide 59 text

I/O is everywhere

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

I/O is slow!

Slide 62

Slide 62 text

I/O is slow!

Slide 63

Slide 63 text

This is React 63

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

What React is not 67

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

React core components 71

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

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

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

Examples and demo time! 84

Slide 85

Slide 85 text

Examples and demo time! All open source with links 85

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

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

Slide 88

Slide 88 text

88

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

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

Slide 95

Slide 95 text

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

Slide 96

Slide 96 text

700 MiB/s 96

Slide 97

Slide 97 text

700 MiB/s 97

Slide 98

Slide 98 text

700 MiB/s 98 dockerd maxed out PHP not

Slide 99

Slide 99 text

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

Slide 100

Slide 100 text

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

Slide 101

Slide 101 text

5k requests/s 101

Slide 102

Slide 102 text

5k requests/s 102

Slide 103

Slide 103 text

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

Slide 104

Slide 104 text

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

Slide 105

Slide 105 text

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

Slide 106

Slide 106 text

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

Slide 107

Slide 107 text

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/

Slide 108

Slide 108 text

108

Slide 109

Slide 109 text

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

Slide 110

Slide 110 text

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

Slide 111

Slide 111 text

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

Slide 112

Slide 112 text

Redis server 112

Slide 113

Slide 113 text

Redis server 113 - Official Redis is written in C

Slide 114

Slide 114 text

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

Slide 115

Slide 115 text

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

Slide 116

Slide 116 text

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…

Slide 117

Slide 117 text

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

Slide 118

Slide 118 text

50k OP/s 118

Slide 119

Slide 119 text

50k OP/s 119

Slide 120

Slide 120 text

50k OP/s pure PHP 120

Slide 121

Slide 121 text

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

Slide 122

Slide 122 text

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

Slide 123

Slide 123 text

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

Slide 124

Slide 124 text

124

Slide 125

Slide 125 text

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

Slide 126

Slide 126 text

126

Slide 127

Slide 127 text

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

Slide 128

Slide 128 text

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

Slide 129

Slide 129 text

129

Slide 130

Slide 130 text

130

Slide 131

Slide 131 text

131

Slide 132

Slide 132 text

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

Slide 133

Slide 133 text

no polling 133

Slide 134

Slide 134 text

no polling 134 jobs instantly picked up

Slide 135

Slide 135 text

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

Slide 136

Slide 136 text

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

Slide 137

Slide 137 text

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

Slide 138

Slide 138 text

Conclusions 138

Slide 139

Slide 139 text

PHP 139

Slide 140

Slide 140 text

PHP faster than you probably thought 140

Slide 141

Slide 141 text

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

Slide 142

Slide 142 text

ReactPHP 142

Slide 143

Slide 143 text

ReactPHP 143 a real deal and here to stay

Slide 144

Slide 144 text

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

Slide 145

Slide 145 text

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

Slide 146

Slide 146 text

try! 146

Slide 147

Slide 147 text

try! whenever having to wait 147

Slide 148

Slide 148 text

try! whenever having to wait whenever accessing network 148

Slide 149

Slide 149 text

help! 149

Slide 150

Slide 150 text

help! elaborate documentation on ReactPHP.org 150

Slide 151

Slide 151 text

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

Slide 152

Slide 152 text

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

Slide 153

Slide 153 text

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

Slide 154

Slide 154 text

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

Slide 155

Slide 155 text

integration 155

Slide 156

Slide 156 text

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

Slide 157

Slide 157 text

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

Slide 158

Slide 158 text

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

Slide 159

Slide 159 text

Integration with traditional environments 159 integrating async into sync is easy

Slide 160

Slide 160 text

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

Slide 161

Slide 161 text

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

Slide 162

Slide 162 text

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

Slide 163

Slide 163 text

Avoid blocking! - The loop must not be blocked 163

Slide 164

Slide 164 text

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

Slide 165

Slide 165 text

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

Slide 166

Slide 166 text

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

Slide 167

Slide 167 text

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