Slide 1

Slide 1 text

Asynchronous programming with PHP Bruce (Baokun) Dou @doubaokun 18th Oct 2017

Slide 2

Slide 2 text

About me • Software Engineer and Architect • Twitter: @doubaokun • Email: [email protected] • Github: github.com/doubaokun • Application performance optimization • Realtime data processing system • Infrastructure

Slide 3

Slide 3 text

#APO Tracelytics / Appneta, 2015

Slide 4

Slide 4 text

Transfon.com
 
 APO => Cost reduction
 
 Saving the ?

Slide 5

Slide 5 text

I/O bound vs CPU bound

Slide 6

Slide 6 text

Communication vs Computation

Slide 7

Slide 7 text

• PHP (7) • Asynchoronous & non-blocking IO • Asynchoronous PHP • PHP Extension and PHP server • Swoole: Async programming framework Summary

Slide 8

Slide 8 text

PHP

Slide 9

Slide 9 text

Why PHP As of 2017, PHP comprises more than 80% of the websites on the Internet.

Slide 10

Slide 10 text

The performance of PHP 7

Slide 11

Slide 11 text

Is

Slide 12

Slide 12 text

Awesome

Slide 13

Slide 13 text

Traditional PHP model • LNMP (LAMP): Linux, Nginx, MySQL, PHP-FPM • Originally created as a templating language • Thread/process based model • Blocking IO and Sync • Stateless and scalable • FastCGI • Process per connection is memory expensive • Build up everything then destroy everything • Triggered by user request (No scheduler) • Process per connection is easy for 504 timeout

Slide 14

Slide 14 text

1.Receive the request 2.Load and compile files and codes 3.Initialize the objects and variables 4.Execute the functions 5.Send the response 6.Recycle the resources The life cycle of HTTP request in PHP

Slide 15

Slide 15 text

Pros and Cons of PHP Pros Cons Simple and easy No IoT (asynchronous I/O) Huge Ecosystem Difficult to implement Server side protocol and services: TCP Socket, SMTP, MQTT, WebSocket, Thrift, etc Sync Simple and easy Sync One wait stop all Stateless Stateless Scalable Low performance Robust No long-live connection No pooling

Slide 16

Slide 16 text

Async and non-blocking I/O

Slide 17

Slide 17 text

Sync vs Async http://mikecantelon.com/talks/trijug/preso.html#14

Slide 18

Slide 18 text

How to do async tasks Message Queue or Task Queue Restful services or RPC Ajax CURL Fork a new process/thread CRON vs I/O multiplexing, Non-blocking I/O, IPC

Slide 19

Slide 19 text

Why non-blocking I/O matters Multiple cores and fast CPU vs slow Network I/O

Slide 20

Slide 20 text

Why non-blocking I/O matters Multiple cores and fast CPU vs slow Network I/O

Slide 21

Slide 21 text

Linux I/O multiplexing, non-blocking • select() monitors the sockets’ status flag and return the status of all sockets, manually iterate through all the fds (C10K problem) • poll() similar to select() but allocate an array of pollfd, can handle more than 1024 sockets •epoll() monitors sockets’ status and trigger related events, and only return triggered events array.

Slide 22

Slide 22 text

Decoupling IO and Computation

Slide 23

Slide 23 text

libevent

Slide 24

Slide 24 text

libuv

Slide 25

Slide 25 text

Reactor pattern

Slide 26

Slide 26 text

Reactor pattern

Slide 27

Slide 27 text

• Node.js • Netty (Java) • Twisted (Python) • Ruby (EventMachine) • Akka (Scala) • Swoole (PHP) Reactor pattern

Slide 28

Slide 28 text

Async vs parallel Responsiveness vs Performance

Slide 29

Slide 29 text

Async PHP

Slide 30

Slide 30 text

The problems we like to sort out for PHP • High concurrency, large number of connections • Long-live connections, real-time services • Long-live connections to DB, Cache, RPC etc • Support more server side protocols • Server side programming • Scheduler • Async tasks need 3rd party services • Manage and reuse the status in memory • Performance

Slide 31

Slide 31 text

Async PHP server side options • ReactPHP • Workerman • Swoole

Slide 32

Slide 32 text

1.Receive the request 2.Load and compile files and codes 3.Initialize the objects and variables 4.Execute the functions 5.Send the response 6.Recycle the resources The life cycle of HTTP request in PHP

Slide 33

Slide 33 text

1.Load and compile files and codes 2.Initialize the objects and variables 3.Receive the request 4.Execute the functions 5.Send the response 6.Recycle the resources The life cycle of HTTP request in PHP

Slide 34

Slide 34 text

Not a replacement for 80% PHP web applications

Slide 35

Slide 35 text

But it opens the door for PHP developers: • RPC server • Websocket server • TCP server • High performance services • High concurrency systems (C100K) • Internet of things server • Compete with language X, Y, Z

Slide 36

Slide 36 text

PHP Extension

Slide 37

Slide 37 text

PHP Extension

Slide 38

Slide 38 text

Demo https://github.com/doubaokun/rxserver

Slide 39

Slide 39 text

Swoole https://www.swoole.co.uk/ High performance, Open source, Production ready async programming framework for PHP @php_swoole

Slide 40

Slide 40 text

https://gist.github.com/nkt/e49289321c744155484c

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

PSR-7 HTTP Message Interfaces http://symfony.com/blog/psr-7-support-in-symfony-is-here https://www.drupal.org/node/2492955 https://laravel.com/docs/5.1/requests

Slide 48

Slide 48 text

Not the End

Slide 49

Slide 49 text

Create something awesome with Swoole