SlideShare ist ein Scribd-Unternehmen logo
1 von 24
Caching,
                 Memcached,
                 and Rails

                 Mike Perham
                 http://mikeperham.com

Wednesday, March 25, 2009
Me	


                  Maintainer of memcache-client, shipped with Rails

                  Hardcore performance nerd




Wednesday, March 25, 2009
Performance


                  Let me Tell you a Story...




Wednesday, March 25, 2009
Performance Tuning

                  Remove unnecessary Functionality

                  Tune necessary Functionality

                  ONLY THEN, add caching last

                  ALWAYS track before/after timings




Wednesday, March 25, 2009
Caching

                  Cache the results of expensive operations

                  Why is this memoization dumb?




Wednesday, March 25, 2009
Why Dumb?

                            SCALABILITY!
                            FRESHNESS!



Wednesday, March 25, 2009
Rails
                  Built in support for memcached since Rails 2.1

                  config/environments/production.rb:




Wednesday, March 25, 2009
Request
                            Varnish   Apache     Rails     DB




                                               Memcached



                  Standard Toolset for Rails 2.3

                  Further == Slower



Wednesday, March 25, 2009
Caching Mechanisms

                  HTTP Proxy
                               FASTER
                  Page

                  Action

                  Fragment
                               SLOWER
                  Object



Wednesday, March 25, 2009
HTTP Proxy


                  HTTP/URL caching with Varnish

                       Expires, Etag, Cache-Control headers

                  5-10k req/sec!




Wednesday, March 25, 2009
Page Caching	


                  ERB --> Static HTML

                  Uh oh, uses the filesystem!

                  Apache = 1-5k req/sec




Wednesday, March 25, 2009
Action Caching


                  Runs filters (e.g. authorization)

                  Can use any cache strategy

                       File, memory, memcached




Wednesday, March 25, 2009
Fragment Caching


                  Parts of the page are unique per user

                  Fragment cache the various UI boxes

                  Dynamically assemble HTML from the cache




Wednesday, March 25, 2009
Object Caching

                  Built-in AR support for “smart” keys

                       Rails.cache.fetch(post.cache_key) { expensive_calc(post) }

                       “#{table_name}/#{self.id}-#{self.updated_at.to_i}”

                  Support for complex dependencies




Wednesday, March 25, 2009
Expiration
                  “There are only two hard things in Computer
                  Science: naming and cache invalidation”

                  Explicit
                       expire_page :controller => ‘users’, :action => ‘index’

                       expire_action :controller => ‘users’, :action => ‘index’

                       Rails.cache.delete(‘key’)


                  Observers


Wednesday, March 25, 2009
Expiration (Cont’d)
                  Time-based
                        Rails.cache.fetch(‘key’, :expires_in => 1.hour) { ... }

                        expires_in(2.hours) # sets page’s HTTP max-age attribute


                  Versioning
                        Rails.cache.fetch(user.cache_key) { ... }




Wednesday, March 25, 2009
Memcached

                  Giant Hash of Fixed Size

                  Time-based expiration

                  LRU eviction

                  Operations: GET, SET, APPEND, INCR, CAS

                  Limited to 1MB per value, 250 chars per key



Wednesday, March 25, 2009
Facebook


                  100s of servers w/ 28TB of RAM

                  5 billion page views/day

                  400k req/sec from memcached




Wednesday, March 25, 2009
Performance




Wednesday, March 25, 2009
Mongrel + Nginx
                                         Varnish

                                         Nginx

                                         WEB1

                                                   M1     M2
                            M1      M2


                        Memcached                  Memcached


                                 APP1                   APP2

Wednesday, March 25, 2009
Apache + Passenger
                                           Varnish

                                           HAProxy


                                           WEB1

                             Memcached                         Memcached
               Apache                                Apache


                    Rails          Rails              Rails          Rails


                            APP1                              APP2

Wednesday, March 25, 2009
Advanced


                  Spread your cache across a farm of memcached

                  Use the memcaches_page plugin

                  Use AJAX for user-specific widgets




Wednesday, March 25, 2009
Advice

                  Cache late in the development cycle, after tuning!

                  Memcached gives you time-based expiration,
                  versioning and multi-machine support.

                  Cache as early in the request as possible!


                            Varnish   Apache   Rails   DB




Wednesday, March 25, 2009
THANK YOU!
                            HTTP://MIKEPERHAM.COM
                             MPERHAM@GMAIL.COM

                                 QUESTIONS?




Wednesday, March 25, 2009

Weitere ähnliche Inhalte

Was ist angesagt?

Apache Tomcat 8 Application Server
Apache Tomcat 8 Application ServerApache Tomcat 8 Application Server
Apache Tomcat 8 Application Server
mohamedmoharam
 
Vmware management-with-vcli-5.0
Vmware management-with-vcli-5.0Vmware management-with-vcli-5.0
Vmware management-with-vcli-5.0
Sathishkumar A
 
How automated cloud infrastructure setups can help with Continuous Delivery
How automated cloud infrastructure setups can help with Continuous DeliveryHow automated cloud infrastructure setups can help with Continuous Delivery
How automated cloud infrastructure setups can help with Continuous Delivery
Edmund Siegfried Haselwanter
 
Tomcat Clustering
Tomcat ClusteringTomcat Clustering
Tomcat Clustering
gouthamrv
 

Was ist angesagt? (20)

Magento caching
Magento cachingMagento caching
Magento caching
 
Succeding with the Apache SOA stack
Succeding with the Apache SOA stackSucceding with the Apache SOA stack
Succeding with the Apache SOA stack
 
OpenSolaris Web Stack MySQL BOF
OpenSolaris Web Stack MySQL BOFOpenSolaris Web Stack MySQL BOF
OpenSolaris Web Stack MySQL BOF
 
Apache Tomcat 8 Application Server
Apache Tomcat 8 Application ServerApache Tomcat 8 Application Server
Apache Tomcat 8 Application Server
 
Vmware management-with-vcli-5.0
Vmware management-with-vcli-5.0Vmware management-with-vcli-5.0
Vmware management-with-vcli-5.0
 
Apache tomcat
Apache tomcatApache tomcat
Apache tomcat
 
Hosting
HostingHosting
Hosting
 
How automated cloud infrastructure setups can help with Continuous Delivery
How automated cloud infrastructure setups can help with Continuous DeliveryHow automated cloud infrastructure setups can help with Continuous Delivery
How automated cloud infrastructure setups can help with Continuous Delivery
 
Tomcat Configuration (1)
Tomcat Configuration (1)Tomcat Configuration (1)
Tomcat Configuration (1)
 
Tomcat server
 Tomcat server Tomcat server
Tomcat server
 
Tomcat next
Tomcat nextTomcat next
Tomcat next
 
Tomcat Clustering
Tomcat ClusteringTomcat Clustering
Tomcat Clustering
 
Empowering Next Generation Media
Empowering Next Generation MediaEmpowering Next Generation Media
Empowering Next Generation Media
 
Introduction to Apache Tomcat 7 Presentation
Introduction to Apache Tomcat 7 PresentationIntroduction to Apache Tomcat 7 Presentation
Introduction to Apache Tomcat 7 Presentation
 
Next-Generation Best Practices for VMware and Storage
Next-Generation Best Practices for VMware and StorageNext-Generation Best Practices for VMware and Storage
Next-Generation Best Practices for VMware and Storage
 
Infrastructure as Code with Chef / Puppet
Infrastructure as Code with Chef / PuppetInfrastructure as Code with Chef / Puppet
Infrastructure as Code with Chef / Puppet
 
Introduction of Tomcat
Introduction of TomcatIntroduction of Tomcat
Introduction of Tomcat
 
Living the Easy Life with Rules-Based Autonomic Database Clusters
Living the Easy Life with Rules-Based Autonomic Database ClustersLiving the Easy Life with Rules-Based Autonomic Database Clusters
Living the Easy Life with Rules-Based Autonomic Database Clusters
 
Deploying FuseMQ with Fuse Fabric
Deploying FuseMQ with Fuse FabricDeploying FuseMQ with Fuse Fabric
Deploying FuseMQ with Fuse Fabric
 
Where Does VMware Integration Occur?
Where Does VMware Integration Occur?Where Does VMware Integration Occur?
Where Does VMware Integration Occur?
 

Andere mochten auch (6)

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
 
Memcached, presented to LCA2010
Memcached, presented to LCA2010Memcached, presented to LCA2010
Memcached, presented to LCA2010
 
Metaprogramming JavaScript
Metaprogramming  JavaScriptMetaprogramming  JavaScript
Metaprogramming JavaScript
 
12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQL12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQL
 
Caching
CachingCaching
Caching
 
Enterprise Architectures with Ruby (and Rails)
Enterprise Architectures with Ruby (and Rails)Enterprise Architectures with Ruby (and Rails)
Enterprise Architectures with Ruby (and Rails)
 

Ähnlich wie Caching, Memcached And Rails

Rails and Merb
Rails and MerbRails and Merb
Rails and Merb
ludoo
 
Railswaycon 2009 - Summary
Railswaycon 2009 - SummaryRailswaycon 2009 - Summary
Railswaycon 2009 - Summary
daniel.mattes
 
Gear6 Webinar - MySQL Scaling with Memcached
Gear6 Webinar - MySQL Scaling with MemcachedGear6 Webinar - MySQL Scaling with Memcached
Gear6 Webinar - MySQL Scaling with Memcached
Gear6
 
090507.New Replication Features(2)
090507.New Replication Features(2)090507.New Replication Features(2)
090507.New Replication Features(2)
liufabin 66688
 
Google DevFest 09
Google DevFest 09Google DevFest 09
Google DevFest 09
Eze Torres
 

Ähnlich wie Caching, Memcached And Rails (20)

Alternative Databases
Alternative DatabasesAlternative Databases
Alternative Databases
 
Rails and Merb
Rails and MerbRails and Merb
Rails and Merb
 
Memcached
MemcachedMemcached
Memcached
 
Plugin Memcached%20 Study
Plugin Memcached%20 StudyPlugin Memcached%20 Study
Plugin Memcached%20 Study
 
Google html5 Tutorial
Google html5 TutorialGoogle html5 Tutorial
Google html5 Tutorial
 
Html5
Html5Html5
Html5
 
Railswaycon 2009 - Summary
Railswaycon 2009 - SummaryRailswaycon 2009 - Summary
Railswaycon 2009 - Summary
 
Google Developer Days Brazil 2009 - Keynote
Google Developer Days Brazil 2009 -  KeynoteGoogle Developer Days Brazil 2009 -  Keynote
Google Developer Days Brazil 2009 - Keynote
 
Alejandro Villanueva - Google Inc.
Alejandro Villanueva - Google Inc.Alejandro Villanueva - Google Inc.
Alejandro Villanueva - Google Inc.
 
Google - Charla para CTOs
Google - Charla para CTOsGoogle - Charla para CTOs
Google - Charla para CTOs
 
Why Ruby on Rails rocks from a .net developer eye
Why Ruby on Rails rocks from a .net developer eyeWhy Ruby on Rails rocks from a .net developer eye
Why Ruby on Rails rocks from a .net developer eye
 
HTML5 and Google Chrome - DevFest09
HTML5 and Google Chrome - DevFest09HTML5 and Google Chrome - DevFest09
HTML5 and Google Chrome - DevFest09
 
intl me this, intl me that
intl me this, intl me thatintl me this, intl me that
intl me this, intl me that
 
Gear6 Webinar - MySQL Scaling with Memcached
Gear6 Webinar - MySQL Scaling with MemcachedGear6 Webinar - MySQL Scaling with Memcached
Gear6 Webinar - MySQL Scaling with Memcached
 
Nginx: From Russia With Love
Nginx: From Russia With LoveNginx: From Russia With Love
Nginx: From Russia With Love
 
Systems Integration in the Cloud Era, Kai Wähner MaibornWolff
Systems Integration in the Cloud Era, Kai Wähner MaibornWolffSystems Integration in the Cloud Era, Kai Wähner MaibornWolff
Systems Integration in the Cloud Era, Kai Wähner MaibornWolff
 
Using Q4M - a message queue storage engine for MySQL
Using Q4M - a message queue storage engine for MySQLUsing Q4M - a message queue storage engine for MySQL
Using Q4M - a message queue storage engine for MySQL
 
090507.New Replication Features(2)
090507.New Replication Features(2)090507.New Replication Features(2)
090507.New Replication Features(2)
 
Apache Sling - The whys and the hows
Apache Sling - The whys and the howsApache Sling - The whys and the hows
Apache Sling - The whys and the hows
 
Google DevFest 09
Google DevFest 09Google DevFest 09
Google DevFest 09
 

Kürzlich hochgeladen

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
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
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Kürzlich hochgeladen (20)

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
[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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
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
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
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
 
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?
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 

Caching, Memcached And Rails

  • 1. Caching, Memcached, and Rails Mike Perham http://mikeperham.com Wednesday, March 25, 2009
  • 2. Me Maintainer of memcache-client, shipped with Rails Hardcore performance nerd Wednesday, March 25, 2009
  • 3. Performance Let me Tell you a Story... Wednesday, March 25, 2009
  • 4. Performance Tuning Remove unnecessary Functionality Tune necessary Functionality ONLY THEN, add caching last ALWAYS track before/after timings Wednesday, March 25, 2009
  • 5. Caching Cache the results of expensive operations Why is this memoization dumb? Wednesday, March 25, 2009
  • 6. Why Dumb? SCALABILITY! FRESHNESS! Wednesday, March 25, 2009
  • 7. Rails Built in support for memcached since Rails 2.1 config/environments/production.rb: Wednesday, March 25, 2009
  • 8. Request Varnish Apache Rails DB Memcached Standard Toolset for Rails 2.3 Further == Slower Wednesday, March 25, 2009
  • 9. Caching Mechanisms HTTP Proxy FASTER Page Action Fragment SLOWER Object Wednesday, March 25, 2009
  • 10. HTTP Proxy HTTP/URL caching with Varnish Expires, Etag, Cache-Control headers 5-10k req/sec! Wednesday, March 25, 2009
  • 11. Page Caching ERB --> Static HTML Uh oh, uses the filesystem! Apache = 1-5k req/sec Wednesday, March 25, 2009
  • 12. Action Caching Runs filters (e.g. authorization) Can use any cache strategy File, memory, memcached Wednesday, March 25, 2009
  • 13. Fragment Caching Parts of the page are unique per user Fragment cache the various UI boxes Dynamically assemble HTML from the cache Wednesday, March 25, 2009
  • 14. Object Caching Built-in AR support for “smart” keys Rails.cache.fetch(post.cache_key) { expensive_calc(post) } “#{table_name}/#{self.id}-#{self.updated_at.to_i}” Support for complex dependencies Wednesday, March 25, 2009
  • 15. Expiration “There are only two hard things in Computer Science: naming and cache invalidation” Explicit expire_page :controller => ‘users’, :action => ‘index’ expire_action :controller => ‘users’, :action => ‘index’ Rails.cache.delete(‘key’) Observers Wednesday, March 25, 2009
  • 16. Expiration (Cont’d) Time-based Rails.cache.fetch(‘key’, :expires_in => 1.hour) { ... } expires_in(2.hours) # sets page’s HTTP max-age attribute Versioning Rails.cache.fetch(user.cache_key) { ... } Wednesday, March 25, 2009
  • 17. Memcached Giant Hash of Fixed Size Time-based expiration LRU eviction Operations: GET, SET, APPEND, INCR, CAS Limited to 1MB per value, 250 chars per key Wednesday, March 25, 2009
  • 18. Facebook 100s of servers w/ 28TB of RAM 5 billion page views/day 400k req/sec from memcached Wednesday, March 25, 2009
  • 20. Mongrel + Nginx Varnish Nginx WEB1 M1 M2 M1 M2 Memcached Memcached APP1 APP2 Wednesday, March 25, 2009
  • 21. Apache + Passenger Varnish HAProxy WEB1 Memcached Memcached Apache Apache Rails Rails Rails Rails APP1 APP2 Wednesday, March 25, 2009
  • 22. Advanced Spread your cache across a farm of memcached Use the memcaches_page plugin Use AJAX for user-specific widgets Wednesday, March 25, 2009
  • 23. Advice Cache late in the development cycle, after tuning! Memcached gives you time-based expiration, versioning and multi-machine support. Cache as early in the request as possible! Varnish Apache Rails DB Wednesday, March 25, 2009
  • 24. THANK YOU! HTTP://MIKEPERHAM.COM MPERHAM@GMAIL.COM QUESTIONS? Wednesday, March 25, 2009

Hinweis der Redaktion