LAMP scaling 101

when a single server no longer cuts it

Luka Kladaric - phpDay 2013

@kll
[email protected]
http://luka.io

who?

about Luka

web tinkerer since before I was allowed
to stay out after dark

about Luka

10 years of PHP for a living

mostly freelancing
about Luka

putting the genie back in the bottle
at deviantART since 2010.

(a 13-year-old, 30-million-user, 90 million pv/day digital artists community)

anatomy of a LAMP app

  • static requests (css/js/img)
  • app requests (php)
  • database
  • storage / user uploads

static requests

css, js, images and other assets
static requests

different needs, different setup

you either have a very slow PHP setup,
or you're wasting a ton of resources on the static

static requests

separate from app servers!

static requests

nginx

static requests

CDN

moar juice

out of your php

moar juice out of your php

opcode cache

moar juice out of your php

static page caching

for all your logged out traffic

moar juice out of your php

memcached

but make sure you have an off switch

multiple app servers

multiple app servers

no sessions

yes, you could.
but no, you shouldn't.

multiple app servers

no common local cache

multiple app servers

race conditions

multiple app servers

centralised configuration

multiple app servers

distributed cron

atomic claim through db

multiple app servers

async job queue

gearman

databases

db replication

db replication

master/slave

db replication

performance gains

scaling reads only,
writes happen on every slave
db replication

replication lag

the truth is flexible
db replication

update + select


-- atomic increment & return
UPDATE users SET login_count = LAST_INSERT_ID(login_count + 1);
	                    

$new_login_count = $db->last_insert_id(); // magic
	                    
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id

db pooling

as in: a pool of servers.
db pooling

isolation of concerns

achieving consistent performance is often times
more important than achieving speed
db pooling

isolation of pain points

maintaining performance/availability on critical sections
db pooling

higher cache hit rate

db clusters

separating different types of content to completely
unrelated clusters of replicated databases

db partitioning

the art of making your data horribly difficult to track
db partitioning

vertical (row splitting)

splitting lesser-used fields into extra tables

db partitioning

horizontal (sharding)

splitting data by a column

  • over-shard in advance
  • forget about joins

db wenching

satisfying as many consumers of the same source
of data with a single db request

dT blog: mixing DOM and PHP objects for fun & profit

storage

storage

S3 or DIY

cool bonus feature: timebombed links
storage

CDN

storage

no longer "local" data

premature optimization

don't do it.

srsly.

review

review

static

offload & forget

review

php

  • multi-server
  • shared-nothing
  • no local state
review

database

  • partitioning
  • pooling
  • sharding
  • no joins
review

storage

offload & forget

questions? :)

THANK YOU

@kll
[email protected]
http://luka.io


please rate & comment
https://joind.in/8649