SlideShare a Scribd company logo
1 of 45
Download to read offline
Accelerate Your Rails Site with
                           Automatic Generation-Based Action
                           Caching
                           Rod Cope, CTO and Founder
                           OpenLogic, Inc.




Wednesday, July 29, 2009
Goal




                            Built-in caching is hard:

                           Learn how to automate it to
                             make the pain go away




                                      OpenLogic Company Confidential   2

Wednesday, July 29, 2009
Agenda


                     Introduction
                     Built-in caching
                     Automatic generation-based caching
                     Bonus material
                     Conclusion & recommendations




                                          OpenLogic Company Confidential   3

Wednesday, July 29, 2009
Introduction

                     Rod Cope
                           CTO & Founder of OpenLogic
                           25 years of software development experience
                           IBM Global Services, Anthem, Ericsson, many more
                     OpenLogic, Inc.
                            Governance, SLA support, security updates, and
                           indemnification for
                           over 500 Open Source packages
                            Dozens of Fortune 500 customers
                            OSS Census (osscensus.org)
                             Global, community effort to catalog use of open source



                                                        OpenLogic Company Confidential   4

Wednesday, July 29, 2009
No Spoon Feeding




                           OpenLogic Company Confidential   5

Wednesday, July 29, 2009
Don’t Panic




                                                            scotduke.com




                           OpenLogic Company Confidential                  6

Wednesday, July 29, 2009
Reasons for Caching


                     Lots of hits, speed is king
                           Google




                                             OpenLogic Company Confidential   7

Wednesday, July 29, 2009
OpenLogic Company Confidential   8

Wednesday, July 29, 2009
Reasons for Caching

                Lots of hits, speed is king
                      Google




                Complex hits, richness is king
                      OpenLogic’s OLEX




                                              OpenLogic Company Confidential   9

Wednesday, July 29, 2009
OpenLogic Company Confidential   10

Wednesday, July 29, 2009
Rails Caching Background

                Page caching
                      Uberfast, little control
                      Highly dynamic site? Move on.
                Action caching
                      Fast, lots of control
                      Expiration hell
                Fragment caching
                      Expiration hell




                                               OpenLogic Company Confidential   11

Wednesday, July 29, 2009
Problems with Rails Caching

               Caching is easy to implement
               “Site’s borked again - turn off caching.”
               Hard to clear (the right) caches when you’re mixing and
               matching different types, styles, developers, plugins,
               etc.




                                        OpenLogic Company Confidential   12

Wednesday, July 29, 2009
Automatic Generation-Based Caching
                Generation-based
                      Partition the cache so that every state change increments the
                     “generation”
                Automatic
                      Any action that could possibly change state bumps the count
                      Let memcached overwrite old generations
                Very conservative
                      No domain knowledge
                      Don’t cache errors, AJAX, redirects, flash notices, etc.




                                                   OpenLogic Company Confidential     13

Wednesday, July 29, 2009
Overview

                             Non-cached                                     Cached

                                Rails                                         Rails
                              plumbing                                      plumbing



                                Your                                         Cache
                                code                                         helper



                                          Render
                  Database                                                 memcached
                                           View

                                          OpenLogic Company Confidential               14

Wednesday, July 29, 2009
Cache = Hash


                              Key                                      Value


                           /packages/1   <html><body>Rails</body></html>


                           /packages/2   <html><body>Ruby</body></html>




                                          OpenLogic Company Confidential

Wednesday, July 29, 2009
Generational Cache


                                 Key                                       Value

                           /gen/1/packages/1               <html>Rails</html>

                           /gen/1/packages/2               <html>Ruby</html>




                                               OpenLogic Company Confidential

Wednesday, July 29, 2009
Generational Cache


                                 Key                                       Value

                           /gen/1/packages/1               <html>Rails</html>

                           /gen/1/packages/2               <html>Ruby</html>

                           /gen/2/packages/1     <html>Ruby on Rails</html>




                                               OpenLogic Company Confidential

Wednesday, July 29, 2009
memcached is your friend

                 Very fast
                 Don’t worry about removing old entries
                 memcached will automatically drop the oldest keys
                 when it runs low on memory
                 Run it on your web servers if you have the RAM




                                         OpenLogic Company Confidential   18

Wednesday, July 29, 2009
Cache Helper

                 No cache-related code sprinkled throughout every
                 model, view, and/or controller
                 Use a global “around” filter
                 Automatically increment (scoped) generation count
                 upon POST, PUT, or DELETE
                 Handle event recording and playback (optional)




                                         OpenLogic Company Confidential   19

Wednesday, July 29, 2009
Cache Helper Code (in “around” filter)
          key = make_auto_cache_key(request.request_uri)
          output = Rails.cache.read(key)
          if output
            render :text => output
            return
          else
            yield
            unless response.redirected_to || flash[:notice]
              Rails.cache.write(key, response.body)
            end
          end



                                 OpenLogic Company Confidential   20

Wednesday, July 29, 2009
Cache Key


        def make_auto_cache_key(key)
          ol_gen = Rails.cache.fetch(OL_GEN) { "1" }
          "olex/#{ol_gen}"
        end




                             OpenLogic Company Confidential   21

Wednesday, July 29, 2009
“Clear” the Cache


             POST, PUT, or DELETE “clears” the cache


             def maybe_clear_auto_cache
               return if request.get?
               gen = Rails.cache.fetch(OL_GEN) { "1" }
               Rails.cache.write(OL_GEN, (gen.to_i + 1).to_s)
             end




                                     OpenLogic Company Confidential   22

Wednesday, July 29, 2009
Controller Customization

                   Some controllers may need special cache control
                   (e.g., user-specific cache, disable cache for certain
                   methods)
                   olex_auto_cache_is_specific_to_user :only => :show


                   Note: the cache may still need to be cleared when
                   skipping cache usage!
                   skip_filter :olex_auto_cache
                   after_filter :maybe_clear_olex_auto_cache




                                            OpenLogic Company Confidential   23

Wednesday, July 29, 2009
Cache Partitioning


          Make it impossible for users of different corporations and
          permission levels to see each other’s stuff, access the same
          cache, clear somebody else’s cache, etc.
          Make sure any “global” changes clear all caches
          Use a cache hierarchy
                /olex/<gen #>/corp/<corp #>/<corp gen #>/roles/<role #s>/URL
                MD5 the key (memcached has a 250 char limit)
          Extra credit: Use the key as an etag



                                             OpenLogic Company Confidential    24

Wednesday, July 29, 2009
Partitioned Cache
   Global generation
                                                                                        <html>
               /olex/13/corp/72/2/roles/2,7,19/packages/2                        Rails (unsupported)
                                                                                        </html>




                                                OpenLogic Company Confidential

Wednesday, July 29, 2009
Partitioned Cache
                      Corporate account ID
                                                                                        <html>
               /olex/13/corp/72/2/roles/2,7,19/packages/2                        Rails (unsupported)
                                                                                        </html>




                                                OpenLogic Company Confidential

Wednesday, July 29, 2009
Partitioned Cache
              Corporation #72’s generation
                                                                                        <html>
               /olex/13/corp/72/2/roles/2,7,19/packages/2                        Rails (unsupported)
                                                                                        </html>




                                                OpenLogic Company Confidential

Wednesday, July 29, 2009
Partitioned Cache
            Current user’s role IDs
                                                                                        <html>
               /olex/13/corp/72/2/roles/2,7,19/packages/2                        Rails (unsupported)
                                                                                        </html>




                                                OpenLogic Company Confidential

Wednesday, July 29, 2009
Partitioned Cache

                                                URL                                     <html>
               /olex/13/corp/72/2/roles/2,7,19/packages/2                        Rails (unsupported)
                                                                                        </html>




                                                OpenLogic Company Confidential

Wednesday, July 29, 2009
Partitioned Cache

                                                                                            <html>
               /olex/13/corp/72/2/roles/2,7,19/packages/2                            Rails (unsupported)
                                                                                            </html>
                                                                                            <html>
             /olex/13/corp/72/2/roles/2,7,19,22/packages/2                          Rails (manager secret!)
                                                                                            </html>
                                                                                            <html>
               /olex/14/corp/72/2/roles/2,7,19/packages/2                              Rails (supported)
                                                                                            </html>

                           /olex/14/guest/packages/2                                 <html>Rails</html>



                                                   OpenLogic Company Confidential

Wednesday, July 29, 2009
Cache Key

        def make_auto_cache_key(key, user = nil)
          ol_gen = Rails.cache.fetch(OL_GEN) { "1" }
          ol_prefix = "olex/#{ol_gen}"
          if user.nil? && self.respond_to?("current_user")
            user = current_user
          end
          ca = get_corporate_account(user)
          ...




                               OpenLogic Company Confidential   31

Wednesday, July 29, 2009
Cache Key

     ...
     if user.is_corp_user?
       corp_gen = Rails.cache.fetch(corp_gen_key(ca)){"1"}
       final_key = make_corp_key(user, ca, key, corp_gen)
     else
       final_key = "guest#{key}"
     end




                             OpenLogic Company Confidential   32

Wednesday, July 29, 2009
Corporate Cache Key

      def make_corp_key(user, ca, key, corp_gen)
        roles = role_string(user)
        "/corp/#{ca.id}/#{corp_gen}/roles/#{roles}#{key}"
      end

      def role_string(user)
        Rails.cache.fetch("roles/#{user.id}") do
          user.role_string
        end
      end




                                OpenLogic Company Confidential   33

Wednesday, July 29, 2009
Benefits

                Never have to explicitly expire anything
                Can’t get an expired page
                      Generation numbers also stored in memcached
                Cache hierarchy is like a directory structure
                      Easy to grok
                      Can’t run out of “disk space”!




                                                   OpenLogic Company Confidential   34

Wednesday, July 29, 2009
Does it really work?

                In production for over a year - a few minor issues
                      See Gotchas
               Huge performance improvement for us, almost no
               developer pain
                      e.g., needed cookie-based “Welcome Joe!”
                Great for “passive” caching
                      Write caching code once, enjoy it forever
                      Lots of room for more aggressive caching




                                                 OpenLogic Company Confidential   35

Wednesday, July 29, 2009
Gotchas

             Easy to forget to make AJAX calls use REST
                    method:'get','post', 'put', or 'delete'
             Test with and without caching, and test the caching itself!
             Caching doesn’t help the first hit
                    Mitigate by pre-caching when feasible




                                                OpenLogic Company Confidential   36

Wednesday, July 29, 2009
Recommendations

               The “safety first” caching implementation - use it
               It’s still action caching, so don’t expect page caching
               performance
               Measure the result - not all pages will benefit and there
               is some overhead




                                         OpenLogic Company Confidential    37

Wednesday, July 29, 2009
Bonus

                Asynchronous pre-caching

                Event recording and playback




                                        OpenLogic Company Confidential   38

Wednesday, July 29, 2009
Async pre-caching pages at logon


               Use Workling/Starling for async
               When user logs on, make async call
               Async call runs through list of URL’s and hits them on
               behalf of the currently logged on user
               Need secret back-door logon in application.rb
                     Use big scary session key in environment.rb




                                                OpenLogic Company Confidential   39

Wednesday, July 29, 2009
Event Recording

               Q: What if you want to record the fact that something
               happened even though the page was automatically
               cached and served?
               A: Watch it while being cached, record what happens in
               memcached, play it back later when serving cached
               version




                                        OpenLogic Company Confidential   40

Wednesday, July 29, 2009
Summary

                           Rails built-in caching takes
                           a lot of on-going work
                               Easy to screw it up




                           Automatic caching can be written
                           once and enjoyed forever
                               Much harder to screw up


                           Still possible to use more aggressive
                           techniques selectively

                                                         OpenLogic Company Confidential   41

Wednesday, July 29, 2009
Any questions for Rod?

                                     OpenLogic Company Confidential   42

Wednesday, July 29, 2009
Resources


              memcached: http://www.danga.com/memcached/
              Workling: http://github.com/purzelrakete/workling/tree/
              master
              Starling: http://rubyforge.org/projects/starling/
              OLEX: http://olex.openlogic.com




                                        OpenLogic Company Confidential   43

Wednesday, July 29, 2009
Credits

               Unless otherwise indicated, photos were licensed from
               BigStockPhoto.com




                                       OpenLogic Company Confidential   44

Wednesday, July 29, 2009
OpenLogic is hiring!

                Rails guru?
                Live near Denver, Colorado?
                Love Open Source?

                Come see me!




                                        OpenLogic Company Confidential   45

Wednesday, July 29, 2009

More Related Content

What's hot

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
 
Apache Tomcat 8 Application Server
Apache Tomcat 8 Application ServerApache Tomcat 8 Application Server
Apache Tomcat 8 Application Server
mohamedmoharam
 
Tomcat Clustering
Tomcat ClusteringTomcat Clustering
Tomcat Clustering
gouthamrv
 

What's hot (18)

OpenSolaris Web Stack MySQL BOF
OpenSolaris Web Stack MySQL BOFOpenSolaris Web Stack MySQL BOF
OpenSolaris Web Stack MySQL BOF
 
The Future of Cloud Computing Today with Ubuntu 10.10
The Future of Cloud Computing Today with Ubuntu 10.10The Future of Cloud Computing Today with Ubuntu 10.10
The Future of Cloud Computing Today with Ubuntu 10.10
 
Vmware management-with-vcli-5.0
Vmware management-with-vcli-5.0Vmware management-with-vcli-5.0
Vmware management-with-vcli-5.0
 
Tomcat Optimisation & Performance Tuning
Tomcat Optimisation & Performance TuningTomcat Optimisation & Performance Tuning
Tomcat Optimisation & Performance Tuning
 
Magento caching
Magento cachingMagento caching
Magento caching
 
Hosting
HostingHosting
Hosting
 
Succeding with the Apache SOA stack
Succeding with the Apache SOA stackSucceding with the Apache SOA stack
Succeding with the Apache SOA stack
 
Empowering Next Generation Media
Empowering Next Generation MediaEmpowering Next Generation Media
Empowering Next Generation Media
 
Apache Tomcat 8 Application Server
Apache Tomcat 8 Application ServerApache Tomcat 8 Application Server
Apache Tomcat 8 Application Server
 
Apache tomcat
Apache tomcatApache tomcat
Apache tomcat
 
Building infrastructure for Big Data
Building infrastructure for Big DataBuilding infrastructure for Big Data
Building infrastructure for Big Data
 
Tomcat Configuration (1)
Tomcat Configuration (1)Tomcat Configuration (1)
Tomcat Configuration (1)
 
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
 
Deploying FuseMQ with Fuse Fabric
Deploying FuseMQ with Fuse FabricDeploying FuseMQ with Fuse Fabric
Deploying FuseMQ with Fuse Fabric
 
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
 
Where Does VMware Integration Occur?
Where Does VMware Integration Occur?Where Does VMware Integration Occur?
Where Does VMware Integration Occur?
 
Tomcat Clustering
Tomcat ClusteringTomcat Clustering
Tomcat Clustering
 
Tomcat server
 Tomcat server Tomcat server
Tomcat server
 

Viewers also liked (6)

New Mexico Budget Situation
New Mexico Budget SituationNew Mexico Budget Situation
New Mexico Budget Situation
 
Rails caching
Rails cachingRails caching
Rails caching
 
Feedback From Tourism Pros
Feedback From Tourism ProsFeedback From Tourism Pros
Feedback From Tourism Pros
 
Dona Ana County Extension Office
Dona Ana County Extension OfficeDona Ana County Extension Office
Dona Ana County Extension Office
 
TYPO3 Caching
TYPO3 CachingTYPO3 Caching
TYPO3 Caching
 
Langsame webseiten nerven okt-2013
Langsame webseiten nerven   okt-2013Langsame webseiten nerven   okt-2013
Langsame webseiten nerven okt-2013
 

Similar to Accelerate Your Rails Site with Automatic Generation-Based Action Caching

Karonis Rom Telecom
Karonis Rom TelecomKaronis Rom Telecom
Karonis Rom Telecom
knowhowgr
 
New Repository in AEM 6 by Michael Marth
New Repository in AEM 6 by Michael MarthNew Repository in AEM 6 by Michael Marth
New Repository in AEM 6 by Michael Marth
AEM HUB
 
Log management with Graylog2 - FrOSCon 2012
Log management with Graylog2 - FrOSCon 2012Log management with Graylog2 - FrOSCon 2012
Log management with Graylog2 - FrOSCon 2012
lennartkoopmann
 

Similar to Accelerate Your Rails Site with Automatic Generation-Based Action Caching (20)

Bay Area Drupal Camp Efficiency
Bay Area Drupal Camp EfficiencyBay Area Drupal Camp Efficiency
Bay Area Drupal Camp Efficiency
 
Karonis Rom Telecom
Karonis Rom TelecomKaronis Rom Telecom
Karonis Rom Telecom
 
JavaOne2015-What's in an Object?
JavaOne2015-What's in an Object?JavaOne2015-What's in an Object?
JavaOne2015-What's in an Object?
 
#JavaOne What's in an object?
#JavaOne What's in an object?#JavaOne What's in an object?
#JavaOne What's in an object?
 
Smalltalk Metaprogramming supports Probabilistic Program Analysis
Smalltalk Metaprogramming supports Probabilistic Program AnalysisSmalltalk Metaprogramming supports Probabilistic Program Analysis
Smalltalk Metaprogramming supports Probabilistic Program Analysis
 
OSGI,
OSGI,OSGI,
OSGI,
 
Logging & Docker - Season 2
Logging & Docker - Season 2Logging & Docker - Season 2
Logging & Docker - Season 2
 
JRubyConf 2009
JRubyConf 2009JRubyConf 2009
JRubyConf 2009
 
openark-kit: MySQL utilities for everyday use
openark-kit: MySQL utilities for everyday useopenark-kit: MySQL utilities for everyday use
openark-kit: MySQL utilities for everyday use
 
Comprehensive Monitoring for Docker
Comprehensive Monitoring for DockerComprehensive Monitoring for Docker
Comprehensive Monitoring for Docker
 
J9: Under the hood of the next open source JVM
J9: Under the hood of the next open source JVMJ9: Under the hood of the next open source JVM
J9: Under the hood of the next open source JVM
 
intl me this, intl me that
intl me this, intl me thatintl me this, intl me that
intl me this, intl me that
 
New Repository in AEM 6 by Michael Marth
New Repository in AEM 6 by Michael MarthNew Repository in AEM 6 by Michael Marth
New Repository in AEM 6 by Michael Marth
 
Arakoon: A distributed consistent key-value store
Arakoon: A distributed consistent key-value storeArakoon: A distributed consistent key-value store
Arakoon: A distributed consistent key-value store
 
Agile Project Management - coClarity
Agile Project Management - coClarityAgile Project Management - coClarity
Agile Project Management - coClarity
 
Log management with Graylog2 - FrOSCon 2012
Log management with Graylog2 - FrOSCon 2012Log management with Graylog2 - FrOSCon 2012
Log management with Graylog2 - FrOSCon 2012
 
No Really, It's All About You
No Really, It's All About YouNo Really, It's All About You
No Really, It's All About You
 
Don Schwarz App Engine Talk
Don Schwarz App Engine TalkDon Schwarz App Engine Talk
Don Schwarz App Engine Talk
 
GWT Overview And Feature Preview - SV Web JUG - June 16 2009
GWT Overview And Feature Preview - SV Web JUG -  June 16 2009GWT Overview And Feature Preview - SV Web JUG -  June 16 2009
GWT Overview And Feature Preview - SV Web JUG - June 16 2009
 
Doing More With Less: The Economics of Open Source Database Adoption
Doing More With Less: The Economics of Open Source Database AdoptionDoing More With Less: The Economics of Open Source Database Adoption
Doing More With Less: The Economics of Open Source Database Adoption
 

More from elliando dias

Why you should be excited about ClojureScript
Why you should be excited about ClojureScriptWhy you should be excited about ClojureScript
Why you should be excited about ClojureScript
elliando dias
 
Nomenclatura e peças de container
Nomenclatura  e peças de containerNomenclatura  e peças de container
Nomenclatura e peças de container
elliando dias
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agility
elliando dias
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Libraries
elliando dias
 
How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!
elliando dias
 
A Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the WebA Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the Web
elliando dias
 
Introdução ao Arduino
Introdução ao ArduinoIntrodução ao Arduino
Introdução ao Arduino
elliando dias
 
Incanter Data Sorcery
Incanter Data SorceryIncanter Data Sorcery
Incanter Data Sorcery
elliando dias
 
Fab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine DesignFab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine Design
elliando dias
 
Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.
elliando dias
 
Hadoop and Hive Development at Facebook
Hadoop and Hive Development at FacebookHadoop and Hive Development at Facebook
Hadoop and Hive Development at Facebook
elliando dias
 
Multi-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case StudyMulti-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case Study
elliando dias
 

More from elliando dias (20)

Clojurescript slides
Clojurescript slidesClojurescript slides
Clojurescript slides
 
Why you should be excited about ClojureScript
Why you should be excited about ClojureScriptWhy you should be excited about ClojureScript
Why you should be excited about ClojureScript
 
Functional Programming with Immutable Data Structures
Functional Programming with Immutable Data StructuresFunctional Programming with Immutable Data Structures
Functional Programming with Immutable Data Structures
 
Nomenclatura e peças de container
Nomenclatura  e peças de containerNomenclatura  e peças de container
Nomenclatura e peças de container
 
Geometria Projetiva
Geometria ProjetivaGeometria Projetiva
Geometria Projetiva
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agility
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Libraries
 
How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!
 
Ragel talk
Ragel talkRagel talk
Ragel talk
 
A Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the WebA Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the Web
 
Introdução ao Arduino
Introdução ao ArduinoIntrodução ao Arduino
Introdução ao Arduino
 
Minicurso arduino
Minicurso arduinoMinicurso arduino
Minicurso arduino
 
Incanter Data Sorcery
Incanter Data SorceryIncanter Data Sorcery
Incanter Data Sorcery
 
Rango
RangoRango
Rango
 
Fab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine DesignFab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine Design
 
The Digital Revolution: Machines that makes
The Digital Revolution: Machines that makesThe Digital Revolution: Machines that makes
The Digital Revolution: Machines that makes
 
Hadoop + Clojure
Hadoop + ClojureHadoop + Clojure
Hadoop + Clojure
 
Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.
 
Hadoop and Hive Development at Facebook
Hadoop and Hive Development at FacebookHadoop and Hive Development at Facebook
Hadoop and Hive Development at Facebook
 
Multi-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case StudyMulti-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case Study
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

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...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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?
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
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...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
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
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
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
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 

Accelerate Your Rails Site with Automatic Generation-Based Action Caching

  • 1. Accelerate Your Rails Site with Automatic Generation-Based Action Caching Rod Cope, CTO and Founder OpenLogic, Inc. Wednesday, July 29, 2009
  • 2. Goal Built-in caching is hard: Learn how to automate it to make the pain go away OpenLogic Company Confidential 2 Wednesday, July 29, 2009
  • 3. Agenda Introduction Built-in caching Automatic generation-based caching Bonus material Conclusion & recommendations OpenLogic Company Confidential 3 Wednesday, July 29, 2009
  • 4. Introduction Rod Cope CTO & Founder of OpenLogic 25 years of software development experience IBM Global Services, Anthem, Ericsson, many more OpenLogic, Inc. Governance, SLA support, security updates, and indemnification for over 500 Open Source packages Dozens of Fortune 500 customers OSS Census (osscensus.org) Global, community effort to catalog use of open source OpenLogic Company Confidential 4 Wednesday, July 29, 2009
  • 5. No Spoon Feeding OpenLogic Company Confidential 5 Wednesday, July 29, 2009
  • 6. Don’t Panic scotduke.com OpenLogic Company Confidential 6 Wednesday, July 29, 2009
  • 7. Reasons for Caching Lots of hits, speed is king Google OpenLogic Company Confidential 7 Wednesday, July 29, 2009
  • 8. OpenLogic Company Confidential 8 Wednesday, July 29, 2009
  • 9. Reasons for Caching Lots of hits, speed is king Google Complex hits, richness is king OpenLogic’s OLEX OpenLogic Company Confidential 9 Wednesday, July 29, 2009
  • 10. OpenLogic Company Confidential 10 Wednesday, July 29, 2009
  • 11. Rails Caching Background Page caching Uberfast, little control Highly dynamic site? Move on. Action caching Fast, lots of control Expiration hell Fragment caching Expiration hell OpenLogic Company Confidential 11 Wednesday, July 29, 2009
  • 12. Problems with Rails Caching Caching is easy to implement “Site’s borked again - turn off caching.” Hard to clear (the right) caches when you’re mixing and matching different types, styles, developers, plugins, etc. OpenLogic Company Confidential 12 Wednesday, July 29, 2009
  • 13. Automatic Generation-Based Caching Generation-based Partition the cache so that every state change increments the “generation” Automatic Any action that could possibly change state bumps the count Let memcached overwrite old generations Very conservative No domain knowledge Don’t cache errors, AJAX, redirects, flash notices, etc. OpenLogic Company Confidential 13 Wednesday, July 29, 2009
  • 14. Overview Non-cached Cached Rails Rails plumbing plumbing Your Cache code helper Render Database memcached View OpenLogic Company Confidential 14 Wednesday, July 29, 2009
  • 15. Cache = Hash Key Value /packages/1 <html><body>Rails</body></html> /packages/2 <html><body>Ruby</body></html> OpenLogic Company Confidential Wednesday, July 29, 2009
  • 16. Generational Cache Key Value /gen/1/packages/1 <html>Rails</html> /gen/1/packages/2 <html>Ruby</html> OpenLogic Company Confidential Wednesday, July 29, 2009
  • 17. Generational Cache Key Value /gen/1/packages/1 <html>Rails</html> /gen/1/packages/2 <html>Ruby</html> /gen/2/packages/1 <html>Ruby on Rails</html> OpenLogic Company Confidential Wednesday, July 29, 2009
  • 18. memcached is your friend Very fast Don’t worry about removing old entries memcached will automatically drop the oldest keys when it runs low on memory Run it on your web servers if you have the RAM OpenLogic Company Confidential 18 Wednesday, July 29, 2009
  • 19. Cache Helper No cache-related code sprinkled throughout every model, view, and/or controller Use a global “around” filter Automatically increment (scoped) generation count upon POST, PUT, or DELETE Handle event recording and playback (optional) OpenLogic Company Confidential 19 Wednesday, July 29, 2009
  • 20. Cache Helper Code (in “around” filter) key = make_auto_cache_key(request.request_uri) output = Rails.cache.read(key) if output render :text => output return else yield unless response.redirected_to || flash[:notice] Rails.cache.write(key, response.body) end end OpenLogic Company Confidential 20 Wednesday, July 29, 2009
  • 21. Cache Key def make_auto_cache_key(key) ol_gen = Rails.cache.fetch(OL_GEN) { "1" } "olex/#{ol_gen}" end OpenLogic Company Confidential 21 Wednesday, July 29, 2009
  • 22. “Clear” the Cache POST, PUT, or DELETE “clears” the cache def maybe_clear_auto_cache return if request.get? gen = Rails.cache.fetch(OL_GEN) { "1" } Rails.cache.write(OL_GEN, (gen.to_i + 1).to_s) end OpenLogic Company Confidential 22 Wednesday, July 29, 2009
  • 23. Controller Customization Some controllers may need special cache control (e.g., user-specific cache, disable cache for certain methods) olex_auto_cache_is_specific_to_user :only => :show Note: the cache may still need to be cleared when skipping cache usage! skip_filter :olex_auto_cache after_filter :maybe_clear_olex_auto_cache OpenLogic Company Confidential 23 Wednesday, July 29, 2009
  • 24. Cache Partitioning Make it impossible for users of different corporations and permission levels to see each other’s stuff, access the same cache, clear somebody else’s cache, etc. Make sure any “global” changes clear all caches Use a cache hierarchy /olex/<gen #>/corp/<corp #>/<corp gen #>/roles/<role #s>/URL MD5 the key (memcached has a 250 char limit) Extra credit: Use the key as an etag OpenLogic Company Confidential 24 Wednesday, July 29, 2009
  • 25. Partitioned Cache Global generation <html> /olex/13/corp/72/2/roles/2,7,19/packages/2 Rails (unsupported) </html> OpenLogic Company Confidential Wednesday, July 29, 2009
  • 26. Partitioned Cache Corporate account ID <html> /olex/13/corp/72/2/roles/2,7,19/packages/2 Rails (unsupported) </html> OpenLogic Company Confidential Wednesday, July 29, 2009
  • 27. Partitioned Cache Corporation #72’s generation <html> /olex/13/corp/72/2/roles/2,7,19/packages/2 Rails (unsupported) </html> OpenLogic Company Confidential Wednesday, July 29, 2009
  • 28. Partitioned Cache Current user’s role IDs <html> /olex/13/corp/72/2/roles/2,7,19/packages/2 Rails (unsupported) </html> OpenLogic Company Confidential Wednesday, July 29, 2009
  • 29. Partitioned Cache URL <html> /olex/13/corp/72/2/roles/2,7,19/packages/2 Rails (unsupported) </html> OpenLogic Company Confidential Wednesday, July 29, 2009
  • 30. Partitioned Cache <html> /olex/13/corp/72/2/roles/2,7,19/packages/2 Rails (unsupported) </html> <html> /olex/13/corp/72/2/roles/2,7,19,22/packages/2 Rails (manager secret!) </html> <html> /olex/14/corp/72/2/roles/2,7,19/packages/2 Rails (supported) </html> /olex/14/guest/packages/2 <html>Rails</html> OpenLogic Company Confidential Wednesday, July 29, 2009
  • 31. Cache Key def make_auto_cache_key(key, user = nil) ol_gen = Rails.cache.fetch(OL_GEN) { "1" } ol_prefix = "olex/#{ol_gen}" if user.nil? && self.respond_to?("current_user") user = current_user end ca = get_corporate_account(user) ... OpenLogic Company Confidential 31 Wednesday, July 29, 2009
  • 32. Cache Key ... if user.is_corp_user? corp_gen = Rails.cache.fetch(corp_gen_key(ca)){"1"} final_key = make_corp_key(user, ca, key, corp_gen) else final_key = "guest#{key}" end OpenLogic Company Confidential 32 Wednesday, July 29, 2009
  • 33. Corporate Cache Key def make_corp_key(user, ca, key, corp_gen) roles = role_string(user) "/corp/#{ca.id}/#{corp_gen}/roles/#{roles}#{key}" end def role_string(user) Rails.cache.fetch("roles/#{user.id}") do user.role_string end end OpenLogic Company Confidential 33 Wednesday, July 29, 2009
  • 34. Benefits Never have to explicitly expire anything Can’t get an expired page Generation numbers also stored in memcached Cache hierarchy is like a directory structure Easy to grok Can’t run out of “disk space”! OpenLogic Company Confidential 34 Wednesday, July 29, 2009
  • 35. Does it really work? In production for over a year - a few minor issues See Gotchas Huge performance improvement for us, almost no developer pain e.g., needed cookie-based “Welcome Joe!” Great for “passive” caching Write caching code once, enjoy it forever Lots of room for more aggressive caching OpenLogic Company Confidential 35 Wednesday, July 29, 2009
  • 36. Gotchas Easy to forget to make AJAX calls use REST method:'get','post', 'put', or 'delete' Test with and without caching, and test the caching itself! Caching doesn’t help the first hit Mitigate by pre-caching when feasible OpenLogic Company Confidential 36 Wednesday, July 29, 2009
  • 37. Recommendations The “safety first” caching implementation - use it It’s still action caching, so don’t expect page caching performance Measure the result - not all pages will benefit and there is some overhead OpenLogic Company Confidential 37 Wednesday, July 29, 2009
  • 38. Bonus Asynchronous pre-caching Event recording and playback OpenLogic Company Confidential 38 Wednesday, July 29, 2009
  • 39. Async pre-caching pages at logon Use Workling/Starling for async When user logs on, make async call Async call runs through list of URL’s and hits them on behalf of the currently logged on user Need secret back-door logon in application.rb Use big scary session key in environment.rb OpenLogic Company Confidential 39 Wednesday, July 29, 2009
  • 40. Event Recording Q: What if you want to record the fact that something happened even though the page was automatically cached and served? A: Watch it while being cached, record what happens in memcached, play it back later when serving cached version OpenLogic Company Confidential 40 Wednesday, July 29, 2009
  • 41. Summary Rails built-in caching takes a lot of on-going work Easy to screw it up Automatic caching can be written once and enjoyed forever Much harder to screw up Still possible to use more aggressive techniques selectively OpenLogic Company Confidential 41 Wednesday, July 29, 2009
  • 42. Any questions for Rod? OpenLogic Company Confidential 42 Wednesday, July 29, 2009
  • 43. Resources memcached: http://www.danga.com/memcached/ Workling: http://github.com/purzelrakete/workling/tree/ master Starling: http://rubyforge.org/projects/starling/ OLEX: http://olex.openlogic.com OpenLogic Company Confidential 43 Wednesday, July 29, 2009
  • 44. Credits Unless otherwise indicated, photos were licensed from BigStockPhoto.com OpenLogic Company Confidential 44 Wednesday, July 29, 2009
  • 45. OpenLogic is hiring! Rails guru? Live near Denver, Colorado? Love Open Source? Come see me! OpenLogic Company Confidential 45 Wednesday, July 29, 2009