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

How Swoole Blows Up Your Mind

Albert Chen
December 20, 2022
120

How Swoole Blows Up Your Mind

PHPCon Okinawa 2019

Albert Chen

December 20, 2022
Tweet

Transcript

  1. How Swoole
    Blows Up Your Mind?
    @PHPCon Okinawa 2019
    By Albert Chen

    View full-size slide

  2. About Me
    • Albert Chen
    • Software Architect
    • M17 HandsUp
    • Open Source Maintainer

    View full-size slide

  3. What is Swoole?

    View full-size slide

  4. 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)
    • High performance for high concurrency

    View full-size slide

  5. Why PHP Performs
    Poorly in High Concurrency?

    View full-size slide

  6. Lifecycle in PHP

    View full-size slide

  7. Lifecycle in PHP
    PHP

    Files
    Tokenizing
    Semantic
    Parsing
    AST
    Generate
    Bytecode
    Execute
    Bytecode
    Output
    Load Bytecode
    From Memory
    Check
    Bytecode Cache

    View full-size slide

  8. Blocking I/O in PHP
    Request
    Request
    Request
    Request
    PHP
    FPM
    FPM
    Pool
    FPM
    Pool
    Worker
    Worker
    Worker
    Worker
    Blocking I/O

    View full-size slide

  9. Eventloop in NodeJS

    View full-size slide

  10. There’s no Eventloop
    in Traditional PHP

    View full-size slide

  11. Let’s See What Changes
    in Swoole?

    View full-size slide

  12. Structure in Swoole Server

    View full-size slide

  13. Structure in Swoole Server
    • Code Is Persistent in Memory
    • Swoole Takes Lifecycle Over PHP
    • Reduce Initialization Steps
    • Reduce Response Latency
    • Event-driven Non-blocking I/O for Requests

    View full-size slide

  14. Benchmark Comparison
    of Go with Swoole

    View full-size slide

  15. Server Benchmark in Go

    View full-size slide

  16. Server Benchmark in Go

    View full-size slide

  17. Server Benchmark in Swoole

    View full-size slide

  18. Server Benchmark in Swoole

    View full-size slide

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

    View full-size slide

  20. Coroutine in Swoole

    View full-size slide

  21. Coroutine in Swoole
    • Blocking I/Os Are Scheduled By Coroutine Automatically.

    View full-size slide

  22. Coroutine in Swoole
    • Blocking I/Os Are Scheduled By Coroutine Automatically.

    View full-size slide

  23. Coroutine in Swoole
    • CSP Model

    View full-size slide

  24. Does This Mean Every
    Blocking I/O Doesn’t Block Anymore?

    View full-size slide

  25. Coroutine in Swoole
    • How about This?

    View full-size slide

  26. Coroutine in Swoole
    • How about This?

    View full-size slide

  27. Runtime Hooks
    Transform Blocking I/Os into
    Coroutines with Magic!

    View full-size slide

  28. Use Case
    In HandsUp

    View full-size slide

  29. Use Case
    Facebook
    Webhooks
    Message
    Load
    Balancer
    Server
    Server
    Server
    Queue
    PubNub

    Service
    Analysis
    Service
    Swoole Service
    PubSub
    Many to Many
    Queue Workers
    High Concurrent
    Requests

    View full-size slide

  30. Use Case
    • Replace Guzzle Handler with Swoole Client

    View full-size slide