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

Build Your High Concurrency App With PHP and Swoole

Albert Chen
December 20, 2022

Build Your High Concurrency App With PHP and Swoole

Jomlaunch Asia 2021

Albert Chen

December 20, 2022
Tweet

More Decks by Albert Chen

Other Decks in Programming

Transcript

  1. @JomLaunch 2021
    Build your High Concurrency App
    with PHP and Swoole
    Albert Chen

    View full-size slide

  2. About Me
    {

    "data": {

    "human": {

    "name": "Albert Chen",

    "occupation": "Software Architect",

    "website": "https://albert-chen.com",

    "interests": [

    "traveling",

    "programming",

    "cooking"

    ]

    },

    "tags": [

    "Laravel Artisan",

    "Swoole Enthusiast",

    "Open Source Contributor"

    ]

    }

    }

    View full-size slide

  3. Outline
    • Introduction of Swoole

    • PHP’s Internal Lifecycle

    • Blocking I/O in PHP

    • Server Structure of Swoole

    • Benchmark of Swoole

    • Coroutine in Swoole

    • Some Facts about Swoole

    • Q&A

    View full-size slide

  4. Slido
    Join at

    slido.com
    #760920

    View full-size slide

  5. Slido
    Join at


    slido.com
    #760920

    View full-size slide

  6. What is Swoole?

    View full-size slide

  7. What is Swoole?
    • Since 2012

    • Open source based on Apache-2.0 License

    • 17k stars until July 2021

    • More than 100 contributors

    • Adopted by many enterprises in China

    • O
    ffi
    cially supported by Laravel Octane

    View full-size slide

  8. What is Swoole?
    • A C extension for PHP

    • An asynchronous network engine for PHP

    • Features:

    • Event-driven non-blocking I/O

    • HTTP / HTTP2 / Websocket / TCP / UDP

    • Coroutine (CSP Model)

    • Excellent performance for high concurrency

    View full-size slide

  9. What is Swoole?
    • Not a new language, but an extension for PHP

    • Provides many new features for traditional PHP

    • New lifecycle in Swoole

    • Server patterns, coroutine, async I/O, Process Management, etc.

    • It's still under active development

    • Stable for production environment

    • High performance

    View full-size slide

  10. Usage Statistics for Websites

    View full-size slide

  11. PHP’s Benchmark
    https://kinsta.com/blog/php-benchmarks/

    View full-size slide

  12. Benchmark Comparison
    https://www.toptal.com/back-end/server-side-io-performance-node-php-java-go

    View full-size slide

  13. Why PHP Performs
    Poorly in High Concurrency?

    View full-size slide

  14. PHP Lifecycle

    View full-size slide

  15. How many files are required for one
    request in Laravel?
    426
    get_included_files()

    In Laravel 8.52

    View full-size slide

  16. Stateless PHP
    • How do we serve PHP today?

    • PHP-FPM

    • mod_php for Apache

    • Both patterns are all stateless

    View full-size slide

  17. Stateless PHP
    • Pros
    • Easy scaling

    • Simple, less risk causing memory leaks

    • Cons
    • States can't be shared between requests

    • States must rely on external storages

    • Resources can't be reused e
    ffi
    ciently

    • Connection cost is expensive (like database)

    • Not good for high performance

    View full-size slide

  18. Blocking I/O in PHP
    • PHP is originally created as glue layer to call C functions

    • The I/O model is blocking by default

    • Almost all client-side libraries involving I/O are blocking

    • Multiple process for handling blocking I/O

    • I/O bound concurrency depends on process number

    • Cost for context switch in processes is expensive

    View full-size slide

  19. Blocking I/O in PHP
    • Resource can't be reused

    • I/O requests are blocking

    • 80% time cost on blocking I/O

    View full-size slide

  20. Blocking I/O in PHP

    View full-size slide

  21. Event Loop in NodeJS

    View full-size slide

  22. Concurrency Problem
    • 100 requests at the same time need 100 processes

    • 100 connections will be established as well

    • Scale to increase concurrency

    View full-size slide

  23. Let’s See What Changes
    in Swoole

    View full-size slide

  24. Server Structure in Swoole

    View full-size slide

  25. PHP Lifecycle in Swoole
    Run only once!

    View full-size slide

  26. Server Structure in Swoole
    • Code Is Persistent in Memory

    • Swoole Server Takes Lifecycle Over PHP

    • Reduce Initialization Steps

    • Reduce Response Latency

    • Event-driven Non-blocking I/O for Requests

    View full-size slide

  27. Benchmark Comparison
    between Go and Swoole

    View full-size slide

  28. Server Benchmark in Go

    View full-size slide

  29. Server Benchmark in Go

    View full-size slide

  30. Server Benchmark in PHP

    View full-size slide

  31. Server Benchmark in PHP

    View full-size slide

  32. This Doesn’t Mean
    PHP with Swoole is Faster Than Go
    (Of Course Not)

    View full-size slide

  33. Coroutine in Swoole

    View full-size slide

  34. Coroutine in Swoole
    • Non-Blocking I/Os are scheduled by coroutine automatically.
    PHP is the best

    Swoole is awesome!

    Hello world!

    View full-size slide

  35. Coroutine in Swoole
    Hello Swoole

    1.2.3.4

    • Non-Blocking I/Os are scheduled by coroutine automatically.

    View full-size slide

  36. Coroutine in Swoole
    1.2.3.4

    1.2.3.4

    1.2.3.4

    1.2.3.4



    Hello Swoole!

    • Non-Blocking I/Os are scheduled by coroutine automatically.

    View full-size slide

  37. Coroutine in Swoole
    Hello Swoole!

    1.2.3.4

    1.2.3.4

    1.2.3.4

    1.2.3.4



    • Non-Blocking I/Os are scheduled by coroutine automatically.

    View full-size slide

  38. Coroutine in Swoole
    • CSP Model
    1

    2

    3

    4

    5

    View full-size slide

  39. Some Facts about Swoole
    • Most of traditional PHP projects needs to be refactored.

    • Convert requests and responses

    View full-size slide

  40. Some Facts about Swoole
    • Global states in PHP needs to be used more carefully. (eg. $_GET,
    $_POST, static, global)

    View full-size slide

  41. Some Facts about Swoole
    • Global states in PHP needs to be used more carefully. (eg. $_GET,
    $_POST, static, global)

    View full-size slide

  42. Some Facts about Swoole
    • Don’t use exit/die function in your PHP application.

    View full-size slide

  43. Some Facts about Swoole
    • Memory leak may happen!!!

    View full-size slide

  44. Some Facts about Swoole
    • You can try Hyperf framework for your new projects.
    Hyperf = Hyperspeed + Flexibility
    https://www.hyperf.io

    View full-size slide

  45. Some Facts about Swoole
    • Swoole is now o
    ffi
    cially supported by Laravel team.

    • Laravel will become friendly in long-running server

    • Some Octane features rely on Swoole

    • No coroutine features are supported yet

    View full-size slide

  46. Some Facts about Swoole
    • There’s knowledge gap for traditional PHP developers.

    View full-size slide