Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Asynchronous programming with PHP

Bruce Dou
October 18, 2017

Asynchronous programming with PHP

* Why and How to utilize asynchronous in PHP
* How to implement a simple asynchronous server with PHP C extension
* Production ready, large-scale asynchronous programming with PHP language and Swoole

Bruce Dou

October 18, 2017
Tweet

More Decks by Bruce Dou

Other Decks in Technology

Transcript

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

    View full-size slide

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

    View full-size slide

  3. #APO
    Tracelytics / Appneta, 2015

    View full-size slide

  4. Transfon.com


    APO => Cost reduction


    Saving the ?

    View full-size slide

  5. I/O bound vs CPU bound

    View full-size slide

  6. Communication vs Computation

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  9. The performance of PHP 7

    View full-size slide

  10. 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

    View full-size slide

  11. 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

    View full-size slide

  12. 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

    View full-size slide

  13. Async and non-blocking I/O

    View full-size slide

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

    View full-size slide

  15. 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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  18. 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.

    View full-size slide

  19. Decoupling IO and Computation

    View full-size slide

  20. Reactor pattern

    View full-size slide

  21. Reactor pattern

    View full-size slide

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

    View full-size slide

  23. Async vs parallel
    Responsiveness
    vs
    Performance

    View full-size slide

  24. 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

    View full-size slide

  25. Async PHP server side options
    • ReactPHP
    • Workerman
    • Swoole

    View full-size slide

  26. 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

    View full-size slide

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

    View full-size slide

  28. Not a replacement
    for 80% PHP web applications

    View full-size slide

  29. 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

    View full-size slide

  30. PHP Extension

    View full-size slide

  31. PHP Extension

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  35. 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

    View full-size slide

  36. Create something awesome with Swoole

    View full-size slide