SlideShare a Scribd company logo
1 of 104
Download to read offline
CHRIS WANSTRATH
   ERR FREE

   [ http://errfree.com ]
50,000,000 pages

 no downtime
memcached.
Memcaching Rails
  CHRIS WANSTRATH
     ERR FREE
     [ http://errfree.com ]
Memcaching Rails
  CHRIS WANSTRATH
     ERR FREE
     [ http://errfree.com ]
chris wanstrath
           railsconf 2007




 kickin’ ass
with cache-fu
{}
class Memcache < Hash
  undef :each, :keys
end
class Memcache < DRbHash
  undef :each, :keys
end
$ memcached -vv
<3 server listening
<7 new client connection
<7 get app-test:Story:1
>7 END
<7 set app-test:Story:2 0
>7 STORED
<7 delete app-test:Story:1
>7 DELETED
$ memcached -vv
<3 server listening
<7 new client connection
<7 get app-test:Story:1
>7 END
<7 set app-test:Story:2 0
>7 STORED
<7 delete app-test:Story:1
>7 DELETED
YAGNI
UYRDNI
UYRDNI
(unless you really do need it)
class Presentation < ActiveRecord::Base

      def self.get_cache(id)
          if data = @cache.get(id)
            data
	

 	

 else
            data = find(id)
            @cache.set(id, data)
            data
          end
      end

end
class Presentation  ActiveRecord::Base

      def self.get_cache(id)
          @cache.get(id) ||
	

 	

   @cache.set(id, find(id))
      end

end
Fragments
  Actions
 Sessions
     
  Objects
memcache-client
memcache-client

$ gem install memcache-client
topfunky memcached
CachedModel
Fragment Cache Store
Session Store
cache_fu
cache_fu
( acts_as_cached 2.0 )
Fragments
  Actions
 Sessions
     
  Objects
acts_as_cached
config/memcached.yml
defaults:
  ttl: 1800
  namespace: railsconf
  sessions: false
  fragments: false
  servers: localhost:11211
config/memcached.yml
defaults:
  ttl: 1800
  namespace: railsconf
  sessions: true
  fragments: true
  servers: localhost:11211
config/memcached.yml
production:
  benchmarking: false
  sessions: true
  fragments: true
  servers:
    - 192.185.254.121:11211
    - 192.185.254.138:11211
    - 192.185.254.160:11211
config/memcached.yml
production:
  benchmarking: false
  sessions: true
  fragments: true
  servers:
    - 192.185.254.121:11211
    - 192.185.254.138:11211
    - 192.185.254.160:11211
class Presentation  ActiveRecord::Base

  acts_as_cached

end
get_cache

expire_cache
class Presentation  ActiveRecord::Base

  acts_as_cached

  after_save :expire_cache

end
class Presentation  ActiveRecord::Base

      def self.get_cache(id)
          if data = @cache.get(id)
            data
	

 	

 else
            data = find(id)
            @cache.set(id, data)
            data
          end
      end

end
class Presentation  ActiveRecord::Base

      def self.get_cache(id)
          if not (data = @cache.get(id)).nil?
            data
	

 	

 else
            data = find(id)
            @cache.set(id, data)
            data
          end
      end

end
class Presentation  ActiveRecord::Base

      def self.get_cache(id)
          if not (data = @cache.get(id)).nil?
            data
	

 	

 else
            data = find(id) || false
            @cache.set(id, data)
            data
          end
      end

end
Presentation.get_cache(1337)
class Presentation  ActiveRecord::Base

  acts_as_cached :conditions = 'published = 1'

end
class Presentation  ActiveRecord::Base

  acts_as_cached :finder = :find_live

end
Topic.find :all,
  :conditions =
    [quot;created_at  ?quot;, 1.week.ago],
  :order = 'post_count desc',
  :limit = 5
class Topic  ActiveRecord::Base

  def self.weekly_popular(limit = 5)
    find :all,
      :conditions =
        [quot;created_at  ?quot;, 1.week.ago],
      :order = 'post_count desc',
      :limit = limit
  end

end
Topic.weekly_popular
DB: 0.00 (0%)
class Topic  ActiveRecord::Base

  def self.cached_weekly_popular
    get_cache(:weekly_popular) do
      weekly_popular
    end
  end

end
Topic.cached_weekly_popular
ruby mocha
bdd test spec
A   Ruby object acting as cached
-   should be able to retrieve a cached version of itself
-   should be able to set itself to the cache
-   should pass its cached self into a block when supplied
-   should be able to expire its cache
-   should be able to reset its cache
-   should be able to tell if it is cached
-   should be able to set itself to the cache with an arbitrary ttl

Finished in 0.028509 seconds.

28 specifications (53 requirements), 0 failures
context quot;Calling #cached_weekly_popularquot; do
  specify quot;should call #weekly_popular if not cachedquot; do
    Topic.expects(:fetch_cache).returns(nil)
    Topic.cached_weekly_popular.should.equal Topic.weekly_popular
  end

  specify quot;should return if cachedquot; do
    Topic.expects(:get_cache).returns(true)
    Topic.expects(:weekly_popular).never
    Topic.cached_weekly_popular
  end
end
Topic.cached(:weekly_popular)
def self.cache_key_with_date(id)
  date = Date.today.to_s.tr(' ', '_')
  cache_key_without_date(id) + ':' + date
end

class  self
  alias_method_chain :cache_key, :date
end
class Topic  ActiveRecord::Base

  def self.date_for_key
    Date.today.to_s.tr(' ', '_')
  end

  def self.cached_weekly_popular
    key = 'weekly_popular' + date_for_key
    get_cache(key) { weekly_popular }
  end

end
Topic.find(1, 2, 3)
Topic.get_cache(1, 2, 3)
user_ids = @topic.posts.map(:user_id).uniq
@users   = User.get_cache(user_ids)
class ApplicationController
  before_filter :local_cache_for_request
end
# pulls from memcache
@user = User.get_cache(1)

# pulls from local cache
@user = User.get_cache(1)
class ApplicationController
  before_filter :set_cache_override

  def set_cache_override
    returning true do
      ActsAsCached.skip_cache_gets =
        !!params[:skip_cache]
    end
  end

end
www.mysite.com/home?skip_cache=1
reset_cache
@topic.set_cache
class Presentation  ActiveRecord::Base

  acts_as_cached

  after_save :reset_cache

end
class Presentation  ActiveRecord::Base

  acts_as_cached :version = 1

end
monit
libketama
1



600         200




      400
1



600 cache_get :railsconf   200




             400
1

      :railsconf == 100

600                 200




      400
1

      :railsconf == 200

600                 200




      400
1

      :railsconf == 200

600                 200
1
      700
            :railsconf == 200

600                       200


      500              300
            400
l33t h4x0rs

                    • Geoffrey Grosenbach
•   Rob Sanheim
                    • Ryan King
•   Lourens Naudé
                    • Michael Moen
•   Corey Donohoe
                    • PJ Hyett
•   Eric Hodel
{}
( thanks. any questions? )
http://flickr.com/photos/seibuone/144588686/
http://flickr.com/photos/j00zt1n/255430115/
http://flickr.com/photos/jameswong/145397570/
http://flickr.com/photos/xalpha/58368229/
http://flickr.com/photos/63503896@N00/35723413/
http://flickr.com/photos/mrcrash/145451993/
                                                   thanks
http://flickr.com/photos/psybernoid/398301743/
http://flickr.com/photos/45royale/422227291/
                                                    flickr
http://flickr.com/photos/andrson/420810541/
http://flickr.com/photos/joshuaweiland/370931770/
http://flickr.com/photos/zesmerelda/27258314/
http://flickr.com/photos/slice/390271923/
http://flickr.com/photos/cocoen/411960476/
http://flickr.com/photos/pinguino/198885132/
http://flickr.com/photos/davidfmiller/468476118/
http://laughingsquid.com - Scott Beale
http://flickr.com/photos/bail56/313536999/
http://flickr.com/photos/65995199@N00/272672183/

More Related Content

What's hot

High Performance Django
High Performance DjangoHigh Performance Django
High Performance Django
DjangoCon2008
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统
yiditushe
 
4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook
guoqing75
 
Php user groupmemcached
Php user groupmemcachedPhp user groupmemcached
Php user groupmemcached
Jason Anderson
 
Harmonious Development: Standardizing The Deployment Process via Vagrant and ...
Harmonious Development: Standardizing The Deployment Process via Vagrant and ...Harmonious Development: Standardizing The Deployment Process via Vagrant and ...
Harmonious Development: Standardizing The Deployment Process via Vagrant and ...
Acquia
 
"Mobage DBA Fight against Big Data" - NHN TE
"Mobage DBA Fight against Big Data" - NHN TE"Mobage DBA Fight against Big Data" - NHN TE
"Mobage DBA Fight against Big Data" - NHN TE
Ryosuke IWANAGA
 
Cloud, Cache, and Configs
Cloud, Cache, and ConfigsCloud, Cache, and Configs
Cloud, Cache, and Configs
Scott Taylor
 
Stack kicker devopsdays-london-2013
Stack kicker devopsdays-london-2013Stack kicker devopsdays-london-2013
Stack kicker devopsdays-london-2013
Simon McCartney
 

What's hot (20)

Top Node.js Metrics to Watch
Top Node.js Metrics to WatchTop Node.js Metrics to Watch
Top Node.js Metrics to Watch
 
Beyond PHP - it's not (just) about the code
Beyond PHP - it's not (just) about the codeBeyond PHP - it's not (just) about the code
Beyond PHP - it's not (just) about the code
 
RubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteRubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - Keynote
 
High Performance Django
High Performance DjangoHigh Performance Django
High Performance Django
 
Nginx and friends - putting a turbo button on your site
Nginx and friends - putting a turbo button on your siteNginx and friends - putting a turbo button on your site
Nginx and friends - putting a turbo button on your site
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统
 
Node.js in action
Node.js in actionNode.js in action
Node.js in action
 
4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook
 
Php user groupmemcached
Php user groupmemcachedPhp user groupmemcached
Php user groupmemcached
 
Harmonious Development: Standardizing The Deployment Process via Vagrant and ...
Harmonious Development: Standardizing The Deployment Process via Vagrant and ...Harmonious Development: Standardizing The Deployment Process via Vagrant and ...
Harmonious Development: Standardizing The Deployment Process via Vagrant and ...
 
Beyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the codeBeyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the code
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
 
My sql administration
My sql administrationMy sql administration
My sql administration
 
Php on Windows
Php on WindowsPhp on Windows
Php on Windows
 
"Mobage DBA Fight against Big Data" - NHN TE
"Mobage DBA Fight against Big Data" - NHN TE"Mobage DBA Fight against Big Data" - NHN TE
"Mobage DBA Fight against Big Data" - NHN TE
 
20141011 mastering mysqlnd
20141011 mastering mysqlnd20141011 mastering mysqlnd
20141011 mastering mysqlnd
 
Cloud, Cache, and Configs
Cloud, Cache, and ConfigsCloud, Cache, and Configs
Cloud, Cache, and Configs
 
Memcached Study
Memcached StudyMemcached Study
Memcached Study
 
Stack kicker devopsdays-london-2013
Stack kicker devopsdays-london-2013Stack kicker devopsdays-london-2013
Stack kicker devopsdays-london-2013
 
Cache is King - RubyHACK 2019
Cache is King - RubyHACK 2019Cache is King - RubyHACK 2019
Cache is King - RubyHACK 2019
 

Viewers also liked (6)

Attitude
AttitudeAttitude
Attitude
 
Learning Activities Week 3
Learning Activities Week 3Learning Activities Week 3
Learning Activities Week 3
 
week presentation
week presentationweek presentation
week presentation
 
Learning Activities Week #4
Learning Activities Week #4Learning Activities Week #4
Learning Activities Week #4
 
my presentation
my presentationmy presentation
my presentation
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving Cars
 

Similar to Kickin' Ass with Cache-Fu (without notes)

High Performance Django 1
High Performance Django 1High Performance Django 1
High Performance Django 1
DjangoCon2008
 
Innovation and Security in Ruby on Rails
Innovation and Security in Ruby on RailsInnovation and Security in Ruby on Rails
Innovation and Security in Ruby on Rails
tielefeld
 
Varnish @ Velocity Ignite
Varnish @ Velocity IgniteVarnish @ Velocity Ignite
Varnish @ Velocity Ignite
Artur Bergman
 
CHI-YAPC-2009
CHI-YAPC-2009CHI-YAPC-2009
CHI-YAPC-2009
jonswar
 

Similar to Kickin' Ass with Cache-Fu (without notes) (20)

All I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web FrameworkAll I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web Framework
 
High Performance Django 1
High Performance Django 1High Performance Django 1
High Performance Django 1
 
Cache is King: Get the Most Bang for Your Buck From Ruby
Cache is King: Get the Most Bang for Your Buck From RubyCache is King: Get the Most Bang for Your Buck From Ruby
Cache is King: Get the Most Bang for Your Buck From Ruby
 
Javantura v2 - Replication with MongoDB - what could go wrong... - Philipp Krenn
Javantura v2 - Replication with MongoDB - what could go wrong... - Philipp KrennJavantura v2 - Replication with MongoDB - what could go wrong... - Philipp Krenn
Javantura v2 - Replication with MongoDB - what could go wrong... - Philipp Krenn
 
파이썬 개발환경 구성하기의 끝판왕 - Docker Compose
파이썬 개발환경 구성하기의 끝판왕 - Docker Compose파이썬 개발환경 구성하기의 끝판왕 - Docker Compose
파이썬 개발환경 구성하기의 끝판왕 - Docker Compose
 
Innovation and Security in Ruby on Rails
Innovation and Security in Ruby on RailsInnovation and Security in Ruby on Rails
Innovation and Security in Ruby on Rails
 
Cutting through the fog of cloud
Cutting through the fog of cloudCutting through the fog of cloud
Cutting through the fog of cloud
 
Play vs Rails
Play vs RailsPlay vs Rails
Play vs Rails
 
(SDD402) Amazon ElastiCache Deep Dive | AWS re:Invent 2014
(SDD402) Amazon ElastiCache Deep Dive | AWS re:Invent 2014(SDD402) Amazon ElastiCache Deep Dive | AWS re:Invent 2014
(SDD402) Amazon ElastiCache Deep Dive | AWS re:Invent 2014
 
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
Rapid and Scalable Development with MongoDB, PyMongo, and MingRapid and Scalable Development with MongoDB, PyMongo, and Ming
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
 
PHP Performance Trivia
PHP Performance TriviaPHP Performance Trivia
PHP Performance Trivia
 
Sinatra for REST services
Sinatra for REST servicesSinatra for REST services
Sinatra for REST services
 
Varnish @ Velocity Ignite
Varnish @ Velocity IgniteVarnish @ Velocity Ignite
Varnish @ Velocity Ignite
 
CHI-YAPC-2009
CHI-YAPC-2009CHI-YAPC-2009
CHI-YAPC-2009
 
Java и Linux — особенности эксплуатации / Алексей Рагозин (Дойче Банк)
Java и Linux — особенности эксплуатации / Алексей Рагозин (Дойче Банк)Java и Linux — особенности эксплуатации / Алексей Рагозин (Дойче Банк)
Java и Linux — особенности эксплуатации / Алексей Рагозин (Дойче Банк)
 
Rails 3 (beta) Roundup
Rails 3 (beta) RoundupRails 3 (beta) Roundup
Rails 3 (beta) Roundup
 
Cache Money Talk: Practical Application
Cache Money Talk: Practical ApplicationCache Money Talk: Practical Application
Cache Money Talk: Practical Application
 
Identify Literate Code
Identify Literate CodeIdentify Literate Code
Identify Literate Code
 
Django Celery
Django Celery Django Celery
Django Celery
 
Using apache spark for processing trillions of records each day at Datadog
Using apache spark for processing trillions of records each day at DatadogUsing apache spark for processing trillions of records each day at Datadog
Using apache spark for processing trillions of records each day at Datadog
 

More from err (6)

Inside GitHub
Inside GitHubInside GitHub
Inside GitHub
 
The Real-Time Web (and Other Buzzwords)
The Real-Time Web (and Other Buzzwords)The Real-Time Web (and Other Buzzwords)
The Real-Time Web (and Other Buzzwords)
 
Git: The Lean, Mean, Distributed Machine
Git: The Lean, Mean, Distributed MachineGit: The Lean, Mean, Distributed Machine
Git: The Lean, Mean, Distributed Machine
 
Kings of Code 2009
Kings of Code 2009Kings of Code 2009
Kings of Code 2009
 
Forbidden Fruit: A Taste of Ruby's ParseTree
Forbidden Fruit: A Taste of Ruby's ParseTreeForbidden Fruit: A Taste of Ruby's ParseTree
Forbidden Fruit: A Taste of Ruby's ParseTree
 
Making and Breaking Web Services with Ruby
Making and Breaking Web Services with RubyMaking and Breaking Web Services with Ruby
Making and Breaking Web Services with Ruby
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 

Kickin' Ass with Cache-Fu (without notes)