SlideShare a Scribd company logo
1 of 35
Download to read offline
CACHING FOR CASH -
    CACHING
     Scott MacVicar
     php|works 2008
WHY CACHE?


Reduce memory usage

Reduce processing time

Reduce network traffic

Reduce disk access
CACHING OPTIONS

Flat file

RAM disk

Database

Memcache

APC

Script level
WHAT TO CACHE?


External resources

Large result sets

Static data

Sessions
FLAT FILE CACHE


Write data out to a local file

Config files from Database or XML

Remote resources store locally

Output buffering to local file
CACHING FILES



PEAR::Cache_Lite

Zend_Cache in Zend Framework

var_export in PHP
RAM DISK



Mount some memory as disk space

Used when IO actions can’t be avoided

Non resizable on the fly
LIGHTTPD + PHP



Lua is a scripting language in lighttpd

Use PHP to write out file to disk

Have Lua look at the modified time of the file, if
within the limit serve else execute PHP
MEMCACHE


Origins within Livejournal

Used by Facebook, Youtube, Wikipedia and digg

Caching daemon, no persistence

Stores key / value pairs
FACEBOOK EXAMPLE



200 dedicated memcache servers

Each 16GB quad-core amd64

3TB memcache data
MEMCACHE


Slab Allocator

Libevent based (non-blocking)

Simple Protocol

Server is just a hash table

No authentication or self awareness
MEMCACHE CLIENT



Clients provide key and value

Responsible for serialising any value

Compress data
SERVER PROTOCOL


set/replace/add

get

append/prepend

increment/decrement

compare and swap
HASHING


Key is hashed into a value

Modulous then applied which is the number of the
servers

Server is then picked and stored

If one server drops out you only lose a fraction of the
keys
MEMCACHE LIMITS


Key Size is 250 bytes

Data Size is 1 megabyte

32bit/64bit memory limit

No Replication across cluster
MEMCACHE CLEANING UP



Uses Least Recently Used Algorithm

Looks for the oldest item and removes

TTL also applies to key / value pairs
SIMPLE USAGE



Fetch from memcache

If there return

Else calculate, store in cache and return
WHERE TO RUN?



Any server works

No need for matching size

Low CPU usage
MEMCACHE + PHP


PECL extension using custom connection

pecl install memcache

Provides MemcachePool and Memcache

INI settings to change hash strategy, function and
protocol.
MEMCACHE SAMPLE
MEMCACHE ACTIONS



Add - only if the key doesn’t exist

Set - will add or update key

Replace - only if the key already exists
MEMCACHE TASK


Open Terminal in VMWare Image and run
sudo service memcached start

Try using memcache to set, get and add values

Try setting a value using the Time To Live parameter

Ask questions :-)
MEMCACHEDB



Persistent key/value storage system

BDB used for persitence

Compatible with Memcache protocol
MEMCACHEDB CAVEATS



No expiration

Isn’t a replacement for memcache
APC


Provides OPCode caching in PHP

Shared memory for storage

APC is only on the local server

No network overhead from TCP/IP
APC
  Input


Tokenizer


 Parser                     APC


Compiler
            Store Opcodes



Executor
APC SAMPLE
CACHE LAYERING

                                         Disk /
   APC            Memcache
                                        Database


Fetch from APC locally

Fetch from Memcache, add to APC

Finally fetch from the original source and store back
in Memcache
APC USE CASES



Should always use it to cache opcode of files

Small but frequently accessed things

You only have one server
APC TASK



Try storing classes or arrays in APC

Use the Time To Live

APC supports an array for keys in apc_get, fetch
multiple values
SQLITE



Local node storage of filesystem

Cache result sets from remote database

In memory database
DATABASE QUERY CACHING



Rely on MySQL to do the query caching

Doesn’t quite work though :-(

Invalidation of cache happens easily
ALTERNATIVE DATABASE
        CACHING


Store the result set from the database using another
caching software

Hash query as a key value

Extend your DB layer to add this transparently?
CACHING TIPS


Pre-heat the cache

Use multiple levels of cache

Cache even dynamic data for short times

serialize() is slow
QUESTIONS?

More Related Content

What's hot

Php psr standard 2014 01-22
Php psr standard 2014 01-22Php psr standard 2014 01-22
Php psr standard 2014 01-22
Võ Duy Tuấn
 
Roy foubister (hosting high traffic sites on a tight budget)
Roy foubister (hosting high traffic sites on a tight budget)Roy foubister (hosting high traffic sites on a tight budget)
Roy foubister (hosting high traffic sites on a tight budget)
WordCamp Cape Town
 
Tuning the Performance of Your ColdFusion Environment to Racecar Specs!
Tuning the Performance of Your ColdFusion Environment to Racecar Specs!Tuning the Performance of Your ColdFusion Environment to Racecar Specs!
Tuning the Performance of Your ColdFusion Environment to Racecar Specs!
Hostway|HOSTING
 
플랫폼 통합을 위한 Client Module 개발 & 배포
플랫폼 통합을 위한 Client Module 개발 & 배포플랫폼 통합을 위한 Client Module 개발 & 배포
플랫폼 통합을 위한 Client Module 개발 & 배포
흥래 김
 
Itb2018 cf apps to dev to production with command box cf-config docker
Itb2018   cf apps to dev to production with command box cf-config dockerItb2018   cf apps to dev to production with command box cf-config docker
Itb2018 cf apps to dev to production with command box cf-config docker
Ortus Solutions, Corp
 
Aeon mike guide transparent ssl filtering
Aeon mike guide transparent ssl filteringAeon mike guide transparent ssl filtering
Aeon mike guide transparent ssl filtering
Conrad Cruz
 
Aeon mike guide transparent ssl filtering (1)
Aeon mike guide transparent ssl filtering (1)Aeon mike guide transparent ssl filtering (1)
Aeon mike guide transparent ssl filtering (1)
Conrad Cruz
 

What's hot (20)

Nginx [engine x] and you (and WordPress)
Nginx [engine x] and you (and WordPress)Nginx [engine x] and you (and WordPress)
Nginx [engine x] and you (and WordPress)
 
Php psr standard 2014 01-22
Php psr standard 2014 01-22Php psr standard 2014 01-22
Php psr standard 2014 01-22
 
Roy foubister (hosting high traffic sites on a tight budget)
Roy foubister (hosting high traffic sites on a tight budget)Roy foubister (hosting high traffic sites on a tight budget)
Roy foubister (hosting high traffic sites on a tight budget)
 
High Performance Web Sites
High Performance Web SitesHigh Performance Web Sites
High Performance Web Sites
 
EasyEngine - Command-Line tool to manage WordPress Sites on Nginx
EasyEngine - Command-Line tool to manage WordPress Sites on NginxEasyEngine - Command-Line tool to manage WordPress Sites on Nginx
EasyEngine - Command-Line tool to manage WordPress Sites on Nginx
 
Memcached B box presentation
Memcached B box presentationMemcached B box presentation
Memcached B box presentation
 
How to turn any dynamic website into a static site | 24.01.2018 | Artem Danil...
How to turn any dynamic website into a static site | 24.01.2018 | Artem Danil...How to turn any dynamic website into a static site | 24.01.2018 | Artem Danil...
How to turn any dynamic website into a static site | 24.01.2018 | Artem Danil...
 
快快樂樂用Homestead
快快樂樂用Homestead快快樂樂用Homestead
快快樂樂用Homestead
 
Tuning the Performance of Your ColdFusion Environment to Racecar Specs!
Tuning the Performance of Your ColdFusion Environment to Racecar Specs!Tuning the Performance of Your ColdFusion Environment to Racecar Specs!
Tuning the Performance of Your ColdFusion Environment to Racecar Specs!
 
Caching Data For Performance
Caching Data For PerformanceCaching Data For Performance
Caching Data For Performance
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Performance all teh things
Performance all teh thingsPerformance all teh things
Performance all teh things
 
Apache
ApacheApache
Apache
 
Apache
ApacheApache
Apache
 
플랫폼 통합을 위한 Client Module 개발 & 배포
플랫폼 통합을 위한 Client Module 개발 & 배포플랫폼 통합을 위한 Client Module 개발 & 배포
플랫폼 통합을 위한 Client Module 개발 & 배포
 
Itb2018 cf apps to dev to production with command box cf-config docker
Itb2018   cf apps to dev to production with command box cf-config dockerItb2018   cf apps to dev to production with command box cf-config docker
Itb2018 cf apps to dev to production with command box cf-config docker
 
Handling 10k requests per second with Symfony and Varnish - SymfonyCon Berlin...
Handling 10k requests per second with Symfony and Varnish - SymfonyCon Berlin...Handling 10k requests per second with Symfony and Varnish - SymfonyCon Berlin...
Handling 10k requests per second with Symfony and Varnish - SymfonyCon Berlin...
 
Aeon mike guide transparent ssl filtering
Aeon mike guide transparent ssl filteringAeon mike guide transparent ssl filtering
Aeon mike guide transparent ssl filtering
 
Aeon mike guide transparent ssl filtering (1)
Aeon mike guide transparent ssl filtering (1)Aeon mike guide transparent ssl filtering (1)
Aeon mike guide transparent ssl filtering (1)
 
Apache
ApacheApache
Apache
 

Viewers also liked

Cache memory
Cache memoryCache memory
Cache memory
Anuj Modi
 
Cpu and its functions
Cpu and its functionsCpu and its functions
Cpu and its functions
myrajendra
 

Viewers also liked (13)

Alternative Databases
Alternative DatabasesAlternative Databases
Alternative Databases
 
Unit iv functions
Unit  iv functionsUnit  iv functions
Unit iv functions
 
Ch2: Computer System Structure (OS)
Ch2: Computer System Structure (OS)Ch2: Computer System Structure (OS)
Ch2: Computer System Structure (OS)
 
Types of memory (bba 1st)
Types of memory (bba 1st)Types of memory (bba 1st)
Types of memory (bba 1st)
 
Caching in
Caching inCaching in
Caching in
 
Cache memory
Cache memoryCache memory
Cache memory
 
04 Cache Memory
04  Cache  Memory04  Cache  Memory
04 Cache Memory
 
cache memory
cache memorycache memory
cache memory
 
Micro Programmed Control Unit
Micro Programmed Control UnitMicro Programmed Control Unit
Micro Programmed Control Unit
 
Cpu and its functions
Cpu and its functionsCpu and its functions
Cpu and its functions
 
Memory Organization
Memory OrganizationMemory Organization
Memory Organization
 
SQLite 3
SQLite 3SQLite 3
SQLite 3
 
Cache memory presentation
Cache memory presentationCache memory presentation
Cache memory presentation
 

Similar to Caching for Cash: Caching

Improving Website Performance and Scalability with Memcached
Improving Website Performance and Scalability with MemcachedImproving Website Performance and Scalability with Memcached
Improving Website Performance and Scalability with Memcached
Acquia
 
Dutch php conference_apc_mem2010
Dutch php conference_apc_mem2010Dutch php conference_apc_mem2010
Dutch php conference_apc_mem2010
isnull
 

Similar to Caching for Cash: Caching (20)

Caching and tuning fun for high scalability @ FrOSCon 2011
Caching and tuning fun for high scalability @ FrOSCon 2011Caching and tuning fun for high scalability @ FrOSCon 2011
Caching and tuning fun for high scalability @ FrOSCon 2011
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
 
phptek13 - Caching and tuning fun tutorial
phptek13 - Caching and tuning fun tutorialphptek13 - Caching and tuning fun tutorial
phptek13 - Caching and tuning fun tutorial
 
Caching and tuning fun for high scalability @ phpBenelux 2011
Caching and tuning fun for high scalability @ phpBenelux 2011Caching and tuning fun for high scalability @ phpBenelux 2011
Caching and tuning fun for high scalability @ phpBenelux 2011
 
Improving Website Performance with Memecached Webinar | Achieve Internet
Improving Website Performance with Memecached Webinar | Achieve InternetImproving Website Performance with Memecached Webinar | Achieve Internet
Improving Website Performance with Memecached Webinar | Achieve Internet
 
Improving Website Performance with Memecached Webinar | Achieve Internet
Improving Website Performance with Memecached Webinar | Achieve InternetImproving Website Performance with Memecached Webinar | Achieve Internet
Improving Website Performance with Memecached Webinar | Achieve Internet
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
 
Caching and tuning fun for high scalability @ FOSDEM 2012
Caching and tuning fun for high scalability @ FOSDEM 2012Caching and tuning fun for high scalability @ FOSDEM 2012
Caching and tuning fun for high scalability @ FOSDEM 2012
 
Improving Website Performance and Scalability with Memcached
Improving Website Performance and Scalability with MemcachedImproving Website Performance and Scalability with Memcached
Improving Website Performance and Scalability with Memcached
 
Give Your Site a Boost with Memcache
Give Your Site a Boost with MemcacheGive Your Site a Boost with Memcache
Give Your Site a Boost with Memcache
 
Memcache d
Memcache dMemcache d
Memcache d
 
Introduction to memcached
Introduction to memcachedIntroduction to memcached
Introduction to memcached
 
Anthony Somerset - Site Speed = Success!
Anthony Somerset - Site Speed = Success!Anthony Somerset - Site Speed = Success!
Anthony Somerset - Site Speed = Success!
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
 
Session Handling Using Memcache
Session Handling Using MemcacheSession Handling Using Memcache
Session Handling Using Memcache
 
Caching with Memcached and APC
Caching with Memcached and APCCaching with Memcached and APC
Caching with Memcached and APC
 
Dutch php conference_apc_mem2010
Dutch php conference_apc_mem2010Dutch php conference_apc_mem2010
Dutch php conference_apc_mem2010
 
Caching and tuning fun for high scalability @ PHPTour
Caching and tuning fun for high scalability @ PHPTourCaching and tuning fun for high scalability @ PHPTour
Caching and tuning fun for high scalability @ PHPTour
 
Performance and Scalability
Performance and ScalabilityPerformance and Scalability
Performance and Scalability
 
MySQL Performance - SydPHP October 2011
MySQL Performance - SydPHP October 2011MySQL Performance - SydPHP October 2011
MySQL Performance - SydPHP October 2011
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

Caching for Cash: Caching