100% found this document useful (3 votes)
309 views32 pages

Habits of Highly Scalable Web Applications

The document discusses strategies for scaling web applications. It covers load balancing web servers, scaling the database through techniques like master-slave replication, random distribution of reads across slave servers, database partitioning, and caching. Database scaling involves multiple steps like adding slaves, distributing slaves across pools, and partitioning tables horizontally or vertically. Application-level partitioning and integrating data across datacenters are also briefly covered.

Uploaded by

kaplumb_aga
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
309 views32 pages

Habits of Highly Scalable Web Applications

The document discusses strategies for scaling web applications. It covers load balancing web servers, scaling the database through techniques like master-slave replication, random distribution of reads across slave servers, database partitioning, and caching. Database scaling involves multiple steps like adding slaves, distributing slaves across pools, and partitioning tables horizontally or vertically. Application-level partitioning and integrating data across datacenters are also briefly covered.

Uploaded by

kaplumb_aga
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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

You might also like