Memcached, presented to LCA2010


Published on

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Support the Scaled Web stack
  • Web 2.0 driving user & traffic growth

    Key Apps
    : Entertainment, Communication, Social Networking

    Key Drivers
    : Mobile, Broadband, International, Consumer

    Mobile applications – 3G+ is 22% by 2010 (Mobile goes hi speed)

    Consumer Internet Traffic 2006–2012 This category encompasses any IP traffic that crosses the Internet and is not confined to a single service provider’s network. Peer-to-peer (P2P) traffic, still the largest share of Internet traffic today, will decrease as a percentage of overall Internet traffic. Internet video streaming and downloads are beginning to take a larger share of bandwidth, and will grow to nearly 50 percent of all consumer Internet traffic in 2012.

    Fortunately the web stack has evolved. OSS Technologies such as Varnish (
    high-performance HTTP accelerator… reverse proxy), gearman (open source message queuing system that makes it easy to do distributed job processing using multiple languages… function dispatcher) and hadoop (distributed computing platform)

    Otherwise there would be some hard decisions

    These horizontal architectures require a distributed caching system at SCALE (for dynamic data… video can be cached at the edge)

    The alternatives in the earlier part of this decade was
    to scale up and put more memory in the application and database servers
  • connections, operations
  • Memcached, presented to LCA2010

    1. 1. Introduction to Memcached Mark Atwood <>
    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: : 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 <> : Copyright 2009 Gear6 Inc.