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
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
9. How many files are required for one
request in Laravel?
426
get_included_files()
In Laravel 8.52
10. Stateless PHP
• How do we serve PHP today?
• PHP-FPM
• mod_php for Apache
• Both patterns are all stateless
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
13. 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
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
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
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.
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
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
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
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
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
38. • Ticker
• You can set a timer to execute periodic tasks
(e.g. Report your server states every 10 seconds)
Other Features in Octane
39. • Cache & Table
• High performance swoole table driver shared by di
ff
erent
workers without external storage
Other Features in Octane
40. • Cache & Table
• High performance swoole table driver shared by di
ff
erent
workers without external storage
Other Features in Octane
41. • What will this counter number be?
Process Communication
1
2
3
1
4
1
42. • Each worker has its own memory space
Process Communication
Worker
Memory
Worker
Memory
Worker
Memory
Worker
Memory
43. • Use Swoole Table for Sharing Data
Process Communication
Worker
Memory
Worker
Memory
Worker
Memory
Worker
Memory
Swoole Table
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
45. Blocking I/O in PHP
• Resource can't be reused
• I/O requests are blocking
• 80% time cost on blocking I/O
46. Concurrency Problem
• 100 requests at the same time need 100 processes
• 100 connections will be established as well
• Scale to increase concurrency
54. • Does Octane support coroutine?
Coroutine in Octane
Coroutine A
Coroutine B
Coroutine C
Process
Request
Request
Request
Yield & Resume
55. • Does Octane support coroutine?
Coroutine in Octane
Coroutine A
Coroutine B
Coroutine C
Process
Request
Request
Request
Yield & Resume
ContainerA
ContainerB
ContainerC
Container ???
56. • Does Octane support coroutine?
Coroutine in Octane
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
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