Caching and tuning fun for high scalability Wim Godden Cu.be Solutions Conference Jan 28, 2011
Note about this presentation The slides of this presentation were created to assist me during the live presentation at phpBenelux Conference. When viewing it, several slides will seem odd and unclear, because the live explanation is missing. A webcast of the live presentation might be available soon. Check my blog ( http://techblog.wimgodden.be ) or Twitter (@wimgtr) for updates.
Who am I ? Wim Godden
Owner of Cu.be Solutions (http://cu.be)
PHP developer since 1997
Developer of OpenX
Zend Certified Engineer
Zend Framework Certified Engineer
MySQL Certified Developer
Who are you ? Developers ?
System/network engineers ?
Managers ?
Caching experience ?
Caching and tuning fun for high scalability Wim Godden Cu.be Solutions Conference Jan 28, 2011
Goals of this tutorial Everything about caching and tuning
A few techniques
-> Increase reliability, performance and scalability
5 visitors/day -> 5 million visitors/day
(Don't expect miracle cure !)
Goals of this tutorial Concepts & techniques
How to do stuff
How  NOT  to do stuff
LAMP
LAMP
LAMP
LAMP
Architecture
Caching
What's caching ?
What is caching ? select * from article join user on article.user_id = user.id order by created desc limit 10
Caching goals - 1 st  goal Reduce # of request
Reduce the load
Caching goals - 2 nd  goal
Some figures Pageviews : 5000 (4000 on 10 pages)
Avg. loading time : 200ms
Cache 10 pages
Avg. loading time : 20ms
-> Total avg. loading time : 56ms
Worth it ?
Caching goals - 3 rd  goal Send less data across the network / Internet You benefit -> lower bill from upstream provider
Users benefit -> faster page load
Wait a second... that's mostly frontend stuff !
Theory of caching DB
Theory of caching DB
Caching techniques #1 : Store entire pages Company Websites
Blogs
Full pages that don't change
Render -> Store in cache -> retrieve from cache
Caching techniques #1 : Store entire pages
Caching techniques #2 : Store parts of a page Most common technique
Usually a small block in a page
Best effect : reused on lots of pages
Can be inserted on dynamic pages
Caching techniques #2 : Store parts of a page
Caching techniques #3 : Store SQL queries ↔ SQL query cache Limited in size
Caching techniques #3 : Store SQL queries ↔ SQL query cache Limited in size
Resets on every insert/update/delete
Server and connection overhead Goal : not  to get rid of DB
free up DB resources for more hits ! Better : store returned object
store group of objects
Caching techniques #3 : Store SQL queries
Caching techniques #4 : Store complex PHP results Not just calculations
CPU intensive tasks : Config file parsing
XML file parsing
Loading CSV in an array Save resources -> more resources available
Caching techniques #4 : Store complex PHP results
Caching techniques #xx : Your call Only limited by your imagination ! When you have data, think : Creating time ?
Modification frequency ?
Retrieval frequency ?
How to find cacheable data New projects : start from 'cache anything'
Existing projects : Look at MySQL slow query log
Make a complete query log (don't forget to turn it off !)
Check page loading times
Caching storage - MySQL query cache Use it
Don't rely on it
Good if you have : lots of reads
few different queries Bad if you have : lots of insert/update/delete
lots of different queries
Caching storage - Database memory tables Tables stored in memory
In MySQL : memory/heap table
↔ temporary table : memory tables are persistent
temporary tables are session-specific Faster than disk-based tables
Can be joined with disk-based tables
But :  default 16MByte limit
master-slave = trouble
if you don't need join -> overhead of DB software So : don't use it unless you need to join
Caching storage - Opcode caching DO !
Caching storage - Opcode caching APC De-facto standard
Will be in PHP core in 5.4 ? 5.5 ? 6.0 ?
PECL or packages eAccelerator
Zend Accelerator
Caching storage - Opcode caching APC De-facto standard
Will be in PHP core in 5.4 ? 5.5 ? 6.0 ?
PECL or packages eAccelerator
Zend Accelerator
X-Cache
WinCacheForPhp PHP PHP + APC PHP + eAccelerator 42.18 req/sec 206.20 req/sec 211.49 req/sec
Caching storage - Disk Data with few updates : good
Caching SQL queries : preferably not
DON'T  use NFS or other network file systems especially for sessions
high latency
locking issues !
Caching storage - Memory disk (ramdisk) Usually faster than physical disk
But : OS file caching makes difference minimal
Caching storage - Disk / ramdisk Overhead : filesystem access
Limited number of files per directory -> Subdirectories Local 5 Webservers -> 5 local caches
-> Hard to scale
How will you keep them synchronized ? -> Don't say NFS or rsync !
Caching storage - APC variable cache More than an opcode cache
Store user data in memory
apc_add / apc_store to add/update
apc_fetch to retrieve
apc_delete
Fast -> huge performance impact
Caching storage - APC variable cache More than an opcode cache
Store user data in memory
apc_add / apc_store to add/update
apc_fetch to retrieve
apc_delete
Fast -> huge performance impact
Session support !
Downside : local storage -> hard to scale
restart Apache -> cache = empty
Caching storage - Memcache Facebook, Twitter, Slashdot, … -> need we say more ?
Distributed memory caching system
Key-value storage system Keys - max. 250bytes
Values - max. 1Mbyte
Caching storage - Memcache Facebook, Twitter, Slashdot, … -> need we say more ?
Distributed memory caching system
Multiple machines ↔ 1 big memory-based hash-table
Key-value storage system Keys - max. 250bytes
Values - max. 1Mbyte Extremely fast... non-blocking, UDP (!)
Memcache - where to install
Memcache - where to install
Memcache - installation & running it Installation Distribution package
PECL

Caching and tuning fun for high scalability @ phpBenelux 2011