Deep Dive into Laravel Octane 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 Why PHP Performs Poorly in High Concurrency? Lifecycle in PHP 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 How many files are required for one request in Laravel? 426 get_included_files() In Laravel 8.52 Stateless PHP • How do we serve PHP today? • PHP-FPM • mod_php for Apache • Both patterns are all stateless 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 Laravel Octane Laravel Octane • Published in April 2021 • Maintained by o ffi 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 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 What is RoadRunner? (Process Manager) Request HTTP Handler Load Balancer PSR-7 Consumer Workers Pool RoadRunner 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 Server Structure in Swoole Lifecycle in Octane Reset Prepare Sandbox Request WarmUp Middleware Dispatch by Router Routes Match Controller Response Terminate Middleware Run only once PHP Lifecycle in Octane Run only once! 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. • Potential Memory Leaks Reminders in Long-Lived PHP • Don’t exit in your PHP code Reminders in Long-Lived PHP Static variables in Laravel?