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.
Memcache 
Rob Sharp 
rob@sharp.id.au 
Lead Developer 
The Sound Alliance
About Memcached 
• conceived by Brad Fitzpatrick as a solution 
to the scaling issues faced by Livejournal 
• “memcached i...
Who we are 
• Sydney-based online media publishing 
• Community and Content Sites 
• Fasterlouder.com.au 
• Inthemix.com.a...
Who we are 
• Inthemix.com.au 
• Australia’s busiest music website 
• ~ 250,000 pages per day 
• Plus two other busy sites...
Current Architechture 
• 3 Linux servers 
• Apache 
• Lighttpd 
• Memcache 
• 1 MySQL Master
Why do we use 
memcached? 
• CMS written in OO style, using 
Activerecord 
• PHP4 and objects don't mix too well 
• Active...
Our Application 
• CMS written from the ground up 
• Effectively three sites running on one 
codebase 
• Uses three sepera...
Our Memcache Setup 
• We have 3 webservers running memcache 
• Each server runs three daemons on 
separate ports - one for...
Memcache Pool 
• Each daemon knows about the other 2 
daemons and connects to them over TCP 
• This allows us to store dat...
Memcache Pool 
• But what if we lose a server? We can 
• Ignore it - we simply get misses for any 
keys we attempt to retr...
Memcache Pool 
• Consistent hashing will solve the problem 
of removing or adding servers once data 
has been hashed 
• Cu...
Installing Memcached 
• Available in most Linux distros 
• packaged for Fedora, RHEL4/5, Ubuntu, 
Debian, Gentoo and BSD 
...
Memcache and Ruby 
• We’ll use the memcache-client gem 
• Pure Ruby implementation 
• Pretty fast!
Storing Stuff 
rsharp$ sudo gem install memcache-client 
require 'memcache' 
memcache_options = { 
:compression => true, 
...
Storing Stuff 
Cache.set 'favourite_artist', 'Salvador Dali' 
skateboarder = Cache.get 'favourite_artist' 
Cache.delete 'f...
Memcache Namespaces 
• Memcache doesn’t have namespaces, so we 
have to improvise 
• Prefix your keys with a namespace by ...
Roll your own? 
• Memcache-client provides basic cache 
methods 
• What if we extended ActiveRecord? 
• We can, with activ...
Storing Stuff Part Deux 
rsharp$ sudo gem install cached_model 
require 'cached_model' 
memcache_options = { 
:compression...
Storing Stuff Part Deux 
class Artist < CachedModel 
end
cached_model 
Performance 
• CachedModel is not magic. 
• CachedModel only accelerates simple finds for 
single rows. 
• C...
Other options 
• acts_as_cached provides a similar solution
Memcache Storage 
• Memcache stores blobs 
• The memcache client handles marshalling, 
so you can easily cache objects 
• ...
Memcache Storage 
• The most obvious things to store are 
objects 
• We cache articles 
• We cache collections of articles...
What we cache 
• Our sites are fairly big and data-rich 
communities 
• Almost every page has editorially 
controlled attr...
Our Example Dataset 
• Article 
• Joins Artists 
• Joins Locations 
• Joins Genres 
• Joins Related Content 
• Joins Relat...
Our Example Dataset 
• Article (continues) 
• ... 
• Joins Media Content 
• Joins Comments 
• Joins ‘Rollcalls’ 
• Joins o...
Our Example 
• An article requires many data facets 
• Most don’t change that often 
• We also know when they change 
• Ya...
Our Example 
Summary 
• Data can be loosely divided into editorially 
controlled and user-generated 
• Cache editorially c...
Fragment Caching 
• Memcache allows timed expiry of 
fragments 
• Identify areas that change infrequently and 
cache 
• Re...
Caching Fragments 
rsharp$ sudo gem install memcache_fragments 
require 'memcache_fragments' 
memcache_options = { 
:compr...
Caching Fragments 
ActionController::Base.fragment_cache_store = :mem_cache_store ,{} 
ActionController::Base.fragment_cac...
Caching Fragments 
<% cache 'my/cache/key', :expire => 10.minutes do %> 
... 
<% end %>
Memcache Sessions 
• We could store our session in Memcache 
• Great for load balancing - share across a 
server farm with...
In Summary 
• Memcache gives you a distributed cache 
store 
• Very fast and very easy to use 
• Lots of ruby and rails li...
Any Questions?
Upcoming SlideShare
Loading in …5
×

Memcached Talk

6,633 views

Published on

A talk on using Memcache in Ruby, given to the Ruby-on-Rails Sydney Group.

  • Be the first to comment

Memcached Talk

  1. 1. Memcache Rob Sharp rob@sharp.id.au Lead Developer The Sound Alliance
  2. 2. About Memcached • conceived by Brad Fitzpatrick as a solution to the scaling issues faced by Livejournal • “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”
  3. 3. Who we are • Sydney-based online media publishing • Community and Content Sites • Fasterlouder.com.au • Inthemix.com.au • Samesame.com.au • Thoughtbythem - Marketing Agency • White Label Gig Ticketing
  4. 4. Who we are • Inthemix.com.au • Australia’s busiest music website • ~ 250,000 pages per day • Plus two other busy sites! • Maximum performance for the hardware we have
  5. 5. Current Architechture • 3 Linux servers • Apache • Lighttpd • Memcache • 1 MySQL Master
  6. 6. Why do we use memcached? • CMS written in OO style, using Activerecord • PHP4 and objects don't mix too well • Activerecord gave us fast development but reduced performance • Call us greedy, but we want both • Use Rails Memcache!
  7. 7. Our Application • CMS written from the ground up • Effectively three sites running on one codebase • Uses three seperate databases, but aiming to consolidate more • Has data namespacing implemented in most places • But seperation is not quite there yet!
  8. 8. Our Memcache Setup • We have 3 webservers running memcache • Each server runs three daemons on separate ports - one for each site (more on this later!)
  9. 9. Memcache Pool • Each daemon knows about the other 2 daemons and connects to them over TCP • This allows us to store data once, and access it from any server, whether in the pool or not • Hashing algorithm means that a given key maps to a single server • Efficient use of memory • Efficient for cache clearing
  10. 10. Memcache Pool • But what if we lose a server? We can • Ignore it - we simply get misses for any keys we attempt to retrieve • Remove it - our hashing algorithm breaks... :( • We can also add new servers to the pool after data has been stored, but the same hashing problem occurs
  11. 11. Memcache Pool • Consistent hashing will solve the problem of removing or adding servers once data has been hashed • Currently in its infancy - not really production ready • We simply monitor our daemons and restart if required
  12. 12. Installing Memcached • Available in most Linux distros • packaged for Fedora, RHEL4/5, Ubuntu, Debian, Gentoo and BSD • OSX? Use Ports! • sudo port install memcache • sudo gem install memcache-client • sudo gem install cached_model
  13. 13. Memcache and Ruby • We’ll use the memcache-client gem • Pure Ruby implementation • Pretty fast!
  14. 14. Storing Stuff rsharp$ sudo gem install memcache-client require 'memcache' memcache_options = { :compression => true, :debug => false, :namespace => 'my_favourite_artists', :readonly => false, :urlencode => false } Cache = MemCache.new memcache_options Cache.servers = 'localhost:11211'
  15. 15. Storing Stuff Cache.set 'favourite_artist', 'Salvador Dali' skateboarder = Cache.get 'favourite_artist' Cache.delete 'favourite_artist'
  16. 16. Memcache Namespaces • Memcache doesn’t have namespaces, so we have to improvise • Prefix your keys with a namespace by setting the namespace when you connect • Our solution: • Run multiple memcache instances on different ports
  17. 17. Roll your own? • Memcache-client provides basic cache methods • What if we extended ActiveRecord? • We can, with active_model
  18. 18. Storing Stuff Part Deux rsharp$ sudo gem install cached_model require 'cached_model' memcache_options = { :compression => true, :debug => false, :namespace => 'hifibuys', :readonly => false, :urlencode => false } CACHE = MemCache.new memcache_options CACHE.servers = 'localhost:11211'
  19. 19. Storing Stuff Part Deux class Artist < CachedModel end
  20. 20. cached_model Performance • CachedModel is not magic. • CachedModel only accelerates simple finds for single rows. • CachedModel won’t cache every query you run. • CachedModel isn’t smart enough to determine the dependencies between your queries so that it can accelerate more complicated queries. If you want to cache more complicated queries you need do it by hand.
  21. 21. Other options • acts_as_cached provides a similar solution
  22. 22. Memcache Storage • Memcache stores blobs • The memcache client handles marshalling, so you can easily cache objects • This does however mean that the objects aren’t necessarily cross-language
  23. 23. Memcache Storage • The most obvious things to store are objects • We cache articles • We cache collections of articles • We cache template data • We cache fragments • We don’t cache SQL queries
  24. 24. What we cache • Our sites are fairly big and data-rich communities • Almost every page has editorially controlled attributes along with user generated content • Like...
  25. 25. Our Example Dataset • Article • Joins Artists • Joins Locations • Joins Genres • Joins Related Content • Joins Related Forum Activity • Joins Related Gallery Data
  26. 26. Our Example Dataset • Article (continues) • ... • Joins Media Content • Joins Comments • Joins ‘Rollcalls’ • Joins other secret developments
  27. 27. Our Example • An article requires many data facets • Most don’t change that often • We also know when they change • Yay for the Observer pattern • User content changes much more regularly • Can be changed from outside our controlled area (e.g. Forums)
  28. 28. Our Example Summary • Data can be loosely divided into editorially controlled and user-generated • Cache editorially controlled content separately from user-generated content • Simplest way to implement is in fragment caching
  29. 29. Fragment Caching • Memcache allows timed expiry of fragments • Identify areas that change infrequently and cache • Remember to measure performance before and after • Evidence suggests very large gains! • Use memcache_fragments
  30. 30. Caching Fragments rsharp$ sudo gem install memcache_fragments require 'memcache_fragments' memcache_options = { :compression => true, :debug => false, :namespace => 'hifibuys', :readonly => false, :urlencode => false } CACHE = MemCache.new memcache_options CACHE.servers = 'localhost:11211'
  31. 31. Caching Fragments ActionController::Base.fragment_cache_store = :mem_cache_store ,{} ActionController::Base.fragment_cache_store.data = CACHE, {} ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.merge! ({ 'cache' => CACHE })
  32. 32. Caching Fragments <% cache 'my/cache/key', :expire => 10.minutes do %> ... <% end %>
  33. 33. Memcache Sessions • We could store our session in Memcache • Great for load balancing - share across a server farm without using a DB store • Ideal for transient data • Solution exists in db_memcache_store • DB backend with memcache layer - the best of both worlds
  34. 34. In Summary • Memcache gives you a distributed cache store • Very fast and very easy to use • Lots of ruby and rails libraries • memcache_client • cached_model • db_memcache_store • memcache_fragments
  35. 35. Any Questions?

×