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

Deep Dive into Laravel Octane

Albert Chen
December 20, 2022

Deep Dive into Laravel Octane

LaravelVueConf Taiwan 2021

Albert Chen

December 20, 2022
Tweet

More Decks by Albert Chen

Other Decks in Programming

Transcript

  1. @LaravelConf Taiwan 2021
    Deep Dive into Laravel Octane
    Albert Chen

    View 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 Slide

  3. Outline
    • Lifecycle in PHP and Laravel

    • Introduction of Octane

    • Reminders in Long-Lived PHP

    • Service Container

    • Concurrent Tasks

    • Other Features in Octane

    • Blocking I/O in PHP

    • Coroutine

    • Benchmark

    • Q&A

    View Slide

  4. Slido
    Join at

    slido.com
    #257351

    View Slide

  5. Slido
    Join at


    slido.com
    #257351

    View Slide

  6. Why PHP Performs
    Poorly in High Concurrency?

    View Slide

  7. Lifecycle in PHP

    View Slide

  8. Lifecycle in Laravel
    Autoload Load App Bootstrap
    Register


    Service
    Providers
    Boot


    Service
    Providers
    Http


    Kernel
    Middleware
    Dispatch
    by Router
    Routes


    Match
    Controller
    Response
    Terminate

    Middleware
    Request
    public/
    index.php

    View Slide

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

    In Laravel 8.52

    View Slide

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

    • PHP-FPM

    • mod_php for Apache

    • Both patterns are all stateless

    View Slide

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

  12. Laravel Octane

    View Slide

  13. Laravel Octane
    • Published in April 2021

    • Maintained by o
    ff i
    cial Laravel team

    • Supports Swoole and Roadrunner

    • Requires Laravel 8 and PHP 8

    • Becoming more friendly in long-lived app

    • Hot reload support

    • Brings additional features

    View Slide

  14. What is RoadRunner?
    • Built with Golang

    • A replacement for web server and PHP-FPM

    • Works on both Linux and Windows

    • HTTPS and HTTP/2 support (including HTTP/2 Push, H2C)

    • No external PHP dependencies

    View Slide

  15. What is RoadRunner?
    (Process Manager)
    Request HTTP


    Handler
    Load
    Balancer
    PSR-7

    Consumer
    Workers


    Pool
    RoadRunner

    View Slide

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

  17. Server Structure in Swoole

    View Slide

  18. Lifecycle in Octane
    Reset
    Prepare


    Sandbox
    Request
    WarmUp
    Middleware
    Dispatch
    by Router
    Routes


    Match
    Controller
    Response
    Terminate

    Middleware
    Run only once

    View Slide

  19. PHP Lifecycle in Octane
    Run only once!

    View Slide

  20. Reminders in Long-Lived PHP
    • Global States Pollution
    • Global states will be shared in
    di
    ff
    erent requests.

    • Use global variables carefully
    unless you know what you’re
    doing in long-lived PHP.

    • You need to reset these states in
    the beginning of request if you
    don’t want to share them.

    View Slide

  21. • Potential Memory Leaks
    Reminders in Long-Lived PHP

    View Slide

  22. • Don’t exit in your PHP code
    Reminders in Long-Lived PHP

    View Slide

  23. Static variables in Laravel?

    View Slide

  24. Laravel’s Service Container
    $resolved = [];
    $aliases = [];
    $bindings = [];
    $instances = [];
    $serviceProviders = [];
    $loadedProviders = [];
    Container

    View Slide

  25. Laravel’s Service Container
    protected static $app;
    protected static
    $resolvedInstance;
    Facades
    Service Container
    • $app->singleton(‘event’, …)


    • $app->singleton(‘db’, …)


    • $app->singleton(‘auth’, …)
    Service Provider
    event db auth

    View Slide

  26. Containers in Octane
    • Warm Up Initial Container
    Container
    Auth Cache Con
    fi
    g Cookie
    DB Encrypt Files Hash
    Log Router Routes Session
    Trans Url View
    Default Services
    Register

    View Slide

  27. Containers in Octane
    • Container Dependencies
    Http Kernel
    App
    Router
    Container
    Routes Collection
    Route
    Container
    Route
    Container

    View Slide

  28. Containers in Octane
    • Setup Sandbox Container
    Clone
    Initial
    Container
    Sandbox
    Reset States
    Replace Container
    Register Other Services
    Handle Request

    View Slide

  29. • Container dependencies in singleton
    Reminders in Octane

    View Slide

  30. • Container dependencies in singleton
    Reminders in Octane

    View Slide

  31. • Container dependencies in singleton
    Reminders in Octane

    View Slide

  32. Concurrent Tasks

    View Slide

  33. • PHP’s Blocking I/O Model
    Concurrent Tasks
    Process Send API A Send API B Query DB A Query DB B
    50ms 50ms 20ms 20ms
    140ms in total
    Request

    View Slide

  34. Concurrent Tasks
    • PHP’s Blocking I/O Model
    Process
    50ms
    Child
    Child
    Child
    Child
    Fork
    Return
    Send API request A
    Query DB A
    Query DB B
    50ms
    Send API request A
    20ms
    20ms
    Request

    View Slide

  35. Concurrent Tasks
    • Concurrent Tasks in Octane (Swoole Only)
    Process
    Task Worker
    Dispatch Task Worker
    Task Worker
    Task Worker
    Workers Pool
    Send API request A
    Send API request B
    Query DB A
    Query DB B
    Return
    Request

    View Slide

  36. Concurrent Tasks
    • Concurrent Tasks in Octane (Swoole Only)

    View Slide

  37. Other Features in Octane
    (Only support Swoole)

    View Slide

  38. • Ticker
    • You can set a timer to execute periodic tasks
    (e.g. Report your server states every 10 seconds)
    Other Features in Octane

    View Slide

  39. • Cache & Table
    • High performance swoole table driver shared by di
    ff
    erent
    workers without external storage
    Other Features in Octane

    View Slide

  40. • Cache & Table
    • High performance swoole table driver shared by di
    ff
    erent
    workers without external storage
    Other Features in Octane

    View Slide

  41. • What will this counter number be?
    Process Communication
    1

    2
    3
    1
    4
    1

    View Slide

  42. • Each worker has its own memory space
    Process Communication
    Worker
    Memory
    Worker
    Memory
    Worker
    Memory
    Worker
    Memory

    View Slide

  43. • Use Swoole Table for Sharing Data
    Process Communication
    Worker
    Memory
    Worker
    Memory
    Worker
    Memory
    Worker
    Memory
    Swoole Table

    View Slide

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

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

    • I/O requests are blocking

    • 80% time cost on blocking I/O

    View Slide

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

    • 100 connections will be established as well

    • Scale to increase concurrency

    View Slide

  47. Coroutine in Swoole

    View Slide

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

    Swoole is awesome!
    Hello world!

    View Slide

  49. Coroutine in Swoole
    Hello Swoole

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

    View Slide

  50. Coroutine in 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.
    Hello Swoole!

    View Slide

  51. Coroutine in Swoole
    Hello Swoole!

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

    1.2.3.4

    1.2.3.4

    1.2.3.4


    View Slide

  52. Coroutine in Swoole
    • CSP Model
    1

    2

    3

    4

    5

    View Slide

  53. Does Octane Support
    Coroutine?

    View Slide

  54. • Does Octane support coroutine?
    Coroutine in Octane
    Coroutine A
    Coroutine B
    Coroutine C
    Process
    Request
    Request
    Request
    Yield & Resume

    View Slide

  55. • Does Octane support coroutine?
    Coroutine in Octane
    Coroutine A
    Coroutine B
    Coroutine C
    Process
    Request
    Request
    Request
    Yield & Resume
    ContainerA
    ContainerB
    ContainerC
    Container ???

    View Slide

  56. • Does Octane support coroutine?
    Coroutine in Octane

    View Slide

  57. Laravel’s Container
    Is Not Friendly to Coroutine

    View Slide

  58. • Swoole’s New Concurrency Mode
    Coroutine in Octane

    View Slide

  59. • Concurrent Tasks in Coroutine
    Coroutine in Octane
    Process
    Coroutine A
    Yield & Resume
    Coroutine B
    Coroutine C
    Coroutine D
    Send API request A
    Send API request B
    Query DB A
    Query DB B
    Request
    max_concurrency=1

    View Slide

  60. • Concurrent Tasks in Coroutine
    • Connection pool support for database

    • No global variable modi
    fi
    cations in coroutines

    • Only one request in one worker at the same time

    • Still not e
    ff
    ective enough unless container supports
    coroutine
    Coroutine in Octane

    View Slide

  61. • Environment
    • MacBook Air 2020

    • Core i5 1.1GHz (4 Cores)
    Benchmark

    View Slide

  62. • PHP-FPM + Nginx
    Benchmark

    View Slide

  63. • Laravel Octane
    Benchmark

    View Slide

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

    View Slide

  65. Q&A

    View Slide