Download - Caching with Memcached and APC

Transcript
Page 1: Caching with Memcached and APC

Caching with Memcached & APC

Ben RamseyTEK·X • May 21, 2010

Page 3: Caching with Memcached and APC

What is a cache?

Page 4: Caching with Memcached and APC

“A cache is a collection of data duplicating original values stored elsewhere or computed earlier, where the original data is expensive to fetch (owing to longer access time) or to compute, compared to the cost of reading the cache.”

–Wikipedia

Page 5: Caching with Memcached and APC

“A cache is a temporary storage area where frequently accessed data can be stored for rapid access.”

Page 6: Caching with Memcached and APC

Why use a cache?

Page 7: Caching with Memcached and APC

• To reduce the number or retrieval queries made to a database

• To reduce the number of requests made to external services

• To reduce the time spent computing data

• To reduce filesystem access

Page 8: Caching with Memcached and APC

Types of caches

Page 9: Caching with Memcached and APC

• File system

• Database

• Shared memory

• RAM disk

• Object cache (memcached and APC)

• Opcode cache (APC)

Page 10: Caching with Memcached and APC

Memcached

Page 11: Caching with Memcached and APC

What is memcached?

• Distributed memory object caching

• Acts as a simple key/value dictionary

• Runs as a daemon

• Has a simple protocol for client access over TCP and UDP

• Can be run in a pool, but individual daemons are not aware of the pool

• Clients/applications manage the pool

• Not an opcode cache

Page 12: Caching with Memcached and APC

Who uses memcached?

• Facebook

• Digg

• Youtube

• Wikipedia

• Us (Moontoast)

• Many others...

Page 13: Caching with Memcached and APC

Memcached principles

• Fast asynchronous network I/O

• Not a persistent data store

• It does not provide redundancy

• Data is not replicated across the cluster

• It doesn’t handle failover

Page 14: Caching with Memcached and APC

Memcached principles

• Daemons are not aware of each other

• It does not provide authentication

• Works great on a small and local-area network

• A single value cannot contain more than 1MB of data

• Keys are strings limited to 250 characters

Page 15: Caching with Memcached and APC

Basic concepts and usage

1. Set up a pool of memcached servers

2. Assign values to keys that are stored in the cluster

3. The client hashes the key to a particular machine in the cluster

4. Subsequent requests for that key retrieve the value from the memcached server on which it was stored

5. Values time out after the specified TTL

Page 16: Caching with Memcached and APC

memcached

memcached

memcached

www

www

www

Page 17: Caching with Memcached and APC

memcached

memcached

memcached

www

www

www

Page 18: Caching with Memcached and APC

A very simple protocol

• Storage commands:set, add, replace, append, prepend, cas

• Retrieval command: get, gets

• Deletion command: delete

• Increment/decrement: incr, decr

• Other commands: stats, flush_all, version, verbosity, quit

Page 19: Caching with Memcached and APC

$> telnet localhost 11211Trying ::1...Connected to localhost.Escape character is '^]'.set foobar 0 0 15This is a test.STOREDget foobarVALUE foobar 0 15This is a test.ENDquitConnection closed by foreign host.$>

Page 20: Caching with Memcached and APC

Let’s see that with some code.

Page 21: Caching with Memcached and APC

$memcache = new Memcached();$memcache->addServers(array( array('10.35.24.1', '11211'), array('10.35.24.2', '11211'), array('10.35.24.3', '11211'),));

Page 22: Caching with Memcached and APC

$book = $memcache->get('0764596349');

if ($book === false){ if ($memcache->getResultCode() == Memcached::RES_NOTFOUND) { $book = Book::getByIsbn('0764596349'); $memcache->set($book->getCacheKey(), $book); }}

Page 23: Caching with Memcached and APC

Memcached limits

• Key size has a 250 byte limit

• Value can not be larger than 1 MB

• Memory limits for 32bit/64bit systems

• Replication not built-in; dependent on the client

Page 24: Caching with Memcached and APC

pecl/memcached

Page 25: Caching with Memcached and APC

pecl/memcached basics

• PHP extension based on the libmemcached C client library

• Andrei Zmievski authored the extension

• Now at a stable version 1.0.2

• http://php.net/memcached

Page 26: Caching with Memcached and APC

Settings and configuration

• Memcached::OPT_COMPRESSION

• Memcached::OPT_DISTRIBUTION

• Memcached::OPT_LIBKETAMA_COMPATIBLE

• Memcached::OPT_BINARY_PROTOCOL

• Memcached::OPT_NO_BLOCK

Page 27: Caching with Memcached and APC

Memcached methods

• add()

• addServer()

• append()

• cas()

• decrement()

• delete()

• get()

• getMulti()

• getResultCode()

• getResultMessage()

• getServerList()

• getStats()

• increment()

• prepend()

• replace()

• set()

• setMulti()

• ... and more!

Page 28: Caching with Memcached and APC

Alternative PHP Cache (APC)

Page 29: Caching with Memcached and APC

apc

apc

apc

www

www

www

Page 30: Caching with Memcached and APC

What is APC?

• Opcode cache

• Provides object caching (also referred to in places as “APC user variables”)

• Gives information about file upload progress

• Stores to local, shared memory

• Not distributed

• http://php.net/apc

Page 31: Caching with Memcached and APC

Basic concepts and usage

• For opcode caching, just install the extension and turn it on: apc.enabled=1

• For memory allocation, change apc.shm_size; by default, it’s 30 MB

• To speed things up even more, turn off apc.stat (set to 0)

• “Set and forget” … but it also does object storage

Page 32: Caching with Memcached and APC

Settings and configuration

• apc.shm_size – Determines how much memory is allocated to APC

• apc.stat – Determines whether APC will check if a file has been modified on every request

• apc.ttl – Leaving at zero means APC could potentially fill up with stale entries while newer ones won’t be cached; if greater than zero, APC will attempt to remove expired entries

Page 33: Caching with Memcached and APC

How does opcode caching work?

Page 34: Caching with Memcached and APC

example.com/ index.php

public/index.phplibrary/Zend/Application.phplibrary/Zend/Loader/Autoloader.phplibrary/Zend/Loader.phplibrary/Zend/Config/Ini.phplibrary/Zend/Config.phpapplication/Bootstrap.phplibrary/Zend/Application/Bootstrap/Bootstrap.phplibrary/Zend/Application/Bootstrap/BootstrapAbstract.phplibrary/Zend/Application/Bootstrap/Bootstrapper.phplibrary/Zend/Application/Bootstrap/ResourceBootstrapper.phplibrary/Zend/Application/Module/Autoloader.phplibrary/Zend/Loader/Autoloader/Resource.phplibrary/Zend/Loader/Autoloader/Interface.phplibrary/Zend/Loader/PluginLoader.php... 48 more files ...

APC

Page 35: Caching with Memcached and APC

example.com/ index.php

public/index.phplibrary/Zend/Application.phplibrary/Zend/Loader/Autoloader.phplibrary/Zend/Loader.phplibrary/Zend/Config/Ini.phplibrary/Zend/Config.phpapplication/Bootstrap.phplibrary/Zend/Application/Bootstrap/Bootstrap.phplibrary/Zend/Application/Bootstrap/BootstrapAbstract.phplibrary/Zend/Application/Bootstrap/Bootstrapper.phplibrary/Zend/Application/Bootstrap/ResourceBootstrapper.phplibrary/Zend/Application/Module/Autoloader.phplibrary/Zend/Loader/Autoloader/Resource.phplibrary/Zend/Loader/Autoloader/Interface.phplibrary/Zend/Loader/PluginLoader.php... 48 more files ...

APC

Page 36: Caching with Memcached and APC

database

language key translationen HELLO Hellofr HELLO Bonjoures HELLO Holade HELLO Hallonl HELLO Hallofi HELLO Hei

ga HELLO Dia duitpt HELLO Olá... ... ...... ... ...... ... ...

background process

en.php

/welcome

Magic!

Page 37: Caching with Memcached and APC

Even better:store the array to APC with apc_store()!

Page 38: Caching with Memcached and APC

APC object storage

Page 39: Caching with Memcached and APC

if (($book = apc_fetch('0764596349')) === false){ $book = Book::getByIsbn('0764596349'); apc_store($book->getCacheKey(), $book);}

Page 40: Caching with Memcached and APC

• apc_cache_info()

• apc_clear_cache()

• apc_sma_info()

• apc_store()

• apc_fetch()

• apc_delete()

• apc_delete_file()

• apc_compile_file()

• apc_define_constants()

• apc_load_constants()

• apc_add()

• apc_inc()

• apc_dec()

• apc_cas()

• apc_bin_dump()

• apc_bin_load()

• apc_bin_dumpfile()

• apc_bin_loadfile()

APC functions

Page 41: Caching with Memcached and APC

• apc_compile_file()

• apc_bin_dump()

• apc_bin_load()

• apc_bin_dumpfile()

• apc_bin_loadfile()

Advanced APC

Page 42: Caching with Memcached and APC

Memcachedvs. APC

Page 43: Caching with Memcached and APC

When should you use memcached?

• When requests aren’t guaranteed to always go to the same machine

• Data is specific or targeted to a user

• User sessions

Page 44: Caching with Memcached and APC

When should you use APC?

• Application settings

• Configuration

• Data that is the same for each user

• Requests are guaranteed to go to the same machine (i.e. sticky sessions)

• File upload progress & sessions (if using sticky sessions)

Page 45: Caching with Memcached and APC

Why not use both?

• Create a caching adapter for a uniform caching interface and decide where to store at the app level or even dynamically at runtime

• Use APC for things it’s good at and memcached for things it’s good at

Page 46: Caching with Memcached and APC

Questions?

Page 47: Caching with Memcached and APC

Thank you!

Ben [email protected]/1599