Habits of Highly Scalable Web Applications

Post on 16-Nov-2014

179 views 3 download

description

Eli White's "Habits of Highly Scalable Web Applications" slides from Dutch PHP Conference 2009

Transcript of Habits of Highly Scalable Web Applications

Habits of Highly Scalable Web Applications

Eli WhiteZend

http://eliw.com/

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Scaling?

Enabling your application to grow as traffic grows

Only do what you need but …Don't code yourself into a corner

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Various aspects

Web ServerDatabaseCaching

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

In the Beginning …

One Web Server:

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Load Balanced

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Numerous Options

DNS RotationApache ProxyCloud Services

NetScaler

BIG-IP

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Preparation

Ensure you don't preclude this, for example:If using local caching (APC / Zend Server)

Avoid assuming exclusive/single cacheDon't rely on the filesystem

Temporary files, Sessions, etc

If you do have code assuming one server: Encapsulate

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Database Scaling

Everyone starts with just one server:

Multiple steps to take as you move forward

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Step One: Master/Slave

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Master/Slave Preparation

Even with one server:Make code write to master and read from slave

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Avoid Slave Lag

Don't write code that would fail with slave lag:

$master->query('update users set comments += 1'); $slave->query('select comments from users');

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Step Two: Multiple Slaves

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

One Slave per Web Server?

Not as flexible

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Better Solution: Random

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Code to Select Random Slaveclass DB { private static $cfg = array( 'write' => array('mysql:dbname=MyDB;host=10.1.2.3'), 'read' => array('mysql:dbname=MyDB;host=10.1.2.7', 'mysql:dbname=MyDB;host=10.1.2.8', 'mysql:dbname=MyDB;host=10.1.2.9'); );

public static function getConnection($pool) { $max = count(self::$cfg[$pool]) - 1; $dsn = self::$cfg[$pool][mt_rand(0, $max)]; return new PDO($dsn, USER, PASS); }}

$db = DB::getConnection('read');

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Step Three: Slave Pools

Virtually divide your slaves into pools

Use this to isolation high database load

Potentially enhance query caches

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Possible Pool Layout

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Step Four: Partitioning

Simplest Definition:Break your tables or databases into smaller ones

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Multiple Masters

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Cons of Partitioning

Loss of direct SQL supportIncreased Web Server / PHP loadMore complicated programming

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Main Types of Partitioning

HorizontalVertical

Application Level

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Vertical Partitioning

“Moving various columns of your table into different tables”

Various methodologies:● Move rarely used columns into auxiliary table● Move often empty columns into auxiliary table● Move columns that are not used in where clauses

Usually done within the same database/server

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Vertical Partitioning

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Horizontal Partitioning

“Moving various rows of your table into different tables”

Various methodologies:● Range Based● Date Based● Interlaced● User Based

Can be done on one server, or break into multiple masters

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Horizontal Partitioning

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Application Level Partitioning

“Moving various tables of your DB onto different servers”

Various methodologies:● Move single tables to specific servers● Move groups of related tables together to allow joining

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Application Level Partitioning

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Multiple Datacenters

You thought worrying about slave lag was bad

Data from multiple sources all needs integrated

Good luck!

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Brief Touch on Caching

Often considered performance

Can absolutely be a scalability factor, especially when combined with smaller discrete DB queries

Allows you to get around DB scalability by ignoring the DB

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Type of Caching

Single server memory caches

APC or Zend Server Data CacheLimited due to lack of sync'd cache

Distributed

Memcached or Zend PlatformRequired for true scalability enhancement

Eli White — Habits of Highly Scalable Web Applications — Dutch PHP Conference 2009 — 6/13/2009

Caching Best Practices

Write through cache

Choose small, discrete, reusable data units

Don't store data you can't recreate

Store data in as close to final processed form

Questions?

For this presentation & more:

http://eliw.com/

Zend's DevZone:

http://dz.zend.com/

Twitter: @eliw

Rate Me: http://joind.in/591