scaling LAMP
doesn’t have to
suck!
@stevencorona
(800 million api calls/day)
let’s learn how to scale
php
!
...in 40 minutes
LAMP
LINUX
APACHE
MYSQL
PHP
a better LAMP
LINUX +TUNING
NGINX
PHP-FPM
PERCONA
NSQ
REDIS
nginx
loadbalancer
nginx
php-fpm
MySQL
Master
nginx
php-fpm
nginx
php-fpm
nsq
MySQL
Slavecache
MySQL
Slave
tuning linux
•linux is mostly sane	

•use latest kernel	

•distro doesn’t matter	

•(i like ubuntu)
top 3 settings
open_files	

network/sysctl tuning	

i/o scheduler	

!
most settings you find	

online are outdated
open files
default is 1024	

on linux, each socket = open file	

!
/etc/security/limits.conf!
soft nofile 65535!
hard nofile...
sysctl tuning!
thousands of settings	

newer kernels autotune network	

!
/etc/sysctl.conf!
net.core.somaxconn=65536!
net....
i/o scheduler
linux has swappable scheduler	

default is cfq	

!
$ echo “deadline” > !
/sys/block/sda/queue/scheduler
!
+5...
nginx
open-source HTTP server	

swap out apache	

easy + sane config	

nginx.org
nginx
most popular webserver
of top 1000 sites	

!
15,000 requests/second	

with 20% CPU and100MB RAM
nginx
nginx
load balancer	

http cache	

fastcgi proxy	

web server
php
php-fpm is king	

standard with php5.3
php-fpm
use static worker pool	

4x number of CPU cores	

!
pm=static!
pm.max_children=128
php
php 5.5 is much faster
than 5.4 and 5.3	

!
USE IT!
php
!
php 5.5 includes Zend
Opcache	

(no more APC)	

!
10-20% faster than APC in real world
php
horizontal scaling	

watch out for sessions!
php + mysql
mysql persistent
connections are GOOD
to use.	

!
despite what the internet says
percona
!
open source mysql fork	

!
patches from 	

twitter and google	

percona.com
percona
drop-in replacement	

!
faster at scale, more
reliable
percona
new transactions per
minute (more is better)
percona
•only use innodb	

•disable query cache	

•enable thread pool	

thread_handling=pool-of-threads!
percona
stop wasting time	

use SSDs
!
redis
in the old days, we used
memcache	

!
redis is better
!
redis.io
redis
•data persisted to disk	

•hot cache	

•500,000 GET/second
redis
config is great out of box	

!
avoid stalls on EC2	

use HVM instances
redis
lots of php libraries	

!
use pecl-redis	

c extension	

pecl install redis
nsq
message queue server in golang by
bit.ly	

!
we use it to process 800 million
events/day	

!
https://github.com/bitly/...
nsq
•nsqphp is best php library	

•pub/sub model	

•do work async	

•api calls	

•defer long-running work	

!
https://gith...
nsq
nsq
DON’T USE MYSQL	

AS A WORK QUEUE!
don’t scale code
!
your code is fast enough	

!
scale infrastructure
thanks!
check out my book—

SCALING PHP



http://scalingphpbook.com
Upcoming SlideShare
Loading in …5
×

Scaling LAMP doesn't have to suck

696 views

Published on

Scale PHP Effortlessly— Learn the 80/20 of owning your LAMP stack and get back to coding! I spent a year sleeping next to my laptop, rolling over to restart Apache, as I scaled Twitpic to 50m users. Save your sanity and learn from my mistakes.

Published in: Technology
2 Comments
3 Likes
Statistics
Notes
No Downloads
Views
Total views
696
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
11
Comments
2
Likes
3
Embeds 0
No embeds

No notes for slide

Scaling LAMP doesn't have to suck

  1. 1. scaling LAMP doesn’t have to suck! @stevencorona (800 million api calls/day)
  2. 2. let’s learn how to scale php ! ...in 40 minutes
  3. 3. LAMP LINUX APACHE MYSQL PHP
  4. 4. a better LAMP LINUX +TUNING NGINX PHP-FPM PERCONA NSQ REDIS
  5. 5. nginx loadbalancer nginx php-fpm MySQL Master nginx php-fpm nginx php-fpm nsq MySQL Slavecache MySQL Slave
  6. 6. tuning linux •linux is mostly sane •use latest kernel •distro doesn’t matter •(i like ubuntu)
  7. 7. top 3 settings open_files network/sysctl tuning i/o scheduler ! most settings you find online are outdated
  8. 8. open files default is 1024 on linux, each socket = open file ! /etc/security/limits.conf! soft nofile 65535! hard nofile 65535
  9. 9. sysctl tuning! thousands of settings newer kernels autotune network ! /etc/sysctl.conf! net.core.somaxconn=65536! net.ipv4.ip_local_port_range=2000 62000!
  10. 10. i/o scheduler linux has swappable scheduler default is cfq ! $ echo “deadline” > ! /sys/block/sda/queue/scheduler ! +50% IOPS on MySQL
  11. 11. nginx open-source HTTP server swap out apache easy + sane config nginx.org
  12. 12. nginx most popular webserver of top 1000 sites ! 15,000 requests/second with 20% CPU and100MB RAM
  13. 13. nginx
  14. 14. nginx load balancer http cache fastcgi proxy web server
  15. 15. php php-fpm is king standard with php5.3
  16. 16. php-fpm use static worker pool 4x number of CPU cores ! pm=static! pm.max_children=128
  17. 17. php php 5.5 is much faster than 5.4 and 5.3 ! USE IT!
  18. 18. php ! php 5.5 includes Zend Opcache (no more APC) ! 10-20% faster than APC in real world
  19. 19. php horizontal scaling watch out for sessions!
  20. 20. php + mysql mysql persistent connections are GOOD to use. ! despite what the internet says
  21. 21. percona ! open source mysql fork ! patches from twitter and google percona.com
  22. 22. percona drop-in replacement ! faster at scale, more reliable
  23. 23. percona new transactions per minute (more is better)
  24. 24. percona •only use innodb •disable query cache •enable thread pool thread_handling=pool-of-threads!
  25. 25. percona stop wasting time use SSDs !
  26. 26. redis in the old days, we used memcache ! redis is better ! redis.io
  27. 27. redis •data persisted to disk •hot cache •500,000 GET/second
  28. 28. redis config is great out of box ! avoid stalls on EC2 use HVM instances
  29. 29. redis lots of php libraries ! use pecl-redis c extension pecl install redis
  30. 30. nsq message queue server in golang by bit.ly ! we use it to process 800 million events/day ! https://github.com/bitly/nsq
  31. 31. nsq •nsqphp is best php library •pub/sub model •do work async •api calls •defer long-running work ! https://github.com/davegardnerisme/nsqphp
  32. 32. nsq
  33. 33. nsq DON’T USE MYSQL AS A WORK QUEUE!
  34. 34. don’t scale code ! your code is fast enough ! scale infrastructure
  35. 35. thanks! check out my book—
 SCALING PHP
 
 http://scalingphpbook.com

×