SlideShare a Scribd company logo
MEMCACHED: WHAT IS IT
                AND WHAT DOES IT DO?
                                        Brian Moon
                                       dealnews.com
                                http://brian.moonspot.net/




Wednesday, September 30, 2009
@BRIANLMOON
                • Senior Web Engineer for dealnews.com
                  • Survived a 2006 Yahoo front page link
                • Founder and lead developer of Phorum
                • Memcached community member
                • Gearmand contributor
                • PHP internals contributor
                • I used PHP/FI

Wednesday, September 30, 2009
WHAT IS MEMCACHED?
       memcached is a high-performance, distributed
       memory object caching system, generic in nature, but
       intended for use in speeding up dynamic web
       applications by alleviating database load.
                •      Dumb daemon
                •      It is a generic key/data storage system
                •      Uses libevent and epoll/kqueue
                •      Caches data in memory
                •      Cache is distributed by the smart clients

Wednesday, September 30, 2009
PHP OPTIONS
                • PECL/memcache
                  • Mature
                  • Standalone
                  • More hand holding
                • PECL/memcached
                  • Built on libmemcached
                  • More of a raw API
                  • Has more features
Wednesday, September 30, 2009
SIMPLE PHP EXAMPLE
   $MEMCACHE = new Memcache();
   $MEMCACHE->addServer(“192.168.0.1”);
   $MEMCACHE->addServer(“192.168.0.2”);

   $mydata = $MEMCACHE->get(“mydata”);

   if($mydata === false){
       $mydata = generate_mydata();
       $MEMCACHE->set(“mydata”, $mydata,
           MEMCACHE_COMPRESSED,
           86400);
   }
   echo $mydata;

Wednesday, September 30, 2009
WHERE IS MY DATA?
                • The client (not server) uses a hashing algorithm to
                       determine the storage server
                •      Data is sent to only one server
                •      Servers do not share data
                •      Data is not replicated
                •      Two hashing algorithms possible:
                       • Traditional
                       • “Consistent”

Wednesday, September 30, 2009
WHERE IS MY DATA?
                • Both hash the key and use the result to choose a server.
                • Traditional hashing uses the forumla:
                  • hash % num_servers
                  • Resulting number determines the server used.
                • In “Consistent” hashing, each server is allocated LOTS of
                       slots and a key is hashed and to a number. The closest
                       slot to that number is the server.
                       • Adding/removing servers from the list results in less
                                key reassignment.

Wednesday, September 30, 2009
WHAT CAN I STORE?
                •      Server stores blobs of data up to 1MB
                •      PHP extensions will serialize non-scalar data
                •      Keys are limited to 250 bytes in length
                •      Keys can not contain spaces or “high” characters. Stick
                       with letters, numbers, _ and you are pretty safe.
                • PECL/memcache will convert keys for you.
                • PECL/memcached will returns false and an additional
                       method must be used to find out why the set failed.


Wednesday, September 30, 2009
DATA SIZE MATTERS
                • Maximum size for one item is 1MB
                • Both clients support compression, neither by default
                • Data is stored in slabs based on size
                  • Lots of items of the same size is not optimal
                  • Slab size can be customized
                  • May not be able to store items when it appears
                                there is “free” memory
                       • Data can be evicted sooner than expected.

Wednesday, September 30, 2009
EVICTION AND EXPIRATION
                • Expiration time can be expressed as seconds from now
                       or as an absolute epoch time.
                       • Values > 30 days are assumed to be an absolute time
                       • Items are not removed from memory when they
                                expire
                • Items are evicted when newer items need to be stored
                  • Least Recenty Used (LRU) determines what is
                                evicted
                       • Eviction is done per slab
Wednesday, September 30, 2009
HOW WELL IS IT WORKING?
                • Graph stats from memcached using Cacti/Ganglia, etc.
                • Key stats:          STAT cmd_get 4507207
                  • Hits/Misses       STAT cmd_set 1098829
                                      STAT get_hits 3221599
                  • Gets/Sets         STAT get_misses 1285608
                                      STAT evictions 0
                  • Evictions
                  • Cacti Templates: http://dealnews.com/developers/



Wednesday, September 30, 2009
HOW DO I SEE THE CACHE?

                • You have no way to see the cached data.
                • You probably don’t need to see it.
                • For memcached to tell you, it would freeze your entire
                       caching system
                • There are debug ways to see.
                • DO NOT COMPILE PRODUCTION WITH DEBUG
                       BECAUSE YOU ARE A CONTROL FREAK!



Wednesday, September 30, 2009
HOW DO I BACK IT UP?


                •You don’t!
                • If you application requires that, you are using it wrong
                • It is a cache, not a data storage system




Wednesday, September 30, 2009
NAMESPACES & TAGGING

                • There is no concept of namespaces or tagging built in
                       to memcached
                • You can simulate them with an extra key storage
                • See the FAQ for an example of simulated namespaces
                • This of course means there is no mass delete in
                       memcached



Wednesday, September 30, 2009
ADVANCED TIPS
                • Use multi-gets to increase performance
                  • PECL/memcache takes an array of keys to get()
                  • PECL/memcached has a separate method
                • Use the binary protocol in PECL/memcached
                • Group keys with a master key
                • Use a cache hierarchy
                  • GLOBALS + APC + memcached

Wednesday, September 30, 2009
REFERENCES

                • http://code.google.com/p/memcached/
                • http://pecl.php.net/package/memcache
                • http://pecl.php.net/package/memcached

                • http://brian.moonspot.net/
                • http://dealnews.com/developers/


Wednesday, September 30, 2009

More Related Content

KEY
Memcached: What is it and what does it do?
PDF
Improving Website Performance and Scalability with Memcached
PPTX
Using memcache to improve php performance
PPTX
Improving Website Performance with Memecached Webinar | Achieve Internet
PPT
Memcache
ODP
Cassandra as Memcache
PDF
Memcached Presentation
PDF
Implementing High Availability Caching with Memcached
Memcached: What is it and what does it do?
Improving Website Performance and Scalability with Memcached
Using memcache to improve php performance
Improving Website Performance with Memecached Webinar | Achieve Internet
Memcache
Cassandra as Memcache
Memcached Presentation
Implementing High Availability Caching with Memcached

What's hot (20)

PPT
Roy foubister (hosting high traffic sites on a tight budget)
PDF
Moxi - Memcached Proxy
PPTX
Presentation1
PPT
Simple Site Speed Improvements (SMX 2010)
PPTX
Joomla! Performance on Steroids
PDF
High Performance - Joomla!Days NL 2009 #jd09nl
PDF
Memcached Code Camp 2009
PPTX
High performance WordPress
PDF
Wordpress optimization
PPTX
PDF
Caching with Varnish
PDF
WordCamp RVA 2011 - Performance & Tuning
PDF
23 Ways To Speed Up WordPress
PDF
High Performance Drupal Sites
PDF
Caching with Memcached and APC
PDF
Memcached Study
PPTX
캐시 분산처리 인프라
PPTX
How to reduce database load using Memcache
PPTX
Performance all teh things
PDF
Optimizing wp
Roy foubister (hosting high traffic sites on a tight budget)
Moxi - Memcached Proxy
Presentation1
Simple Site Speed Improvements (SMX 2010)
Joomla! Performance on Steroids
High Performance - Joomla!Days NL 2009 #jd09nl
Memcached Code Camp 2009
High performance WordPress
Wordpress optimization
Caching with Varnish
WordCamp RVA 2011 - Performance & Tuning
23 Ways To Speed Up WordPress
High Performance Drupal Sites
Caching with Memcached and APC
Memcached Study
캐시 분산처리 인프라
How to reduce database load using Memcache
Performance all teh things
Optimizing wp
Ad

Similar to Memcached: What is it and what does it do? (PHP Version) (20)

PDF
Plugin Memcached%20 Study
PDF
Memcached
PDF
Give Your Site a Boost with Memcache
PDF
Mysql wp memcached
PDF
All The Little Pieces
PDF
Mysql wp memcached
PDF
Caching for Cash: Caching
PPTX
memcached Distributed Cache
KEY
Introduction to memcached
PDF
Caching objects-in-memory
PDF
All The Little Pieces
PDF
No sql presentation
PDF
APC & Memcache the High Performance Duo
PDF
Barcelona apc mem2010
PDF
Scaling Rails with memcached
DOC
No sql exploration keyvaluestore
ODP
Caching and tuning fun for high scalability
ZIP
All The Little Pieces
PPTX
Memcache
PPTX
Caching
Plugin Memcached%20 Study
Memcached
Give Your Site a Boost with Memcache
Mysql wp memcached
All The Little Pieces
Mysql wp memcached
Caching for Cash: Caching
memcached Distributed Cache
Introduction to memcached
Caching objects-in-memory
All The Little Pieces
No sql presentation
APC & Memcache the High Performance Duo
Barcelona apc mem2010
Scaling Rails with memcached
No sql exploration keyvaluestore
Caching and tuning fun for high scalability
All The Little Pieces
Memcache
Caching
Ad

Recently uploaded (20)

PDF
Machine learning based COVID-19 study performance prediction
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Cloud computing and distributed systems.
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
cuic standard and advanced reporting.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Modernizing your data center with Dell and AMD
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Electronic commerce courselecture one. Pdf
Machine learning based COVID-19 study performance prediction
Digital-Transformation-Roadmap-for-Companies.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Cloud computing and distributed systems.
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Network Security Unit 5.pdf for BCA BBA.
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
MYSQL Presentation for SQL database connectivity
cuic standard and advanced reporting.pdf
Understanding_Digital_Forensics_Presentation.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Modernizing your data center with Dell and AMD
The AUB Centre for AI in Media Proposal.docx
20250228 LYD VKU AI Blended-Learning.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Chapter 3 Spatial Domain Image Processing.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Electronic commerce courselecture one. Pdf

Memcached: What is it and what does it do? (PHP Version)

  • 1. MEMCACHED: WHAT IS IT AND WHAT DOES IT DO? Brian Moon dealnews.com http://brian.moonspot.net/ Wednesday, September 30, 2009
  • 2. @BRIANLMOON • Senior Web Engineer for dealnews.com • Survived a 2006 Yahoo front page link • Founder and lead developer of Phorum • Memcached community member • Gearmand contributor • PHP internals contributor • I used PHP/FI Wednesday, September 30, 2009
  • 3. WHAT IS MEMCACHED? memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load. • Dumb daemon • It is a generic key/data storage system • Uses libevent and epoll/kqueue • Caches data in memory • Cache is distributed by the smart clients Wednesday, September 30, 2009
  • 4. PHP OPTIONS • PECL/memcache • Mature • Standalone • More hand holding • PECL/memcached • Built on libmemcached • More of a raw API • Has more features Wednesday, September 30, 2009
  • 5. SIMPLE PHP EXAMPLE $MEMCACHE = new Memcache(); $MEMCACHE->addServer(“192.168.0.1”); $MEMCACHE->addServer(“192.168.0.2”); $mydata = $MEMCACHE->get(“mydata”); if($mydata === false){ $mydata = generate_mydata(); $MEMCACHE->set(“mydata”, $mydata, MEMCACHE_COMPRESSED, 86400); } echo $mydata; Wednesday, September 30, 2009
  • 6. WHERE IS MY DATA? • The client (not server) uses a hashing algorithm to determine the storage server • Data is sent to only one server • Servers do not share data • Data is not replicated • Two hashing algorithms possible: • Traditional • “Consistent” Wednesday, September 30, 2009
  • 7. WHERE IS MY DATA? • Both hash the key and use the result to choose a server. • Traditional hashing uses the forumla: • hash % num_servers • Resulting number determines the server used. • In “Consistent” hashing, each server is allocated LOTS of slots and a key is hashed and to a number. The closest slot to that number is the server. • Adding/removing servers from the list results in less key reassignment. Wednesday, September 30, 2009
  • 8. WHAT CAN I STORE? • Server stores blobs of data up to 1MB • PHP extensions will serialize non-scalar data • Keys are limited to 250 bytes in length • Keys can not contain spaces or “high” characters. Stick with letters, numbers, _ and you are pretty safe. • PECL/memcache will convert keys for you. • PECL/memcached will returns false and an additional method must be used to find out why the set failed. Wednesday, September 30, 2009
  • 9. DATA SIZE MATTERS • Maximum size for one item is 1MB • Both clients support compression, neither by default • Data is stored in slabs based on size • Lots of items of the same size is not optimal • Slab size can be customized • May not be able to store items when it appears there is “free” memory • Data can be evicted sooner than expected. Wednesday, September 30, 2009
  • 10. EVICTION AND EXPIRATION • Expiration time can be expressed as seconds from now or as an absolute epoch time. • Values > 30 days are assumed to be an absolute time • Items are not removed from memory when they expire • Items are evicted when newer items need to be stored • Least Recenty Used (LRU) determines what is evicted • Eviction is done per slab Wednesday, September 30, 2009
  • 11. HOW WELL IS IT WORKING? • Graph stats from memcached using Cacti/Ganglia, etc. • Key stats: STAT cmd_get 4507207 • Hits/Misses STAT cmd_set 1098829 STAT get_hits 3221599 • Gets/Sets STAT get_misses 1285608 STAT evictions 0 • Evictions • Cacti Templates: http://dealnews.com/developers/ Wednesday, September 30, 2009
  • 12. HOW DO I SEE THE CACHE? • You have no way to see the cached data. • You probably don’t need to see it. • For memcached to tell you, it would freeze your entire caching system • There are debug ways to see. • DO NOT COMPILE PRODUCTION WITH DEBUG BECAUSE YOU ARE A CONTROL FREAK! Wednesday, September 30, 2009
  • 13. HOW DO I BACK IT UP? •You don’t! • If you application requires that, you are using it wrong • It is a cache, not a data storage system Wednesday, September 30, 2009
  • 14. NAMESPACES & TAGGING • There is no concept of namespaces or tagging built in to memcached • You can simulate them with an extra key storage • See the FAQ for an example of simulated namespaces • This of course means there is no mass delete in memcached Wednesday, September 30, 2009
  • 15. ADVANCED TIPS • Use multi-gets to increase performance • PECL/memcache takes an array of keys to get() • PECL/memcached has a separate method • Use the binary protocol in PECL/memcached • Group keys with a master key • Use a cache hierarchy • GLOBALS + APC + memcached Wednesday, September 30, 2009
  • 16. REFERENCES • http://code.google.com/p/memcached/ • http://pecl.php.net/package/memcache • http://pecl.php.net/package/memcached • http://brian.moonspot.net/ • http://dealnews.com/developers/ Wednesday, September 30, 2009