Slide 1

Slide 1 text

@LaravelConf Taiwan 2021 Deep Dive into Laravel Octane Albert Chen

Slide 2

Slide 2 text

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" ] } }

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Slido Join at slido.com #257351

Slide 5

Slide 5 text

Slido Join at slido.com #257351

Slide 6

Slide 6 text

Why PHP Performs Poorly in High Concurrency?

Slide 7

Slide 7 text

Lifecycle in PHP

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

How many files are required for one request in Laravel? 426 get_included_files() In Laravel 8.52

Slide 10

Slide 10 text

Stateless PHP • How do we serve PHP today? • PHP-FPM • mod_php for Apache
 • Both patterns are all stateless

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Laravel Octane

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

What is RoadRunner? (Process Manager) Request HTTP Handler Load Balancer PSR-7 
 Consumer Workers Pool RoadRunner

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Server Structure in Swoole

Slide 18

Slide 18 text

Lifecycle in Octane Reset Prepare Sandbox Request WarmUp Middleware Dispatch by Router Routes Match Controller Response Terminate 
 Middleware Run only once

Slide 19

Slide 19 text

PHP Lifecycle in Octane Run only once!

Slide 20

Slide 20 text

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.

Slide 21

Slide 21 text

• Potential Memory Leaks Reminders in Long-Lived PHP

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Static variables in Laravel?

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

• Container dependencies in singleton Reminders in Octane

Slide 30

Slide 30 text

• Container dependencies in singleton Reminders in Octane

Slide 31

Slide 31 text

• Container dependencies in singleton Reminders in Octane

Slide 32

Slide 32 text

Concurrent Tasks

Slide 33

Slide 33 text

• 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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

Concurrent Tasks • Concurrent Tasks in Octane (Swoole Only)

Slide 37

Slide 37 text

Other Features in Octane (Only support Swoole)

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

• What will this counter number be? Process Communication 1 2 3 1 4 1

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

Blocking I/O in PHP • Resource can't be reused • I/O requests are blocking • 80% time cost on blocking I/O

Slide 46

Slide 46 text

Concurrency Problem • 100 requests at the same time need 100 processes • 100 connections will be established as well • Scale to increase concurrency

Slide 47

Slide 47 text

Coroutine in Swoole

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

Coroutine in Swoole Hello Swoole • Non-Blocking I/Os are scheduled by coroutine automatically. 1.2.3.4

Slide 50

Slide 50 text

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!

Slide 51

Slide 51 text

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 …

Slide 52

Slide 52 text

Coroutine in Swoole • CSP Model 1 2 3 4 5

Slide 53

Slide 53 text

Does Octane Support Coroutine?

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

• Does Octane support coroutine? Coroutine in Octane

Slide 57

Slide 57 text

Laravel’s Container Is Not Friendly to Coroutine

Slide 58

Slide 58 text

• Swoole’s New Concurrency Mode Coroutine in Octane

Slide 59

Slide 59 text

• 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

Slide 60

Slide 60 text

• 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

Slide 61

Slide 61 text

• Environment • MacBook Air 2020 • Core i5 1.1GHz (4 Cores) Benchmark

Slide 62

Slide 62 text

• PHP-FPM + Nginx Benchmark

Slide 63

Slide 63 text

• Laravel Octane Benchmark

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

Q&A