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

What's hot

HA Deployment Architecture with HAProxy and Keepalived
HA Deployment Architecture with HAProxy and KeepalivedHA Deployment Architecture with HAProxy and Keepalived
HA Deployment Architecture with HAProxy and Keepalived
Ganapathi Kandaswamy
 
Laravel Introduction
Laravel IntroductionLaravel Introduction
Laravel Introduction
Ahmad Shah Hafizan Hamidin
 
ZgPHP 97 - Microservice architecture in Laravel
ZgPHP 97 - Microservice architecture in LaravelZgPHP 97 - Microservice architecture in Laravel
ZgPHP 97 - Microservice architecture in Laravel
Frano Šašvari
 
REST API and CRUD
REST API and CRUDREST API and CRUD
REST API and CRUD
Prem Sanil
 
REST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in CodeigniterREST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in Codeigniter
Sachin G Kulkarni
 
Dataflow with Apache NiFi
Dataflow with Apache NiFiDataflow with Apache NiFi
Dataflow with Apache NiFi
DataWorks Summit/Hadoop Summit
 
Where is my bottleneck? Performance troubleshooting in Flink
Where is my bottleneck? Performance troubleshooting in FlinkWhere is my bottleneck? Performance troubleshooting in Flink
Where is my bottleneck? Performance troubleshooting in Flink
Flink Forward
 
Chef for DevOps - an Introduction
Chef for DevOps - an IntroductionChef for DevOps - an Introduction
Chef for DevOps - an Introduction
Sanjeev Sharma
 
Why HATEOAS
Why HATEOASWhy HATEOAS
Why HATEOAS
Lee Wayne
 
Introduction to laravel framework
Introduction to laravel frameworkIntroduction to laravel framework
Introduction to laravel framework
Ahmad Fatoni
 
Understanding Reactive Programming
Understanding Reactive ProgrammingUnderstanding Reactive Programming
Understanding Reactive Programming
Andres Almiray
 
HTTP Request Smuggling via higher HTTP versions
HTTP Request Smuggling via higher HTTP versionsHTTP Request Smuggling via higher HTTP versions
HTTP Request Smuggling via higher HTTP versions
neexemil
 
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js + Expres...
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js +  Expres...Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js +  Expres...
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js + Expres...
Edureka!
 
REST API
REST APIREST API
REST API
Tofazzal Ahmed
 
Load Balancing with HAproxy
Load Balancing with HAproxyLoad Balancing with HAproxy
Load Balancing with HAproxy
Brendan Jennings
 
Introduction to ansible
Introduction to ansibleIntroduction to ansible
Introduction to ansible
Omid Vahdaty
 
Tuning TCP and NGINX on EC2
Tuning TCP and NGINX on EC2Tuning TCP and NGINX on EC2
Tuning TCP and NGINX on EC2
Chartbeat
 
Apache NiFi Crash Course Intro
Apache NiFi Crash Course IntroApache NiFi Crash Course Intro
Apache NiFi Crash Course Intro
DataWorks Summit/Hadoop Summit
 
Automating with Ansible
Automating with AnsibleAutomating with Ansible
Automating with Ansible
Ricardo Schmidt
 
Stephan Ewen - Experiences running Flink at Very Large Scale
Stephan Ewen -  Experiences running Flink at Very Large ScaleStephan Ewen -  Experiences running Flink at Very Large Scale
Stephan Ewen - Experiences running Flink at Very Large Scale
Ververica
 

What's hot (20)

HA Deployment Architecture with HAProxy and Keepalived
HA Deployment Architecture with HAProxy and KeepalivedHA Deployment Architecture with HAProxy and Keepalived
HA Deployment Architecture with HAProxy and Keepalived
 
Laravel Introduction
Laravel IntroductionLaravel Introduction
Laravel Introduction
 
ZgPHP 97 - Microservice architecture in Laravel
ZgPHP 97 - Microservice architecture in LaravelZgPHP 97 - Microservice architecture in Laravel
ZgPHP 97 - Microservice architecture in Laravel
 
REST API and CRUD
REST API and CRUDREST API and CRUD
REST API and CRUD
 
REST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in CodeigniterREST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in Codeigniter
 
Dataflow with Apache NiFi
Dataflow with Apache NiFiDataflow with Apache NiFi
Dataflow with Apache NiFi
 
Where is my bottleneck? Performance troubleshooting in Flink
Where is my bottleneck? Performance troubleshooting in FlinkWhere is my bottleneck? Performance troubleshooting in Flink
Where is my bottleneck? Performance troubleshooting in Flink
 
Chef for DevOps - an Introduction
Chef for DevOps - an IntroductionChef for DevOps - an Introduction
Chef for DevOps - an Introduction
 
Why HATEOAS
Why HATEOASWhy HATEOAS
Why HATEOAS
 
Introduction to laravel framework
Introduction to laravel frameworkIntroduction to laravel framework
Introduction to laravel framework
 
Understanding Reactive Programming
Understanding Reactive ProgrammingUnderstanding Reactive Programming
Understanding Reactive Programming
 
HTTP Request Smuggling via higher HTTP versions
HTTP Request Smuggling via higher HTTP versionsHTTP Request Smuggling via higher HTTP versions
HTTP Request Smuggling via higher HTTP versions
 
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js + Expres...
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js +  Expres...Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js +  Expres...
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js + Expres...
 
REST API
REST APIREST API
REST API
 
Load Balancing with HAproxy
Load Balancing with HAproxyLoad Balancing with HAproxy
Load Balancing with HAproxy
 
Introduction to ansible
Introduction to ansibleIntroduction to ansible
Introduction to ansible
 
Tuning TCP and NGINX on EC2
Tuning TCP and NGINX on EC2Tuning TCP and NGINX on EC2
Tuning TCP and NGINX on EC2
 
Apache NiFi Crash Course Intro
Apache NiFi Crash Course IntroApache NiFi Crash Course Intro
Apache NiFi Crash Course Intro
 
Automating with Ansible
Automating with AnsibleAutomating with Ansible
Automating with Ansible
 
Stephan Ewen - Experiences running Flink at Very Large Scale
Stephan Ewen -  Experiences running Flink at Very Large ScaleStephan Ewen -  Experiences running Flink at Very Large Scale
Stephan Ewen - Experiences running Flink at Very Large Scale
 

Similar to 2021.laravelconf.tw.slides1

How Swoole Blows Up your Mind about PHP?
How Swoole Blows Up your Mind about PHP?How Swoole Blows Up your Mind about PHP?
How Swoole Blows Up your Mind about PHP?
Albert Chen
 
How to Supercharge your PHP Web API
How to Supercharge your PHP Web APIHow to Supercharge your PHP Web API
How to Supercharge your PHP Web API
Aurimas Niekis
 
Load Balancing
Load BalancingLoad Balancing
Load Balancing
optalink
 
Provisioning Oracle Fusion Middleware Environments with Chef and Puppet
Provisioning Oracle Fusion Middleware Environments with Chef and PuppetProvisioning Oracle Fusion Middleware Environments with Chef and Puppet
Provisioning Oracle Fusion Middleware Environments with Chef and Puppet
Edwin Biemond
 
Real time system_performance_mon
Real time system_performance_monReal time system_performance_mon
Real time system_performance_monTomas Doran
 
Intro To Puppet.Key
Intro To Puppet.KeyIntro To Puppet.Key
Intro To Puppet.Key
Work
 
Api fundamentals
Api fundamentalsApi fundamentals
Api fundamentals
AgileDenver
 
Push jobs: an orchestration building block for private Chef
Push jobs: an orchestration building block for private ChefPush jobs: an orchestration building block for private Chef
Push jobs: an orchestration building block for private Chef
Chef Software, Inc.
 
Ratpack Web Framework
Ratpack Web FrameworkRatpack Web Framework
Ratpack Web Framework
Daniel Woods
 
Threading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
Threading Made Easy! A Busy Developer’s Guide to Kotlin CoroutinesThreading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
Threading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
Lauren Yew
 
Intro to CakePHP
Intro to CakePHPIntro to CakePHP
Intro to CakePHP
Walther Lalk
 
Virtual Flink Forward 2020: How Streaming Helps Your Staging Environment and ...
Virtual Flink Forward 2020: How Streaming Helps Your Staging Environment and ...Virtual Flink Forward 2020: How Streaming Helps Your Staging Environment and ...
Virtual Flink Forward 2020: How Streaming Helps Your Staging Environment and ...
Flink Forward
 
Training Slides: 205 - Installing and Configuring Tungsten Dashboard
Training Slides: 205 - Installing and Configuring Tungsten DashboardTraining Slides: 205 - Installing and Configuring Tungsten Dashboard
Training Slides: 205 - Installing and Configuring Tungsten Dashboard
Continuent
 
Lattice yapc-slideshare
Lattice yapc-slideshareLattice yapc-slideshare
Lattice yapc-slideshare
Gwenn Etourneau
 
Api FUNdamentals #MHA2017
Api FUNdamentals #MHA2017Api FUNdamentals #MHA2017
Api FUNdamentals #MHA2017
JoEllen Carter
 
WTF is Twisted?
WTF is Twisted?WTF is Twisted?
WTF is Twisted?
hawkowl
 
WebSocket in Enterprise Applications 2015
WebSocket in Enterprise Applications 2015WebSocket in Enterprise Applications 2015
WebSocket in Enterprise Applications 2015
Pavel Bucek
 
C++ scalable network_io
C++ scalable network_ioC++ scalable network_io
C++ scalable network_io
Arindam Mukherjee
 
A Brief History of OWIN
A Brief History of OWINA Brief History of OWIN
A Brief History of OWIN
Ryan Riley
 
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHPNortheast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Jonathan Klein
 

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

How Swoole Blows Up your Mind about PHP?
How Swoole Blows Up your Mind about PHP?How Swoole Blows Up your Mind about PHP?
How Swoole Blows Up your Mind about PHP?
 
How to Supercharge your PHP Web API
How to Supercharge your PHP Web APIHow to Supercharge your PHP Web API
How to Supercharge your PHP Web API
 
Load Balancing
Load BalancingLoad Balancing
Load Balancing
 
Provisioning Oracle Fusion Middleware Environments with Chef and Puppet
Provisioning Oracle Fusion Middleware Environments with Chef and PuppetProvisioning Oracle Fusion Middleware Environments with Chef and Puppet
Provisioning Oracle Fusion Middleware Environments with Chef and Puppet
 
Real time system_performance_mon
Real time system_performance_monReal time system_performance_mon
Real time system_performance_mon
 
Intro To Puppet.Key
Intro To Puppet.KeyIntro To Puppet.Key
Intro To Puppet.Key
 
Api fundamentals
Api fundamentalsApi fundamentals
Api fundamentals
 
Push jobs: an orchestration building block for private Chef
Push jobs: an orchestration building block for private ChefPush jobs: an orchestration building block for private Chef
Push jobs: an orchestration building block for private Chef
 
Ratpack Web Framework
Ratpack Web FrameworkRatpack Web Framework
Ratpack Web Framework
 
Threading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
Threading Made Easy! A Busy Developer’s Guide to Kotlin CoroutinesThreading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
Threading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
 
Intro to CakePHP
Intro to CakePHPIntro to CakePHP
Intro to CakePHP
 
Virtual Flink Forward 2020: How Streaming Helps Your Staging Environment and ...
Virtual Flink Forward 2020: How Streaming Helps Your Staging Environment and ...Virtual Flink Forward 2020: How Streaming Helps Your Staging Environment and ...
Virtual Flink Forward 2020: How Streaming Helps Your Staging Environment and ...
 
Training Slides: 205 - Installing and Configuring Tungsten Dashboard
Training Slides: 205 - Installing and Configuring Tungsten DashboardTraining Slides: 205 - Installing and Configuring Tungsten Dashboard
Training Slides: 205 - Installing and Configuring Tungsten Dashboard
 
Lattice yapc-slideshare
Lattice yapc-slideshareLattice yapc-slideshare
Lattice yapc-slideshare
 
Api FUNdamentals #MHA2017
Api FUNdamentals #MHA2017Api FUNdamentals #MHA2017
Api FUNdamentals #MHA2017
 
WTF is Twisted?
WTF is Twisted?WTF is Twisted?
WTF is Twisted?
 
WebSocket in Enterprise Applications 2015
WebSocket in Enterprise Applications 2015WebSocket in Enterprise Applications 2015
WebSocket in Enterprise Applications 2015
 
C++ scalable network_io
C++ scalable network_ioC++ scalable network_io
C++ scalable network_io
 
A Brief History of OWIN
A Brief History of OWINA Brief History of OWIN
A Brief History of OWIN
 
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHPNortheast PHP - High Performance PHP
Northeast PHP - High Performance PHP
 

More from LiviaLiaoFontech

2021laravelconftwslides12
2021laravelconftwslides122021laravelconftwslides12
2021laravelconftwslides12
LiviaLiaoFontech
 
2021laravelconftwslides11
2021laravelconftwslides112021laravelconftwslides11
2021laravelconftwslides11
LiviaLiaoFontech
 
2021laravelconftwslides10
2021laravelconftwslides102021laravelconftwslides10
2021laravelconftwslides10
LiviaLiaoFontech
 
2021laravelconftwslides9
2021laravelconftwslides92021laravelconftwslides9
2021laravelconftwslides9
LiviaLiaoFontech
 
2021laravelconftwslides8
2021laravelconftwslides82021laravelconftwslides8
2021laravelconftwslides8
LiviaLiaoFontech
 
2021laravelconftwslides6
2021laravelconftwslides62021laravelconftwslides6
2021laravelconftwslides6
LiviaLiaoFontech
 
2021laravelconftwslides4
2021laravelconftwslides42021laravelconftwslides4
2021laravelconftwslides4
LiviaLiaoFontech
 
2021.laravelconf.tw.slides3
2021.laravelconf.tw.slides32021.laravelconf.tw.slides3
2021.laravelconf.tw.slides3
LiviaLiaoFontech
 
2021.laravelconf.tw.slides2
2021.laravelconf.tw.slides22021.laravelconf.tw.slides2
2021.laravelconf.tw.slides2
LiviaLiaoFontech
 

More from LiviaLiaoFontech (9)

2021laravelconftwslides12
2021laravelconftwslides122021laravelconftwslides12
2021laravelconftwslides12
 
2021laravelconftwslides11
2021laravelconftwslides112021laravelconftwslides11
2021laravelconftwslides11
 
2021laravelconftwslides10
2021laravelconftwslides102021laravelconftwslides10
2021laravelconftwslides10
 
2021laravelconftwslides9
2021laravelconftwslides92021laravelconftwslides9
2021laravelconftwslides9
 
2021laravelconftwslides8
2021laravelconftwslides82021laravelconftwslides8
2021laravelconftwslides8
 
2021laravelconftwslides6
2021laravelconftwslides62021laravelconftwslides6
2021laravelconftwslides6
 
2021laravelconftwslides4
2021laravelconftwslides42021laravelconftwslides4
2021laravelconftwslides4
 
2021.laravelconf.tw.slides3
2021.laravelconf.tw.slides32021.laravelconf.tw.slides3
2021.laravelconf.tw.slides3
 
2021.laravelconf.tw.slides2
2021.laravelconf.tw.slides22021.laravelconf.tw.slides2
2021.laravelconf.tw.slides2
 

Recently uploaded

Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 

Recently uploaded (20)

Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 

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