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. 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" ] } }
  2. 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
  3. 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
  4. How many files are required for one request in Laravel?

    426 get_included_files() In Laravel 8.52
  5. Stateless PHP • How do we serve PHP today? •

    PHP-FPM • mod_php for Apache
 • Both patterns are all stateless
  6. 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
  7. 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
  8. 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
  9. 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
  10. Lifecycle in Octane Reset Prepare Sandbox Request WarmUp Middleware Dispatch

    by Router Routes Match Controller Response Terminate 
 Middleware Run only once
  11. 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.
  12. Laravel’s Service Container $resolved = []; $aliases = []; $bindings

    = []; $instances = []; $serviceProviders = []; $loadedProviders = []; Container
  13. 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
  14. 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
  15. Containers in Octane • Container Dependencies Http Kernel App Router

    Container Routes Collection Route Container Route Container
  16. Containers in Octane • Setup Sandbox Container Clone Initial Container

    Sandbox Reset States Replace Container Register Other Services Handle Request
  17. • 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
  18. 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
  19. 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
  20. • Ticker • You can set a timer to execute

    periodic tasks (e.g. Report your server states every 10 seconds) Other Features in Octane
  21. • Cache & Table • High performance swoole table driver

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

    shared by di ff erent workers without external storage Other Features in Octane
  23. • Each worker has its own memory space Process Communication

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

    Memory Worker Memory Worker Memory Worker Memory Swoole Table
  25. 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
  26. Blocking I/O in PHP • Resource can't be reused •

    I/O requests are blocking • 80% time cost on blocking I/O
  27. Concurrency Problem • 100 requests at the same time need

    100 processes • 100 connections will be established as well • Scale to increase concurrency
  28. Coroutine in Swoole • Non-Blocking I/Os are scheduled by coroutine

    automatically. PHP is the best Swoole is awesome! Hello world!
  29. 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!
  30. 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 …
  31. • Does Octane support coroutine? Coroutine in Octane Coroutine A

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

    Coroutine B Coroutine C Process Request Request Request Yield & Resume ContainerA ContainerB ContainerC Container ???
  33. • 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
  34. • 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
  35. Q&A