SlideShare a Scribd company logo
Caching and tuning fun for high scalability Wim Godden Cu.be Solutions
Notes about this presentation This presentation was presented at Dyncon 2011. It was designed to be presented live and as a result, many of the slides may seem odd when just looking at them without the spoken explanations. Also, the presentation had live benchmarks, which are ofcourse not present in the slides.
Who am I ? Wim Godden (@wimgtr)
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
Goals of this tutorial Everything about caching and tuning
A few techniques How-to
How-NOT-to -> Increase reliability, performance and scalability
5 visitors/day -> 5 million visitors/day
(Don't expect miracle cure !)
LAMP
LAMP
Architecture
Our test site
Our base benchmark Apachebench = useful enough
Result ?
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 Source of information : Reduce # of request
Reduce the load Latency : Reduce for visitor
Reduce for Webserver load Network : Send less data to visitor
Hey, that's frontend !
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
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 !
Caching techniques #3 : Store SQL queries
Caching techniques #4 : Store complex processing 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 processing 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 everything'
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 - 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 - 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 - 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
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
Windows : binaries Running No config-files
memcached -d -m <mem> -l <ip> -p <port>
ex. : memcached -d -m 2048 -l 127.0.0.1 -p 11211
Caching storage - Memcache - some notes Not fault-tolerant It's a cache !
Lose session data
Lose shopping cart data
...
Caching storage - Memcache - some notes Not fault-tolerant It's a cache !
Lose session data
Lose shopping cart data
… Different libraries Original : libmemcache
New : libmemcached (consistent hashing, UDP, binary protocol, …) Firewall your Memcache port !
Caching in PHP <?php $memcache =  new  Memcache(); $memcache->addServer( '172.16.0.1' , 11211); $memcache->addServer( '172.16.0.2' , 11211); $myData = $memcache->get( 'myKey' ); if  ($myData ===  false ) { $myData = GetMyDataFromDB(); // Put it in Memcache as 'myKey', without compression, with no expiration $memcache->set( 'myKey' , $myData,  false , 0); } echo  $myData;
Let's give that a go ! /** * Retrieves the 10 highest rated articles *  @return  array List of highest rated articles */ static public function  getTopRatedArticleList () { if  (!$articleList = $cache->load( 'topRatedArticleList' )) { $articleList =  self :: getTopRatedArticleListUncached (); $cache->save($articleList,  'topRatedArticleList' ); } return  $articleList; }
Where's the data ? Memcache client decides (!)
2 hashing algorithms : Traditional Server failure -> all data must be rehashed Consistent Server failure -> 1/x of data must be rehashed (x = # of servers) No replication !
Memcache slabs (or why Memcache says it's full when it's not) Multiple slabs of different sizes : Slab 1 : 400 bytes
Slab 2 : 480 bytes (400 * 1.2)
Slab 3 : 576 bytes (480 * 1.2) (and so on...) Multiplier (1.2 here) can be configured
Each larger slab has room for fewer items (chunks)
-> Store a lot of very large objects
-> Large slabs might be full
-> Rest of slabs might be free
-> Try to store more -> eviction of data !
Memcache - Is it working ? Connect to it using telnet &quot;stats&quot; command ->
Use Cacti or other monitoring tools STAT pid 2941 STAT uptime 10878 STAT time 1296074240 STAT version 1.4.5 STAT pointer_size 64 STAT rusage_user 20.089945 STAT rusage_system 58.499106 STAT curr_connections 16 STAT total_connections 276950 STAT connection_structures 96 STAT cmd_get 276931 STAT cmd_set 584148 STAT cmd_flush 0 STAT get_hits 211106 STAT get_misses 65825 STAT delete_misses 101 STAT delete_hits 276829 STAT incr_misses 0 STAT incr_hits 0 STAT decr_misses 0 STAT decr_hits 0 STAT cas_misses 0 STAT cas_hits 0 STAT cas_badval 0 STAT auth_cmds 0 STAT auth_errors 0 STAT bytes_read 613193860 STAT bytes_written 553991373 STAT limit_maxbytes 268435456 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT threads 4 STAT conn_yields 0 STAT bytes 20418140 STAT curr_items 65826 STAT total_items 553856 STAT evictions 0 STAT reclaimed 0

More Related Content

ODP
Caching and tuning fun for high scalability @ phpBenelux 2011
ODP
Caching and tuning fun for high scalability @ PHPTour
ODP
Caching and tuning fun for high scalability @ FOSDEM 2012
ODP
Caching and tuning fun for high scalability
ODP
Caching and tuning fun for high scalability
ODP
Caching and tuning fun for high scalability @ 4Developers
PDF
Memcached Presentation
PPTX
Lonestar php scalingmagento
Caching and tuning fun for high scalability @ phpBenelux 2011
Caching and tuning fun for high scalability @ PHPTour
Caching and tuning fun for high scalability @ FOSDEM 2012
Caching and tuning fun for high scalability
Caching and tuning fun for high scalability
Caching and tuning fun for high scalability @ 4Developers
Memcached Presentation
Lonestar php scalingmagento

What's hot (20)

PPT
Memcache
PPTX
Using memcache to improve php performance
PDF
Challenges when building high profile editorial sites
PPTX
High performance WordPress
KEY
Memcached: What is it and what does it do?
KEY
Site Performance - From Pinto to Ferrari
PPTX
cache concepts and varnish-cache
PPT
0628阙宏宇
PPTX
Northeast PHP - High Performance PHP
PPT
Roy foubister (hosting high traffic sites on a tight budget)
PDF
Memcache basics on google app engine
PDF
HTTP caching with Varnish
PDF
How to scale PHP applications
PDF
Memcached Study
PPTX
Http caching basics
ODP
MySQL Scaling Presentation
PPTX
Advanced HTTP Caching
PDF
Clug 2012 March web server optimisation
PDF
Nuvola: a tale of migration to AWS
PPTX
Memcache
Using memcache to improve php performance
Challenges when building high profile editorial sites
High performance WordPress
Memcached: What is it and what does it do?
Site Performance - From Pinto to Ferrari
cache concepts and varnish-cache
0628阙宏宇
Northeast PHP - High Performance PHP
Roy foubister (hosting high traffic sites on a tight budget)
Memcache basics on google app engine
HTTP caching with Varnish
How to scale PHP applications
Memcached Study
Http caching basics
MySQL Scaling Presentation
Advanced HTTP Caching
Clug 2012 March web server optimisation
Nuvola: a tale of migration to AWS
Ad

Similar to Caching and tuning fun for high scalability (20)

ODP
Caching and tuning fun for high scalability @ FrOSCon 2011
ODP
phptek13 - Caching and tuning fun tutorial
ODP
Caching and tuning fun for high scalability
ODP
Caching and tuning fun for high scalability
PPT
Performance and Scalability
PDF
Scaling PHP apps
ODP
High Performance Web Sites
PPTX
Anthony Somerset - Site Speed = Success!
PPT
Drupal Performance - SerBenfiquista.com Case Study
PDF
Less and faster – Cache tips for WordPress developers
KEY
Introduction to memcached
PPTX
Improving Website Performance with Memecached Webinar | Achieve Internet
PPTX
Improving Website Performance with Memecached Webinar | Achieve Internet
PPT
Performance Optimization using Caching | Swatantra Kumar
PDF
Profiling PHP with Xdebug / Webgrind
PDF
php & performance
PDF
Cache all the things #DCLondon
ODP
Itb session v_memcached
PPT
Zend Con 2008 Slides
PPTX
Zendcon scaling magento
Caching and tuning fun for high scalability @ FrOSCon 2011
phptek13 - Caching and tuning fun tutorial
Caching and tuning fun for high scalability
Caching and tuning fun for high scalability
Performance and Scalability
Scaling PHP apps
High Performance Web Sites
Anthony Somerset - Site Speed = Success!
Drupal Performance - SerBenfiquista.com Case Study
Less and faster – Cache tips for WordPress developers
Introduction to memcached
Improving Website Performance with Memecached Webinar | Achieve Internet
Improving Website Performance with Memecached Webinar | Achieve Internet
Performance Optimization using Caching | Swatantra Kumar
Profiling PHP with Xdebug / Webgrind
php & performance
Cache all the things #DCLondon
Itb session v_memcached
Zend Con 2008 Slides
Zendcon scaling magento
Ad

More from Wim Godden (20)

PDF
Beyond php - it's not (just) about the code
PDF
Bringing bright ideas to life
PDF
The why and how of moving to php 8
PDF
The why and how of moving to php 7
PDF
My app is secure... I think
PDF
My app is secure... I think
PDF
Building interactivity with websockets
PDF
Bringing bright ideas to life
ODP
Your app lives on the network - networking for web developers
ODP
The why and how of moving to php 7.x
ODP
The why and how of moving to php 7.x
ODP
Beyond php - it's not (just) about the code
ODP
My app is secure... I think
ODP
Building interactivity with websockets
ODP
Your app lives on the network - networking for web developers
ODP
My app is secure... I think
ODP
My app is secure... I think
ODP
The promise of asynchronous php
ODP
My app is secure... I think
ODP
My app is secure... I think
Beyond php - it's not (just) about the code
Bringing bright ideas to life
The why and how of moving to php 8
The why and how of moving to php 7
My app is secure... I think
My app is secure... I think
Building interactivity with websockets
Bringing bright ideas to life
Your app lives on the network - networking for web developers
The why and how of moving to php 7.x
The why and how of moving to php 7.x
Beyond php - it's not (just) about the code
My app is secure... I think
Building interactivity with websockets
Your app lives on the network - networking for web developers
My app is secure... I think
My app is secure... I think
The promise of asynchronous php
My app is secure... I think
My app is secure... I think

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Approach and Philosophy of On baking technology
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Machine learning based COVID-19 study performance prediction
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Empathic Computing: Creating Shared Understanding
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Encapsulation_ Review paper, used for researhc scholars
Big Data Technologies - Introduction.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
MYSQL Presentation for SQL database connectivity
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Approach and Philosophy of On baking technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
NewMind AI Weekly Chronicles - August'25 Week I
Review of recent advances in non-invasive hemoglobin estimation
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
“AI and Expert System Decision Support & Business Intelligence Systems”
Machine learning based COVID-19 study performance prediction
Dropbox Q2 2025 Financial Results & Investor Presentation
Empathic Computing: Creating Shared Understanding
The AUB Centre for AI in Media Proposal.docx
Unlocking AI with Model Context Protocol (MCP)
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Encapsulation_ Review paper, used for researhc scholars

Caching and tuning fun for high scalability