Caching with Memcached and APC
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Caching with Memcached and APC

on

  • 23,013 views

Today's high-traffic web sites must implement performance-boosting measures that reduce data processing and reduce load on the database, while increasing the speed of content delivery. One such method ...

Today's high-traffic web sites must implement performance-boosting measures that reduce data processing and reduce load on the database, while increasing the speed of content delivery. One such method is the use of a cache to temporarily store whole pages, database recordsets, large objects, and sessions. While many caching mechanisms exist, memcached provides one of the fastest and easiest-to-use caching servers. Coupling memcached with the alternative PHP cache (APC) can greatly improve performance by reducing data processing time. In this talk, Ben Ramsey covers memcached and the pecl/memcached and pecl/apc extensions for PHP, exploring caching strategies, a variety of configuration options to fine-tune your caching solution, and discusses when it may be appropriate to use memcached vs. APC to cache objects or data.

Statistics

Views

Total Views
23,013
Views on SlideShare
22,445
Embed Views
568

Actions

Likes
22
Downloads
413
Comments
3

26 Embeds 568

http://iroylabs.blogspot.com 276
http://www.slideshare.net 95
http://www.twinsky.com 69
http://blog.leog.in 39
http://iroylabs.blogspot.in 22
http://learn.codelesson.com 14
http://iroylabs.blogspot.de 8
http://zubril.in 7
http://twinsky.com 7
http://iroylabs.blogspot.com.es 5
http://iroylabs.blogspot.nl 4
http://iroylabs.blogspot.com.br 3
http://iroylabs.blogspot.ca 2
http://iroylabs.blogspot.co.uk 2
http://iroylabs.blogspot.sg 2
http://a0.twimg.com 2
http://iroylabs.blogspot.se 2
http://iroylabs.blogspot.dk 1
http://webcache.googleusercontent.com 1
http://iroylabs.blogspot.ro 1
http://iroylabs.blogspot.fr 1
http://iroylabs.blogspot.jp 1
http://iroylabs.blogspot.be 1
http://iroylabs.blogspot.kr 1
http://linyo.ws 1
http://iroylabs.blogspot.pt 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • thanks
    Are you sure you want to
    Your message goes here
    Processing…
  • thanks
    Are you sure you want to
    Your message goes here
    Processing…
  • Yes, you can use more than 1mb for values :
    http://code.google.com/p/memcached/wiki/FAQ#What_is_the_maximum_data_size_you_can_store?_%281_megabyte%29
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Caching with Memcached and APC Presentation Transcript

  • 1. Caching with Memcached & APC Ben Ramsey TEK·X • May 21, 2010
  • 2. Hi, I’m Ben. benramsey.com @ramsey joind.in/1599
  • 3. What is a cache?
  • 4. “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
  • 5. “A cache is a temporary storage area where frequently accessed data can be stored for rapid access.”
  • 6. Why use a cache?
  • 7. • 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
  • 8. Types of caches
  • 9. • File system • Database • Shared memory • RAM disk • Object cache (memcached and APC) • Opcode cache (APC)
  • 10. Memcached
  • 11. 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
  • 12. Who uses memcached? • Facebook • Digg • Youtube • Wikipedia • Us (Moontoast) • Many others...
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. memcached www www memcached memcached www
  • 17. memcached www www memcached memcached www
  • 18. 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
  • 19. $> telnet localhost 11211 Trying ::1... Connected to localhost. Escape character is '^]'. set foobar 0 0 15 This is a test. STORED get foobar VALUE foobar 0 15 This is a test. END quit Connection closed by foreign host. $>
  • 20. Let’s see that with some code.
  • 21. $memcache = new Memcached(); $memcache->addServers(array( array('10.35.24.1', '11211'), array('10.35.24.2', '11211'), array('10.35.24.3', '11211'), ));
  • 22. $book = $memcache->get('0764596349'); if ($book === false) { if ($memcache->getResultCode() == Memcached::RES_NOTFOUND) { $book = Book::getByIsbn('0764596349'); $memcache->set($book->getCacheKey(), $book); } }
  • 23. 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
  • 24. pecl/memcached
  • 25. 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
  • 26. Settings and configuration • Memcached::OPT_COMPRESSION • Memcached::OPT_DISTRIBUTION • Memcached::OPT_LIBKETAMA_COMPATIBLE • Memcached::OPT_BINARY_PROTOCOL • Memcached::OPT_NO_BLOCK
  • 27. Memcached methods • add() • getResultMessage() • addServer() • getServerList() • append() • getStats() • cas() • increment() • decrement() • prepend() • delete() • replace() • get() • set() • getMulti() • setMulti() • getResultCode() • ... and more!
  • 28. Alternative PHP Cache (APC)
  • 29. apc www www apc apc www
  • 30. 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
  • 31. 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
  • 32. 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
  • 33. How does opcode caching work?
  • 34. example.com/ index.php public/index.php library/Zend/Application.php library/Zend/Loader/Autoloader.php library/Zend/Loader.php library/Zend/Config/Ini.php library/Zend/Config.php application/Bootstrap.php library/Zend/Application/Bootstrap/Bootstrap.php APC library/Zend/Application/Bootstrap/BootstrapAbstract.php library/Zend/Application/Bootstrap/Bootstrapper.php library/Zend/Application/Bootstrap/ ResourceBootstrapper.php library/Zend/Application/Module/Autoloader.php library/Zend/Loader/Autoloader/Resource.php library/Zend/Loader/Autoloader/Interface.php library/Zend/Loader/PluginLoader.php ... 48 more files ...
  • 35. example.com/ index.php public/index.php library/Zend/Application.php library/Zend/Loader/Autoloader.php library/Zend/Loader.php library/Zend/Config/Ini.php library/Zend/Config.php application/Bootstrap.php library/Zend/Application/Bootstrap/Bootstrap.php APC library/Zend/Application/Bootstrap/BootstrapAbstract.php library/Zend/Application/Bootstrap/Bootstrapper.php library/Zend/Application/Bootstrap/ ResourceBootstrapper.php library/Zend/Application/Module/Autoloader.php library/Zend/Loader/Autoloader/Resource.php library/Zend/Loader/Autoloader/Interface.php library/Zend/Loader/PluginLoader.php ... 48 more files ...
  • 36. /welcome Magic! language key translation en HELLO Hello fr HELLO Bonjour es HELLO Hola en.php de HELLO Hallo nl HELLO Hallo fi HELLO Hei ga HELLO Dia duit pt HELLO Olá ... ... ... ... ... ... ... ... ... background data process base
  • 37. Even better: store the array to APC with apc_store()!
  • 38. APC object storage
  • 39. if (($book = apc_fetch('0764596349')) === false) { $book = Book::getByIsbn('0764596349'); apc_store($book->getCacheKey(), $book); }
  • 40. APC functions • apc_cache_info() • apc_load_constants() • apc_clear_cache() • apc_add() • apc_sma_info() • apc_inc() • apc_store() • apc_dec() • apc_fetch() • apc_cas() • apc_delete() • apc_bin_dump() • apc_delete_file() • apc_bin_load() • apc_compile_file() • apc_bin_dumpfile() • apc_define_constants() • apc_bin_loadfile()
  • 41. Advanced APC • apc_compile_file() • apc_bin_dump() • apc_bin_load() • apc_bin_dumpfile() • apc_bin_loadfile()
  • 42. Memcached vs. APC
  • 43. 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
  • 44. 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)
  • 45. 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
  • 46. Questions?
  • 47. Thank you! Ben Ramsey benramsey.com @ramsey joind.in/1599