Memcached, presented to LCA2010
Upcoming SlideShare
Loading in...5

Memcached, presented to LCA2010






Total Views
Views on SlideShare
Embed Views



3 Embeds 8 5 2 1



Upload Details

Uploaded via as Apple Keynote

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.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment
  • Support the Scaled Web stack
  • Web 2.0 driving user & traffic growth <br /> <br /> <br /> <br /> Key Apps <br /> : Entertainment, Communication, Social Networking <br /> <br /> <br /> <br /> Key Drivers <br /> : Mobile, Broadband, International, Consumer <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> Mobile applications &#x2013; 3G+ is 22% by 2010 (Mobile goes hi speed) <br /> <br /> <br /> <br /> Consumer Internet Traffic 2006&#x2013;2012 This category encompasses any IP traffic that crosses the Internet and is not confined to a single service provider&#x2019;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. <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> Fortunately the web stack has evolved. OSS Technologies such as Varnish ( <br /> high-performance HTTP accelerator&#x2026; reverse proxy), gearman (open source message queuing system that makes it easy to do distributed job processing using multiple languages&#x2026; function dispatcher) and hadoop (distributed computing platform) <br /> <br /> <br /> <br /> <br /> <br /> <br /> Otherwise there would be some hard decisions <br /> <br /> <br /> <br /> <br /> <br /> <br /> These horizontal architectures require a distributed caching system at SCALE (for dynamic data&#x2026; video can be cached at the edge) <br /> <br /> <br /> <br /> <br /> <br /> <br /> The alternatives in the earlier part of this decade was <br /> to scale up and put more memory in the application and database servers
  • connections, operations

Memcached, presented to LCA2010 Memcached, presented to LCA2010 Presentation Transcript

  • Introduction to Memcached Mark Atwood <>
  • What is it? •open source •high performance •distributed •memory •object •cache : Copyright 2009 Gear6 Inc.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • Thank you! Mark Atwood Director of Community Development <> : Copyright 2009 Gear6 Inc.