• Increase requests per second
• Increase speed of response (reduce response time)
• Decrease resource usage
• Secondary Goal: Less Servers!
What is Scaling
Optimization
Goal:
Efficiency
Slide 4
Slide 4 text
• Consistent requests per second
• Consistent speed of response
• Consistent despite increasing traffic
What is Scaling
Scaling Out
Goal:
Consistency
Optimization
PHP-FPM
Optimizing PHP-FPM
Process Management
Slide 26
Slide 26 text
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
Slide 27
Slide 27 text
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!
Slide 28
Slide 28 text
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
Slide 29
Slide 29 text
Optimization
PHP-FPM
apt-get install htop
Slide 30
Slide 30 text
Optimization
Opcache
Opcache enabled
Slide 31
Slide 31 text
Optimization
Opcache
• Reduce CPU and file I/O per request
• Small trade off in RAM usage
• Really a no-brainer
Cache PHP File Bytecode
Slide 32
Slide 32 text
Optimization
Opcache
/etc/php/7.[1|2]/fpm/php.ini
Slide 33
Slide 33 text
Optimization
Opcache
/etc/php/7.[1|2]/fpm/php.ini
Slide 34
Slide 34 text
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
Slide 35
Slide 35 text
Optimization
Review: Server Optimization
PHP-FPM: Process Management
Opcache: Free Performance Boost
Slide 36
Slide 36 text
Optimization
Specialization
• Give MySQL it’s own server***
• Give Redis it’s own server
• Give workers their own server
• etc
Split Out Services:
Slide 37
Slide 37 text
Optimization
MySQL Optimization
tools.percona.com/wizard
Slide 38
Slide 38 text
Optimization
MySQL Optimization
tools.percona.com/wizard
Caches + InnoDB:
The interesting stuff
Slide 39
Slide 39 text
Optimization
Review: Optimization
PHP-FPM: Process Management
Split Servers: Esp MySQL
“n+1 problem”: Eager Loading
Object Caching: Decorators
Scaling Out
Laravel
app/Http/Middleware/TrustProxies.php
Load balancer’s IP address here
172.31.0.238
Slide 53
Slide 53 text
Scaling Out
Laravel
172.31.0.238
Slide 54
Slide 54 text
Scaling Out
Trusted Proxy
X-Forwarded-*
header values
used!
Slide 55
Slide 55 text
Scaling Out
Needed For:
• Correct client detection
• cookies, CSRF token
Trusted Proxy
• Correct URL & form URI generation
• url(), action() and similar helpers
Slide 56
Slide 56 text
Queues
Slide 57
Slide 57 text
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
Slide 58
Slide 58 text
Scaling Out
Queues
Even if you’re on one server!
.env
Slide 59
Slide 59 text
Scaling Out
Queues
Personal Favorite: SQS Driver
*CHEAP*
Slide 60
Slide 60 text
Scaling Out
Queues
Personal Favorite: SQS Driver
**VERY CHEAP**
Slide 61
Slide 61 text
Scaling Out
Queues
Trick 1: Priority
Slide 62
Slide 62 text
Scaling Out
Queues
Trick 2: Segmentation
Separate Queues
Slide 63
Slide 63 text
Scaling Out
Queues
Trick 2: Segmentation:
Separate Workers
Slide 64
Slide 64 text
Queues
Scaling Out
• Put workers on a server sized for their purpose
• Scale workers separately
Segmentation
Slide 65
Slide 65 text
• 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