Highly Scalable Web Applications
Document Sample


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 — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
Various aspects
Web Server
Database
Caching
Eli White — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
In the Beginning …
One Web Server:
Eli White — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
Load Balanced
Eli White — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
Numerous Options
NetScaler
DNS Rotation
Apache Proxy BIG-IP
Cloud Services
Eli White — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
Preparation
Ensure you don't preclude this, for example:
If using local caching (APC / Zend Server / Other )
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 — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
Database Scaling
Everyone starts with just one server:
Multiple steps to take as you move forward
Eli White — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
Step One: Master/Slave
Eli White — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
Master/Slave Preparation
Even with one server:
Make code write to master and read from slave
Eli White — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/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 — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
Step Two: Multiple Slaves
Eli White — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
One Slave per Web Server?
Not as flexible
Eli White — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
Better Solution: Random
Eli White — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/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 — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
Step Three: Slave Pools
Virtually divide your slaves into pools
Use this to isolation high database load
Potentially enhance query caches
Eli White — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
Possible Pool Layout
Eli White — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
Step Four: Partitioning
Simplest Definition:
Break your tables or databases into smaller ones
Eli White — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
Multiple Masters
Eli White — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
Cons of Partitioning
Loss of direct SQL support
Increased Web Server / Application Load
More complicated programming
Eli White — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
Main Types of Partitioning
Vertical Horizontal
Application Level
Eli White — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/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 — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
Vertical Partitioning
Eli White — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/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 — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
Horizontal Partitioning
Eli White — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/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 — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
Application Level Partitioning
Eli White — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
Multiple Datacenters
You thought worrying about slave lag was bad
Data from multiple sources all needs integrated
Good luck!
Eli White — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/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 — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
Type of Caching
Single server memory caches
For PHP: APC or Zend Server Data Cache
Limited due to lack of sync'd cache
Distributed
Memcached (Generic) or Zend Platform (PHP)
Required for true scalability enhancement
Eli White — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/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 — Highly Scalable Web Applications — Web Technology of Frederick Meetup — 9/8/2009
Questions?
For this presentation & more:
http://eliw.com/
Twitter: @eliw
Zend's DevZone:
http://dz.zend.com/
Get documents about "