Sie sind auf Seite 1von 32

Habits of Highly Scalable Web Applications

Eli White
Zend

http://eliw.com/
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 Server
Database
Caching

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

NetScaler

DNS Rotation
Apache Proxy BIG-IP
Cloud Services

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 cache
Don'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 Slave
class 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 support


Increased Web Server / PHP load
More complicated programming

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

Vertical Horizontal

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 Cache
Limited due to lack of sync'd cache

Distributed
Memcached or Zend Platform
Required 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

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

For this presentation & more:


http://eliw.com/

Twitter: @eliw

Zend's DevZone:
http://dz.zend.com/

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

Das könnte Ihnen auch gefallen