Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Memcached, presented to LCA2010

2,085 views

Published on

Published in: Technology
  • Be the first to comment

Memcached, presented to LCA2010

  1. 1. Introduction to Memcached Mark Atwood <matwood@gear6.com>
  2. 2. What is it? •open source •high performance •distributed •memory •object •cache : Copyright 2009 Gear6 Inc.
  3. 3. How we got here •persistent app service processes •local variables •filesystem caching •database query caching •shared memory segment caching •apache server fragment caching None of these scale OUT : Copyright 2009 Gear6 Inc.
  4. 4. Scale out • Multiple machines access the same memcache • Multiple machines running the memcache • Buy more machines, not bigger machines • Each memcache node is a hash table • Memcache client library does consistent hashing » looks at key » computes hash » picks node : Copyright 2009 Gear6 Inc.
  5. 5. What about the MySQL query cache? • A hack that makes poorly written PHP actually work • Heavy write load -> Fail • Heavy load -> Fail • Increasing CPU count -> Fail • Database server memory is precious, better spent on innodb buffer pool : Copyright 2009 Gear6 Inc.
  6. 6. Where Memcached Fits Web Stack Net Interface Storage Clients PHP, Java, Rails, C, Internet Database Apache, Nginx, Storage Interface: Servers Servers Perl, Python PostgreSQL MySQL, Web App Proxy CDN Load client cache Storage cache Balancer Cache Services memcached : Copyright 2009 Gear6 Inc.
  7. 7. Memcached: Technical Background • Written in C • DHT (Distributed Hash Table) Memcached • 2 Stage Protocol Handler » Clients: hash key  server » Server: hash key  value • Slab Allocator, fast Hash • Eviction algorithm: LRU • Supported clients » Java, PHP, Ruby, Perl, Python, C, etc Slab Manager • Key max size: max 250 bytes • Object max size: 1 MiB Item Manager • BSD like License : Copyright 2009 Gear6 Inc.
  8. 8. Application Changes Application Requirements: » Modify calls to subsequent application logic or database by use of memcached client » Send requests to memcached server » Check application or db layer next » Update memcached Cons: » Requires coding » Generic Pros: » Some app frameworks already » Performance gains » Back-end complexity reduction » Generic Source: MySQL.com : Copyright 2009 Gear6 Inc.
  9. 9. When coding, think of places to use it • In Perl & Python, when you have a big dictionary / hash to keep track of work • Or a highly used big lookup table. • There are modules to “tie” memcache to your language’s dictionary API • If you are in C, and are wishing for good dictionary • To share state between processes : Copyright 2009 Gear6 Inc.
  10. 10. Pattern: sessions • Keep session state in the memcache • Turnkey for PHP and for Django » pecl install pecl/memcache » Enable memcache session handler support? [yes] : yes • Win! Avoid sticky HTTP load balancing • Win! Avoid hot session table in database » often the #1 performance hotspot for web apps : Copyright 2009 Gear6 Inc.
  11. 11. Pattern: rate limiting • Limit access speed » to login attempts » to page accesses » to API calls » to expensive operations • Key of $prefix.$sourceip.$currtime • Value of access count • Use ADD method to update • Use MGET method to retrieve • Use expiration to delete : Copyright 2009 Gear6 Inc.
  12. 12. Pattern: SQL query cache • On every SQL query • SQL statement is the key • Result set is the value • Use expire to delete • Beware of inconsistancy, but it may be ok : Copyright 2009 Gear6 Inc.
  13. 13. MySQL User Defined Functions • Access memcached from inside queries • And from inside stored procedures • Can implement the “check cache, then read from table” pattern from inside the SQL query, without having to rewrite the app • Ships with Drizzle DB : Copyright 2009 Gear6 Inc.
  14. 14. Other things to keep in memcache • HTML fragments • Image thumbnails • Message headers & bodies • Unread message list • Unread message counter • Contact list • Work queues • API keys • Nonces • Gearman staged work • Remote data fetched over the web • Structured file read cache (Wafflegrid) : Copyright 2009 Gear6 Inc.
  15. 15. Thank you! Mark Atwood Director of Community Development <matwood@gear6.com> : Copyright 2009 Gear6 Inc.

×