Caching for Cash: Caching

35
CACHING FOR CASH - CACHING Scott MacVicar php|works 2008

Transcript of Caching for Cash: Caching

Page 1: Caching for Cash: Caching

CACHING FOR CASH - CACHING

Scott MacVicarphp|works 2008

Page 2: Caching for Cash: Caching

WHY CACHE?

Reduce memory usage

Reduce processing time

Reduce network traffic

Reduce disk access

Page 3: Caching for Cash: Caching

CACHING OPTIONS

Flat file

RAM disk

Database

Memcache

APC

Script level

Page 4: Caching for Cash: Caching

WHAT TO CACHE?

External resources

Large result sets

Static data

Sessions

Page 5: Caching for Cash: Caching

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

Page 6: Caching for Cash: Caching

CACHING FILES

PEAR::Cache_Lite

Zend_Cache in Zend Framework

var_export in PHP

Page 7: Caching for Cash: Caching

RAM DISK

Mount some memory as disk space

Used when IO actions can’t be avoided

Non resizable on the fly

Page 8: Caching for Cash: Caching

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

Page 9: Caching for Cash: Caching

MEMCACHE

Origins within Livejournal

Used by Facebook, Youtube, Wikipedia and digg

Caching daemon, no persistence

Stores key / value pairs

Page 10: Caching for Cash: Caching

FACEBOOK EXAMPLE

200 dedicated memcache servers

Each 16GB quad-core amd64

3TB memcache data

Page 11: Caching for Cash: Caching

MEMCACHE

Slab Allocator

Libevent based (non-blocking)

Simple Protocol

Server is just a hash table

No authentication or self awareness

Page 12: Caching for Cash: Caching

MEMCACHE CLIENT

Clients provide key and value

Responsible for serialising any value

Compress data

Page 13: Caching for Cash: Caching

SERVER PROTOCOL

set/replace/add

get

append/prepend

increment/decrement

compare and swap

Page 14: Caching for Cash: Caching

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

Page 15: Caching for Cash: Caching

MEMCACHE LIMITS

Key Size is 250 bytes

Data Size is 1 megabyte

32bit/64bit memory limit

No Replication across cluster

Page 16: Caching for Cash: Caching

MEMCACHE CLEANING UP

Uses Least Recently Used Algorithm

Looks for the oldest item and removes

TTL also applies to key / value pairs

Page 17: Caching for Cash: Caching

SIMPLE USAGE

Fetch from memcache

If there return

Else calculate, store in cache and return

Page 18: Caching for Cash: Caching

WHERE TO RUN?

Any server works

No need for matching size

Low CPU usage

Page 19: Caching for Cash: Caching

MEMCACHE + PHP

PECL extension using custom connection

pecl install memcache

Provides MemcachePool and Memcache

INI settings to change hash strategy, function and protocol.

Page 21: Caching for Cash: Caching

MEMCACHE ACTIONS

Add - only if the key doesn’t exist

Set - will add or update key

Replace - only if the key already exists

Page 22: Caching for Cash: Caching

MEMCACHE TASK

Open Terminal in VMWare Image and runsudo service memcached start

Try using memcache to set, get and add values

Try setting a value using the Time To Live parameter

Ask questions :-)

Page 23: Caching for Cash: Caching

MEMCACHEDB

Persistent key/value storage system

BDB used for persitence

Compatible with Memcache protocol

Page 24: Caching for Cash: Caching

MEMCACHEDB CAVEATS

No expiration

Isn’t a replacement for memcache

Page 25: Caching for Cash: Caching

APC

Provides OPCode caching in PHP

Shared memory for storage

APC is only on the local server

No network overhead from TCP/IP

Page 26: Caching for Cash: Caching

APC

Tokenizer

Parser

Compiler

Executor

APC

Input

Store Opcodes

Page 28: Caching for Cash: Caching

CACHE LAYERING

APC Memcache Disk / Database

Fetch from APC locally

Fetch from Memcache, add to APC

Finally fetch from the original source and store back in Memcache

Page 29: Caching for Cash: Caching

APC USE CASES

Should always use it to cache opcode of files

Small but frequently accessed things

You only have one server

Page 30: Caching for Cash: Caching

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

Page 31: Caching for Cash: Caching

SQLITE

Local node storage of filesystem

Cache result sets from remote database

In memory database

Page 32: Caching for Cash: Caching

DATABASE QUERY CACHING

Rely on MySQL to do the query caching

Doesn’t quite work though :-(

Invalidation of cache happens easily

Page 33: Caching for Cash: Caching

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?

Page 34: Caching for Cash: Caching

CACHING TIPS

Pre-heat the cache

Use multiple levels of cache

Cache even dynamic data for short times

serialize() is slow

Page 35: Caching for Cash: Caching

QUESTIONS?