SlideShare a Scribd company logo
@LaravelConf Taiwan 2021
Deep Dive into Laravel Octane
Albert Chen
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"

]

}

}
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
Slido
Join at 

slido.com
#257351
Slido
Join at 

slido.com
#257351
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?
Laravel’s Service Container
$resolved = [];
$aliases = [];
$bindings = [];
$instances = [];
$serviceProviders = [];
$loadedProviders = [];
Container
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
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
Containers in Octane
• Container Dependencies
Http Kernel
App
Router
Container
Routes Collection
Route
Container
Route
Container
Containers in Octane
• Setup Sandbox Container
Clone
Initial
Container
Sandbox
Reset States
Replace Container
Register Other Services
Handle Request
• Container dependencies in singleton
Reminders in Octane
• Container dependencies in singleton
Reminders in Octane
• Container dependencies in singleton
Reminders in Octane
Concurrent Tasks
• 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
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
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
Concurrent Tasks
• Concurrent Tasks in Octane (Swoole Only)
Other Features in Octane
(Only support Swoole)
• Ticker
• You can set a timer to execute periodic tasks
(e.g. Report your server states every 10 seconds)
Other Features in Octane
• Cache & Table
• High performance swoole table driver shared by di
ff
erent
workers without external storage
Other Features in Octane
• Cache & Table
• High performance swoole table driver shared by di
ff
erent
workers without external storage
Other Features in Octane
• What will this counter number be?
Process Communication
1

2
3
1
4
1
• Each worker has its own memory space
Process Communication
Worker
Memory
Worker
Memory
Worker
Memory
Worker
Memory
• Use Swoole Table for Sharing Data
Process Communication
Worker
Memory
Worker
Memory
Worker
Memory
Worker
Memory
Swoole Table
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
Blocking I/O in PHP
• Resource can't be reused

• I/O requests are blocking

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

• 100 connections will be established as well

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

Swoole is awesome!
Hello world!
Coroutine in Swoole
Hello Swoole

• Non-Blocking I/Os are scheduled by coroutine automatically.
1.2.3.4
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!
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

…
Coroutine in Swoole
• CSP Model
1

2

3

4

5
Does Octane Support
Coroutine?
• Does Octane support coroutine?
Coroutine in Octane
Coroutine A
Coroutine B
Coroutine C
Process
Request
Request
Request
Yield & Resume
• Does Octane support coroutine?
Coroutine in Octane
Coroutine A
Coroutine B
Coroutine C
Process
Request
Request
Request
Yield & Resume
ContainerA
ContainerB
ContainerC
Container ???
• Does Octane support coroutine?
Coroutine in Octane
Laravel’s Container
Is Not Friendly to Coroutine
• Swoole’s New Concurrency Mode
Coroutine in Octane
• 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
• 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
• Environment
• MacBook Air 2020

• Core i5 1.1GHz (4 Cores)
Benchmark
• PHP-FPM + Nginx
Benchmark
• Laravel Octane
Benchmark
Some Facts about Swoole
• There’s knowledge gap for traditional PHP developers.
Q&A

More Related Content

PPTX
Laravel ppt
PPTX
A non-technical introduction to ChatGPT - SEDA.pptx
PPTX
Diabetes Mellitus
PPTX
Hypertension
PPTX
Republic Act No. 11313 Safe Spaces Act (Bawal Bastos Law).pptx
PPTX
Power Point Presentation on Artificial Intelligence
PDF
Caça palavras - Bullying
Laravel ppt
A non-technical introduction to ChatGPT - SEDA.pptx
Diabetes Mellitus
Hypertension
Republic Act No. 11313 Safe Spaces Act (Bawal Bastos Law).pptx
Power Point Presentation on Artificial Intelligence
Caça palavras - Bullying

What's hot (20)

PDF
From Generator to Fiber the Road to Coroutine in PHP
PDF
The Integration of Laravel with Swoole
PDF
[COSCUP 2022] 讓黑畫面再次偉大 - 用 PHP 寫 CLI 工具
PDF
How to Build a High Performance Application with PHP and Swoole?
PPTX
Attacking thru HTTP Host header
PDF
PDF
Postman: An Introduction for Developers
PDF
[NDC18] 만들고 붓고 부수고 - 〈야생의 땅: 듀랑고〉 서버 관리 배포 이야기
PDF
[若渴計畫] Challenges and Solutions of Window Remote Shellcode
PPTX
RESTful API Testing using Postman, Newman, and Jenkins
PDF
Postman Webinar: Postman 101
PDF
웹서버 부하테스트 실전 노하우
PPTX
Postman Introduction
PPTX
Postman Collection Format v2.0 (pre-draft)
PDF
API for Beginners
PPTX
Intro to Node.js (v1)
PPSX
Php and MySQL
PPTX
RESTful API - Best Practices
PDF
POST/CON 2019 Workshop: Testing, Automated Testing, and Reporting APIs with P...
PPTX
Python/Flask Presentation
From Generator to Fiber the Road to Coroutine in PHP
The Integration of Laravel with Swoole
[COSCUP 2022] 讓黑畫面再次偉大 - 用 PHP 寫 CLI 工具
How to Build a High Performance Application with PHP and Swoole?
Attacking thru HTTP Host header
Postman: An Introduction for Developers
[NDC18] 만들고 붓고 부수고 - 〈야생의 땅: 듀랑고〉 서버 관리 배포 이야기
[若渴計畫] Challenges and Solutions of Window Remote Shellcode
RESTful API Testing using Postman, Newman, and Jenkins
Postman Webinar: Postman 101
웹서버 부하테스트 실전 노하우
Postman Introduction
Postman Collection Format v2.0 (pre-draft)
API for Beginners
Intro to Node.js (v1)
Php and MySQL
RESTful API - Best Practices
POST/CON 2019 Workshop: Testing, Automated Testing, and Reporting APIs with P...
Python/Flask Presentation
Ad

Similar to 2021.laravelconf.tw.slides1 (20)

PDF
How Swoole Blows Up your Mind about PHP?
PPTX
How to Supercharge your PHP Web API
PPTX
Load Balancing
PPTX
Provisioning Oracle Fusion Middleware Environments with Chef and Puppet
KEY
Real time system_performance_mon
ZIP
Intro To Puppet.Key
PDF
Api fundamentals
PDF
Push jobs: an orchestration building block for private Chef
PDF
Ratpack Web Framework
PDF
The Anatomy of Failure - Lessons from running systems to serve millions of pe...
PDF
Threading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
PDF
Intro to CakePHP
PDF
Virtual Flink Forward 2020: How Streaming Helps Your Staging Environment and ...
PDF
Training Slides: 205 - Installing and Configuring Tungsten Dashboard
PPTX
Lattice yapc-slideshare
PDF
Api FUNdamentals #MHA2017
PDF
WTF is Twisted?
PDF
WebSocket in Enterprise Applications 2015
PPTX
C++ scalable network_io
PPTX
A Brief History of OWIN
How Swoole Blows Up your Mind about PHP?
How to Supercharge your PHP Web API
Load Balancing
Provisioning Oracle Fusion Middleware Environments with Chef and Puppet
Real time system_performance_mon
Intro To Puppet.Key
Api fundamentals
Push jobs: an orchestration building block for private Chef
Ratpack Web Framework
The Anatomy of Failure - Lessons from running systems to serve millions of pe...
Threading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
Intro to CakePHP
Virtual Flink Forward 2020: How Streaming Helps Your Staging Environment and ...
Training Slides: 205 - Installing and Configuring Tungsten Dashboard
Lattice yapc-slideshare
Api FUNdamentals #MHA2017
WTF is Twisted?
WebSocket in Enterprise Applications 2015
C++ scalable network_io
A Brief History of OWIN
Ad

More from LiviaLiaoFontech (10)

PDF
2021laravelconftwslides12
PDF
2021laravelconftwslides11
PDF
2021laravelconftwslides10
PDF
2021laravelconftwslides9
PDF
2021laravelconftwslides8
PDF
2021laravelconftwslides6
PDF
2021laravelconftwslides4
PDF
2021.laravelconf.tw.slides5
PDF
2021.laravelconf.tw.slides3
PDF
2021.laravelconf.tw.slides2
2021laravelconftwslides12
2021laravelconftwslides11
2021laravelconftwslides10
2021laravelconftwslides9
2021laravelconftwslides8
2021laravelconftwslides6
2021laravelconftwslides4
2021.laravelconf.tw.slides5
2021.laravelconf.tw.slides3
2021.laravelconf.tw.slides2

Recently uploaded (20)

PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
KodekX | Application Modernization Development
PDF
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Big Data Technologies - Introduction.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PDF
Electronic commerce courselecture one. Pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
GamePlan Trading System Review: Professional Trader's Honest Take
KodekX | Application Modernization Development
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
cuic standard and advanced reporting.pdf
NewMind AI Monthly Chronicles - July 2025
Big Data Technologies - Introduction.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
NewMind AI Weekly Chronicles - August'25 Week I
Machine learning based COVID-19 study performance prediction
Understanding_Digital_Forensics_Presentation.pptx
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Electronic commerce courselecture one. Pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Advanced methodologies resolving dimensionality complications for autism neur...
The AUB Centre for AI in Media Proposal.docx
MYSQL Presentation for SQL database connectivity
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

2021.laravelconf.tw.slides1

  • 1. @LaravelConf Taiwan 2021 Deep Dive into Laravel Octane Albert Chen
  • 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" ] } }
  • 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
  • 6. Why PHP Performs Poorly in High Concurrency?
  • 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
  • 15. What is RoadRunner? (Process Manager) Request HTTP Handler Load Balancer PSR-7 
 Consumer Workers Pool RoadRunner
  • 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
  • 18. Lifecycle in Octane Reset Prepare Sandbox Request WarmUp Middleware Dispatch by Router Routes Match Controller Response Terminate 
 Middleware Run only once
  • 19. PHP Lifecycle in Octane Run only once!
  • 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.
  • 21. • Potential Memory Leaks Reminders in Long-Lived PHP
  • 22. • Don’t exit in your PHP code Reminders in Long-Lived PHP
  • 24. Laravel’s Service Container $resolved = []; $aliases = []; $bindings = []; $instances = []; $serviceProviders = []; $loadedProviders = []; Container
  • 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
  • 27. Containers in Octane • Container Dependencies Http Kernel App Router Container Routes Collection Route Container Route Container
  • 28. Containers in Octane • Setup Sandbox Container Clone Initial Container Sandbox Reset States Replace Container Register Other Services Handle Request
  • 29. • Container dependencies in singleton Reminders in Octane
  • 30. • Container dependencies in singleton Reminders in Octane
  • 31. • Container dependencies in singleton Reminders in Octane
  • 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
  • 36. Concurrent Tasks • Concurrent Tasks in Octane (Swoole Only)
  • 37. Other Features in Octane (Only support Swoole)
  • 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
  • 48. Coroutine in Swoole • Non-Blocking I/Os are scheduled by coroutine automatically. PHP is the best Swoole is awesome! Hello world!
  • 49. Coroutine in Swoole Hello Swoole • Non-Blocking I/Os are scheduled by coroutine automatically. 1.2.3.4
  • 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!
  • 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 …
  • 52. Coroutine in Swoole • CSP Model 1 2 3 4 5
  • 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
  • 57. Laravel’s Container Is Not Friendly to Coroutine
  • 58. • Swoole’s New Concurrency Mode 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
  • 61. • Environment • MacBook Air 2020 • Core i5 1.1GHz (4 Cores) Benchmark
  • 62. • PHP-FPM + Nginx Benchmark
  • 64. Some Facts about Swoole • There’s knowledge gap for traditional PHP developers.
  • 65. Q&A