Top Banner
scaling LAMP doesn’t have to suck! @stevencorona (400 million api calls/day) (3 billion photos)
43

Steve Corona: Scaling LAMP doesn't have to suck

May 08, 2015

Download

Technology

WebExpo

"I went from sleeping in front of my laptop, restarting apache every hour, to automatically scaling servers from my phone."

More at http://webexpo.net/prague2013/talk/scaling-lamp-doesnt-have-to-suck/
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Steve Corona: Scaling LAMP doesn't have to suck

scaling LAMPdoesn’t have to

suck!@stevencorona

(400 million api calls/day)

(3 billion photos)

Page 2: Steve Corona: Scaling LAMP doesn't have to suck

let’s learn how to scale php

...in 40 minutes

Page 3: Steve Corona: Scaling LAMP doesn't have to suck

scaling is...architecting your app for millions of users

Page 4: Steve Corona: Scaling LAMP doesn't have to suck

scaling is...

an art, not a science

Page 5: Steve Corona: Scaling LAMP doesn't have to suck

scaling is...

NOT CODEabout infrastructure

your code is fast enough

Page 6: Steve Corona: Scaling LAMP doesn't have to suck

LAMPLINUX

APACHEMYSQL

PHP

Page 7: Steve Corona: Scaling LAMP doesn't have to suck

problems•single points of failure•apache+mod_php memory hog•slow linux default settings•can’t do work in the background

Page 8: Steve Corona: Scaling LAMP doesn't have to suck

a better LAMPLINUX +TUNING

NGINXPERCONAPHP-FPM

NSQREDIS

Page 9: Steve Corona: Scaling LAMP doesn't have to suck

nginxloadbalancer

nginxphp-fpm

MySQLMaster

nginxphp-fpm

nginxphp-fpm

nsq

MySQLReadOnly

redis

Page 10: Steve Corona: Scaling LAMP doesn't have to suck

tuning linux•linux is mostly sane•use latest kernel•distro doesn’t matter•(i like ubuntu)

Page 11: Steve Corona: Scaling LAMP doesn't have to suck

top 3 settingsopen_files

network/sysctl tuningi/o scheduler

most settings you find online are outdated

Page 12: Steve Corona: Scaling LAMP doesn't have to suck

open filesdefault is 1024

on linux, each socket = open file

/etc/security/limits.confsoft nofile 999999hard nofile 999999

Page 13: Steve Corona: Scaling LAMP doesn't have to suck

sysctl tuningthousands of settings

newer kernels autotune network

/etc/sysctl.confnet.core.somaxconn=999999

net.ipv4.ip_local_port_range=2000 62000net.ipv4.tcp_tw_recycle=1

vm.swapiness=0

Page 14: Steve Corona: Scaling LAMP doesn't have to suck

i/o schedulerlinux has swappable scheduler

default is cfq$ echo “deadline” > \

/sys/block/sda/queue/scheduler

+50% IOPS on MySQL

Page 15: Steve Corona: Scaling LAMP doesn't have to suck

nginxopen-source HTTP server

swap out apacheeasy + sane config

nginx.org

Page 16: Steve Corona: Scaling LAMP doesn't have to suck

nginxmost popular webserver

of top 1000 sites

15,000 requests/second with 20% CPU and100MB RAM

Page 17: Steve Corona: Scaling LAMP doesn't have to suck

nginx

Page 18: Steve Corona: Scaling LAMP doesn't have to suck

nginxload balancerhttp cache

fastcgi proxyweb server

Page 19: Steve Corona: Scaling LAMP doesn't have to suck

php

php-fpm is kingstandard with php5.3

Page 20: Steve Corona: Scaling LAMP doesn't have to suck

php-fpmuse static worker pool

4x number of CPU cores

pm=staticpm.max_children=128

Page 21: Steve Corona: Scaling LAMP doesn't have to suck

phpphp 5.5 is much faster

than 5.4 and 5.3

USE IT!

Page 22: Steve Corona: Scaling LAMP doesn't have to suck

php

php 5.5 includes Zend Optimizer+ (no more APC)

10-20% faster than APC in real world

Page 23: Steve Corona: Scaling LAMP doesn't have to suck

php

horizontal scalingwatch out for sessions!

Page 24: Steve Corona: Scaling LAMP doesn't have to suck

php + mysqlmysql persistent

connections are GOOD to use.

despite what the internet says

Page 25: Steve Corona: Scaling LAMP doesn't have to suck

php + mysql

20-25% faster for first query

after that it doesn’t matter

Page 26: Steve Corona: Scaling LAMP doesn't have to suck

php + mysqlwatch out for thundering

herdmysql’s max_connections

2(pm.max_children * # servers)

Page 27: Steve Corona: Scaling LAMP doesn't have to suck

php + mysqlturn on mysql protocol

compression

 $m = mysqli_init(); $m->real_connect($username...., MYSQL_CLIENT_COMPRESS);

Page 28: Steve Corona: Scaling LAMP doesn't have to suck

perconaopen source mysql fork

patches from twitter and google

percona.com

Page 29: Steve Corona: Scaling LAMP doesn't have to suck

perconadrop-in replacement

faster at scale, more reliable

Page 30: Steve Corona: Scaling LAMP doesn't have to suck

percona

new transactions per minute (more is better)

Page 31: Steve Corona: Scaling LAMP doesn't have to suck

percona•only use innodb•disable query cache•enable thread pool

thread_handling=pool-of-threads

Page 32: Steve Corona: Scaling LAMP doesn't have to suck

perconawatch out for NUMA

innodb_buffer_pool_populate=1flush_caches=1numa_interleave=1

Page 33: Steve Corona: Scaling LAMP doesn't have to suck

perconastop wasting time

use SSDs

went from15 mysql servers to 2 using SSD

Page 34: Steve Corona: Scaling LAMP doesn't have to suck

redisin the old days, we used

memcache

redis is better

redis.io

Page 35: Steve Corona: Scaling LAMP doesn't have to suck

redis•data persisted to disk•hot cache•500,000 GET/second

Page 36: Steve Corona: Scaling LAMP doesn't have to suck

redisconfig is great out of box

avoid stalls on EC2use HVM instances

Page 37: Steve Corona: Scaling LAMP doesn't have to suck

redislots of php libraries

use pecl-redisc extension

pecl install redis

Page 38: Steve Corona: Scaling LAMP doesn't have to suck

nsqmessage queue server in golang by

bit.ly

we use it to process 370 million events/day

https://github.com/bitly/nsq

Page 39: Steve Corona: Scaling LAMP doesn't have to suck

nsq•nsqphp is best php library•pub/sub model•do work async•api calls•defer long-running work

https://github.com/davegardnerisme/nsqphp

Page 40: Steve Corona: Scaling LAMP doesn't have to suck

nsq

Page 41: Steve Corona: Scaling LAMP doesn't have to suck

nsq

DON’T USE MYSQLAS A WORK QUEUE!

Page 42: Steve Corona: Scaling LAMP doesn't have to suck

don’t scale code

your code is fast enough

scale infrastructure

Page 43: Steve Corona: Scaling LAMP doesn't have to suck

thanks!

follow me on twitter@stevencorona