Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Scaling Laravel - Laracon.net 2018
Search
Chris Fidao
February 07, 2018
Technology
15
1.8k
Scaling Laravel - Laracon.net 2018
Chris Fidao
February 07, 2018
Tweet
Share
More Decks by Chris Fidao
See All by Chris Fidao
Development Environments that Feel Local
fideloper
0
30
Refactoring Terraform - CloudCasts - Scaling EC2
fideloper
0
53
Linux Environment
fideloper
1
10k
Server Survival
fideloper
29
23k
FileBeat (Won't save you from the JVM)
fideloper
1
300
Powering Your Applications With Nginx
fideloper
9
7.7k
Hexagonal Architecture
fideloper
49
200k
Intro to etcd
fideloper
3
540
Service Oriented Architecture with a little help from NodeJS
fideloper
4
2.3k
Other Decks in Technology
See All in Technology
How to be an AWS Community Builder | 君もAWS Community Builderになろう!〜2024 冬 CB募集直前対策編?!〜
coosuke
PRO
2
2.8k
KnowledgeBaseDocuments APIでベクトルインデックス管理を自動化する
iidaxs
1
250
生成AIをより賢く エンジニアのための RAG入門 - Oracle AI Jam Session #20
kutsushitaneko
4
210
祝!Iceberg祭開幕!re:Invent 2024データレイク関連アップデート10分総ざらい
kniino
2
230
Fanstaの1年を大解剖! 一人SREはどこまでできるのか!?
syossan27
2
160
Wantedly での Datadog 活用事例
bgpat
1
400
サーバレスアプリ開発者向けアップデートをキャッチアップしてきた #AWSreInvent #regrowth_fuk
drumnistnakano
0
190
第3回Snowflake女子会_LT登壇資料(合成データ)_Taro_CCCMK
tarotaro0129
0
180
NW-JAWS #14 re:Invent 2024(予選落ち含)で 発表された推しアップデートについて
nagisa53
0
250
WACATE2024冬セッション資料(ユーザビリティ)
scarletplover
0
190
AWS re:Invent 2024 ふりかえり
kongmingstrap
0
130
LINEヤフーのフロントエンド組織・体制の紹介【24年12月】
lycorp_recruit_jp
0
530
Featured
See All Featured
Making the Leap to Tech Lead
cromwellryan
133
9k
GitHub's CSS Performance
jonrohan
1030
460k
The Cost Of JavaScript in 2023
addyosmani
45
7k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
65k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Thoughts on Productivity
jonyablonski
67
4.4k
Into the Great Unknown - MozCon
thekraken
33
1.5k
How to Ace a Technical Interview
jacobian
276
23k
A designer walks into a library…
pauljervisheath
204
24k
Transcript
Scaling Laravel the talk!
What is Scaling? Optimization Scaling Out
• Increase requests per second • Increase speed of response
(reduce response time) • Decrease resource usage • Secondary Goal: Less Servers! What is Scaling Optimization Goal: Efficiency
• Consistent requests per second • Consistent speed of response
• Consistent despite increasing traffic What is Scaling Scaling Out Goal: Consistency
Optimization
Optimization • Eager Loading (n+1 problem) • Object Caching (decorator
pattern) • PHP-FPM (process management) • OpCache • Server Specialization (mysql optimization)
Optimization Eager Loading “n+1 problem” • Initial query returning a
Collection • + 1 Additional query per object in Collection
Optimization Eager Loading 3 Topics 6 Video Series
Optimization Eager Loading +1 queries Lazy Loading
Optimization Eager Loading +6 queries +3 queries Total Queries: 10
Optimization Eager Loading • First Query: Grab all topics (+1
query) • Foreach Topic: Grab Series (+3 queries) • Foreach Series: Grab Content (+6 queries)
Optimization Eager Loading -2 queries
Optimization Eager Loading -5 queries nested!
Optimization Object Caching Reduce DB Queries + Processing
Optimization Object Caching
Optimization Object Caching Decorators • Add behavior w/out modifying code
• Interface • Modifier(s) • Root Object (gross)
Optimization Object Caching An Interface Repository!
Optimization Object Caching Build Our Original Behavior
Optimization Object Caching Get it Working
Optimization Object Caching A cache decorator! magic!
Optimization Object Caching TopicCache::topics( TopicQuery::topics( Topic::all(); ); )
Optimization Object Caching Update Service Provider We “wrap” TopicQuery with
decorator TopicCache.
Optimization Object Caching You’re empowered to do fun things!
Optimization Review: Code Optimization “n+1 problem”: eager loading object caching:
decorators
Optimization PHP-FPM Optimizing PHP-FPM Process Management
Optimization PHP-FPM • Enable more requests per second • Trade-off
between more requests and CPU/RAM usage • Monitor your server, even if just looking at `top` or `htop` commands (preferably under load) Process Management pm.max_children
Optimization PHP-FPM Rule of Thumb (RAM - Overhead) / Memory
per Request (2048mb - 512mb) / 50mb = ~30 max processes (2048mb - 1024mb) / 50mb = ~20 max processes *check CPU usage after making changes!
Optimization PHP-FPM /etc/php/7.[1|2]/fpm/pool.d/www.conf Limits your req/sec directly (but w/ RAM/CPU
trade off) Speeds up “startup time” Keep spares around But not too many Limit potential problems
Optimization PHP-FPM apt-get install htop
Optimization Opcache Opcache enabled
Optimization Opcache • Reduce CPU and file I/O per request
• Small trade off in RAM usage • Really a no-brainer Cache PHP File Bytecode
Optimization Opcache /etc/php/7.[1|2]/fpm/php.ini
Optimization Opcache /etc/php/7.[1|2]/fpm/php.ini
Optimization Opcache • Must reload PHP-FPM after any code change
• (e.g. after any deploy) • (you’re not editing code files in production, right?) Caveat: sudo service php7.2-fpm reload
Optimization Review: Server Optimization PHP-FPM: Process Management Opcache: Free Performance
Boost
Optimization Specialization • Give MySQL it’s own server*** • Give
Redis it’s own server • Give workers their own server • etc Split Out Services:
Optimization MySQL Optimization tools.percona.com/wizard
Optimization MySQL Optimization tools.percona.com/wizard Caches + InnoDB: The interesting stuff
Optimization Review: Optimization PHP-FPM: Process Management Split Servers: Esp MySQL
“n+1 problem”: Eager Loading Object Caching: Decorators
Scaling Out
• Load Balancing (Considerations) • Laravel + Proxies (Trusted Proxy)
• Queues (tricks!) Scaling Out
Scaling Out Load Balancing • Spread traffic across multiple servers
• Remember: This is about consistency, not speed! • Also, caveats!
Scaling Out Caveats • More Servers! • LB + App
Servers • … And everything else… Load Balancing
Scaling Out Caveats Load Balancing • Decentralizing Services • Redis
(session/object cache) • MySQL* • Any other services (search, workers, cron) • Decentralizing Files • Sessions • User uploads: S3, NFS
Scaling Out Load Balancing Example: HAProxy Load Balancing
Scaling Out HAProxy /etc/haproxy/haproxy.cfg
Scaling Out HAProxy /etc/haproxy/haproxy.cfg
Scaling Out Example: Laravel + Proxies (e.g. load balancers) Load
Balancing
Scaling Out Trusted Proxy
Scaling Out Trusted Proxy
Scaling Out Trusted Proxy X-Forwarded-* headers
Scaling Out Laravel app/Http/Middleware/TrustProxies.php Load balancer’s IP address here 172.31.0.238
Scaling Out Laravel 172.31.0.238
Scaling Out Trusted Proxy X-Forwarded-* header values used!
Scaling Out Needed For: • Correct client detection • cookies,
CSRF token Trusted Proxy • Correct URL & form URI generation • url(), action() and similar helpers
Queues
Queues Scaling Out • Don’t make users wait • Remove
artificial timeout and memory constraints • Scale work across many servers Work outside of the HTTP cycle
Scaling Out Queues Even if you’re on one server! .env
Scaling Out Queues Personal Favorite: SQS Driver *CHEAP*
Scaling Out Queues Personal Favorite: SQS Driver **VERY CHEAP**
Scaling Out Queues Trick 1: Priority
Scaling Out Queues Trick 2: Segmentation Separate Queues
Scaling Out Queues Trick 2: Segmentation: Separate Workers
Queues Scaling Out • Put workers on a server sized
for their purpose • Scale workers separately Segmentation
• Optimization vs Scale Out • Optimization • Eager Loading
+ n+1 • Caching • PHP-FPM • Opcache • Specialization • MySQL Optimization • Scale Out • LB Configuration • Headers/Trusted Proxy • Queues What We Covered Scaling Laravel
More! Scaling Laravel • scalinglaravel.com • serversforhackers.com • @fideloper