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

How Swoole Blows Up Your Mind

Albert Chen
December 20, 2022
25

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 Slide

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

    View Slide

  3. What is Swoole?

    View Slide

  4. View Slide

  5. 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 Slide

  6. Why PHP Performs
    Poorly in High Concurrency?

    View Slide

  7. Lifecycle in PHP

    View Slide

  8. Lifecycle in PHP
    PHP

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

    View Slide

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

    View Slide

  10. Eventloop in NodeJS

    View Slide

  11. There’s no Eventloop
    in Traditional PHP

    View Slide

  12. Let’s See What Changes
    in Swoole?

    View Slide

  13. Structure in Swoole Server

    View Slide

  14. 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 Slide

  15. Benchmark Comparison
    of Go with Swoole

    View Slide

  16. Server Benchmark in Go

    View Slide

  17. Server Benchmark in Go

    View Slide

  18. Server Benchmark in Swoole

    View Slide

  19. Server Benchmark in Swoole

    View Slide

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

    View Slide

  21. Coroutine in Swoole

    View Slide

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

    View Slide

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

    View Slide

  24. Coroutine in Swoole
    • CSP Model

    View Slide

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

    View Slide

  26. Coroutine in Swoole
    • How about This?

    View Slide

  27. Coroutine in Swoole
    • How about This?

    View Slide

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

    View Slide

  29. Use Case
    In HandsUp

    View Slide

  30. 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 Slide

  31. Use Case
    • Replace Guzzle Handler with Swoole Client

    View Slide

  32. Q&A

    View Slide