SlideShare ist ein Scribd-Unternehmen logo
1 von 65
Downloaden Sie, um offline zu lesen
@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

Weitere ähnliche Inhalte

Was ist angesagt?

An Overview of Deserialization Vulnerabilities in the Java Virtual Machine (J...
An Overview of Deserialization Vulnerabilities in the Java Virtual Machine (J...An Overview of Deserialization Vulnerabilities in the Java Virtual Machine (J...
An Overview of Deserialization Vulnerabilities in the Java Virtual Machine (J...joaomatosf_
 
Introduction to kotlin coroutines
Introduction to kotlin coroutinesIntroduction to kotlin coroutines
Introduction to kotlin coroutinesNAVER Engineering
 
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 versionsneexemil
 
Linux Binary Exploitation - Heap Exploitation
Linux Binary Exploitation - Heap Exploitation Linux Binary Exploitation - Heap Exploitation
Linux Binary Exploitation - Heap Exploitation Angel Boy
 
NGINX: Basics and Best Practices
NGINX: Basics and Best PracticesNGINX: Basics and Best Practices
NGINX: Basics and Best PracticesNGINX, Inc.
 
Load Balancing with Nginx
Load Balancing with NginxLoad Balancing with Nginx
Load Balancing with NginxMarian Marinov
 
Pwning in c++ (basic)
Pwning in c++ (basic)Pwning in c++ (basic)
Pwning in c++ (basic)Angel Boy
 
XXE Exposed: SQLi, XSS, XXE and XEE against Web Services
XXE Exposed: SQLi, XSS, XXE and XEE against Web ServicesXXE Exposed: SQLi, XSS, XXE and XEE against Web Services
XXE Exposed: SQLi, XSS, XXE and XEE against Web ServicesAbraham Aranguren
 
Introduction to Nginx
Introduction to NginxIntroduction to Nginx
Introduction to NginxKnoldus Inc.
 
Launch the First Process in Linux System
Launch the First Process in Linux SystemLaunch the First Process in Linux System
Launch the First Process in Linux SystemJian-Hong Pan
 
JavaScript Engines and Event Loop
JavaScript Engines and Event Loop JavaScript Engines and Event Loop
JavaScript Engines and Event Loop Tapan B.K.
 
NGINX: High Performance Load Balancing
NGINX: High Performance Load BalancingNGINX: High Performance Load Balancing
NGINX: High Performance Load BalancingNGINX, Inc.
 
Linux Binary Exploitation - Return-oritend Programing
Linux Binary Exploitation - Return-oritend ProgramingLinux Binary Exploitation - Return-oritend Programing
Linux Binary Exploitation - Return-oritend ProgramingAngel Boy
 
OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout
OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and FanoutOpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout
OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and FanoutSaju Madhavan
 
[OPD 2019] Attacking JWT tokens
[OPD 2019] Attacking JWT tokens[OPD 2019] Attacking JWT tokens
[OPD 2019] Attacking JWT tokensOWASP
 
Europython 2011 - Playing tasks with Django & Celery
Europython 2011 - Playing tasks with Django & CeleryEuropython 2011 - Playing tasks with Django & Celery
Europython 2011 - Playing tasks with Django & CeleryMauro Rocco
 

Was ist angesagt? (20)

An Overview of Deserialization Vulnerabilities in the Java Virtual Machine (J...
An Overview of Deserialization Vulnerabilities in the Java Virtual Machine (J...An Overview of Deserialization Vulnerabilities in the Java Virtual Machine (J...
An Overview of Deserialization Vulnerabilities in the Java Virtual Machine (J...
 
Introduction to kotlin coroutines
Introduction to kotlin coroutinesIntroduction to kotlin coroutines
Introduction to kotlin coroutines
 
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
 
Linux Binary Exploitation - Heap Exploitation
Linux Binary Exploitation - Heap Exploitation Linux Binary Exploitation - Heap Exploitation
Linux Binary Exploitation - Heap Exploitation
 
NGINX: Basics and Best Practices
NGINX: Basics and Best PracticesNGINX: Basics and Best Practices
NGINX: Basics and Best Practices
 
Load Balancing with Nginx
Load Balancing with NginxLoad Balancing with Nginx
Load Balancing with Nginx
 
Pwning in c++ (basic)
Pwning in c++ (basic)Pwning in c++ (basic)
Pwning in c++ (basic)
 
Iocp advanced
Iocp advancedIocp advanced
Iocp advanced
 
XXE Exposed: SQLi, XSS, XXE and XEE against Web Services
XXE Exposed: SQLi, XSS, XXE and XEE against Web ServicesXXE Exposed: SQLi, XSS, XXE and XEE against Web Services
XXE Exposed: SQLi, XSS, XXE and XEE against Web Services
 
Introduction to Nginx
Introduction to NginxIntroduction to Nginx
Introduction to Nginx
 
Launch the First Process in Linux System
Launch the First Process in Linux SystemLaunch the First Process in Linux System
Launch the First Process in Linux System
 
Swoole Love PHP
Swoole Love PHPSwoole Love PHP
Swoole Love PHP
 
JavaScript Engines and Event Loop
JavaScript Engines and Event Loop JavaScript Engines and Event Loop
JavaScript Engines and Event Loop
 
NGINX: High Performance Load Balancing
NGINX: High Performance Load BalancingNGINX: High Performance Load Balancing
NGINX: High Performance Load Balancing
 
Nginx
NginxNginx
Nginx
 
Linux Binary Exploitation - Return-oritend Programing
Linux Binary Exploitation - Return-oritend ProgramingLinux Binary Exploitation - Return-oritend Programing
Linux Binary Exploitation - Return-oritend Programing
 
OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout
OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and FanoutOpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout
OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout
 
The Internals of "Hello World" Program
The Internals of "Hello World" ProgramThe Internals of "Hello World" Program
The Internals of "Hello World" Program
 
[OPD 2019] Attacking JWT tokens
[OPD 2019] Attacking JWT tokens[OPD 2019] Attacking JWT tokens
[OPD 2019] Attacking JWT tokens
 
Europython 2011 - Playing tasks with Django & Celery
Europython 2011 - Playing tasks with Django & CeleryEuropython 2011 - Playing tasks with Django & Celery
Europython 2011 - Playing tasks with Django & Celery
 

Ähnlich wie 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 APIAurimas Niekis
 
Load Balancing
Load BalancingLoad Balancing
Load Balancingoptalink
 
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 PuppetEdwin 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.KeyWork
 
Api fundamentals
Api fundamentalsApi fundamentals
Api fundamentalsAgileDenver
 
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 ChefChef Software, Inc.
 
Ratpack Web Framework
Ratpack Web FrameworkRatpack Web Framework
Ratpack Web FrameworkDaniel 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 CoroutinesLauren Yew
 
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 DashboardContinuent
 
Api FUNdamentals #MHA2017
Api FUNdamentals #MHA2017Api FUNdamentals #MHA2017
Api FUNdamentals #MHA2017JoEllen 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 2015Pavel Bucek
 
A Brief History of OWIN
A Brief History of OWINA Brief History of OWIN
A Brief History of OWINRyan Riley
 
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHPNortheast PHP - High Performance PHP
Northeast PHP - High Performance PHPJonathan Klein
 

Ähnlich wie 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
 

Mehr von LiviaLiaoFontech

Mehr von LiviaLiaoFontech (10)

2021laravelconftwslides12
2021laravelconftwslides122021laravelconftwslides12
2021laravelconftwslides12
 
2021laravelconftwslides11
2021laravelconftwslides112021laravelconftwslides11
2021laravelconftwslides11
 
2021laravelconftwslides10
2021laravelconftwslides102021laravelconftwslides10
2021laravelconftwslides10
 
2021laravelconftwslides9
2021laravelconftwslides92021laravelconftwslides9
2021laravelconftwslides9
 
2021laravelconftwslides8
2021laravelconftwslides82021laravelconftwslides8
2021laravelconftwslides8
 
2021laravelconftwslides6
2021laravelconftwslides62021laravelconftwslides6
2021laravelconftwslides6
 
2021laravelconftwslides4
2021laravelconftwslides42021laravelconftwslides4
2021laravelconftwslides4
 
2021.laravelconf.tw.slides5
2021.laravelconf.tw.slides52021.laravelconf.tw.slides5
2021.laravelconf.tw.slides5
 
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
 

Kürzlich hochgeladen

Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 

Kürzlich hochgeladen (20)

Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 

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