Accelerate Your Rails Site with Automatic Generation-Based Action Caching. Rod Cope, CTO and Founder OpenLogic, Inc.



Similar documents
The Social Accelerator Setup Guide

Installing an open source version of MateCat

Smartphone Enterprise Application Integration

1. Comments on reviews a. Need to avoid just summarizing web page asks you for:

Scaling Rails with memcached

AGENDA: INTRODUCTION: 1. How is our cloud monitoring setup? 2. Which are the tools used? 3. How do we access monitoring dashboard?

Citrix Desktop Virtualization Fast Track

Rails Cookbook. Rob Orsini. O'REILLY 8 Beijing Cambridge Farnham Koln Paris Sebastopol Taipei Tokyo

Building Views and Charts in Requests Introduction to Answers views and charts Creating and editing charts Performing common view tasks

Top 10 Lessons Learned from Deploying Hadoop in a Private Cloud. Rod Cope, CTO & Founder OpenLogic, Inc.

FilesAnywhere Feature List

1 (11) Paperiton DMS Document Management System System Requirements Release: 2012/

PHP on IBM i: What s New with Zend Server 5 for IBM i

Cache All The Things

Hands-On Microsoft Windows Server 2008

Ajax: A New Approach to Web Applications

Big Fast Data Hadoop acceleration with Flash. June 2013

CMB 207 1I Citrix XenApp and XenDesktop Fast Track

CYCLOPE let s talk productivity

Kentico CMS security facts

Installation, Configuration, and Usage

and Active Directory Adding, Changing and Deleting Accounts, Compiling Addresses for Mail Lists

IDrive, is a service offered by Pro Softnet Corporation, an ASP and Internet Solutions Provider, based in Woodland Hills, CA.

I-Motion SQL Server admin concerns

Returning to Work is a Lot of Work

Global Outsourcing / Infrastructure Management. Instinct 2.0. Bridging the Gap between the Disparate Needs of Organizations and End-Users

IIS SECURE ACCESS FILTER 1.3

Lecture 6 Cloud Application Development, using Google App Engine as an example

HPUG_1014. Shaw Home Phone. User Guide

Portal User Guide. Customers. Version 1.1. May of 5

Citrix Training. Course: Citrix Training. Duration: 40 hours. Mode of Training: Classroom (Instructor-Led)

Windows PCs & Servers are often the life-blood of your IT investment. Monitoring them is key, especially in today s 24 hour world!

Session 17 Windows 7 Professional DNS & Active Directory(Part 2)

Find the Who, What, Where and When of Your Active Directory

Portals and Hosted Files

The Future of PostgreSQL High Availability Robert Hodges - Continuent, Inc. Simon Riggs - 2ndQuadrant

Introduction. Just So You Know... PCI Can Be Difficult

Front-End Performance Testing and Optimization

System Requirement Specification for A Distributed Desktop Search and Document Sharing Tool for Local Area Networks

Cloud Storage Service

Simple Tips to Improve Drupal Performance: No Coding Required. By Erik Webb, Senior Technical Consultant, Acquia

Enterprise Remote Control 5.6 Manual

Compliance series Guide to meeting requirements of USGCB

SiteWit JavaScript v3 Documentation

Events Forensic Tools for Microsoft Windows

Repsheet. A Behavior Based Approach to Web Application Security. Aaron Bedra Application Security Lead Braintree Payments. tirsdag den 1.

COS 318: Operating Systems

Lotus Domino 8 Monitoring and Maintenance

Varnish Tips & Tricks, 2015 edition

Mirtrak 6 Powered by Cyclope

Use Enterprise SSO as the Credential Server for Protected Sites

CMB-207-1I Citrix Desktop Virtualization Fast Track

HBase Schema Design. NoSQL Ma4ers, Cologne, April Lars George Director EMEA Services

Cognos Performance Troubleshooting

LYONSCG ECOMMERCE ACCELERATOR (LEA) FOR MAGENTO. Discussion of Features

Cloud Monitoring and Auditing with CADF (Cloud Auditing and Data Federation)

Data Deduplication in Tivoli Storage Manager. Andrzej Bugowski Spała

skype ID: store.belvg US phone number:


BillQuick Web i Time and Expense User Guide

IIS Media Services 3.0 Overview. Microsoft Corporation

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective

Continuous Integration

Zmanda Cloud Backup Frequently Asked Questions

Index. AdWords, 182 AJAX Cart, 129 Attribution, 174

SHORT GUIDE TO: SETTING UP YOUR HOME OFFICE IN STYLE

MAGENTO HOSTING Progressive Server Performance Improvements

Layers of Caching: Key to scaling your website. Lance Albertson -- Narayan Newton

JBoss & Infinispan open source data grids for the cloud era

Big Data with Component Based Software

Security IIS Service Lesson 6

In-Memory Analytics: A comparison between Oracle TimesTen and Oracle Essbase

Enterprise Network Deployment, 10,000 25,000 Users

NetIQ Advanced Authentication Framework - Client. User's Guide. Version 5.1.0

HDVideoShare! User Documentation Team January

7 Tips for Achieving Active Directory Compliance. By Darren Mar-Elia

Managing the Cloud as an Incremental Step Forward

Integrating OID with Active Directory and WNA

Web Performance. Sergey Chernyshev. March '09 New York Web Standards Meetup. New York, NY. March 19 th, 2009

SAP Digital CRM. Getting Started Guide. All-in-one customer engagement built for teams. Run Simple

Cross Bulk Mailer 6.1 User Guide

Citrix XenApp 6.5 Administration

CMB-207-1I Citrix XenApp and XenDesktop Fast Track

USING THE LUMI SHOW EVENT APP SAMRA 2014

Mobile Internet for the Next Two Billion

NovaBACKUP. Storage Server. NovaStor / May 2011

Accelerating Wordpress for Pagerank and Profit

Getting Started with AWS. Hosting a Static Website

Building Dynamic Web 2.0 Websites with Ruby on Rails

Confinement Problem. The confinement problem Isolating entities. Example Problem. Server balances bank accounts for clients Server security issues:

Legacy (RedCloud) Security Management Software 4.2 Release Notes

Transcription:

Accelerate Your Rails Site with Automatic Generation-Based Action Caching Rod Cope, CTO and Founder OpenLogic, Inc.

Goal Built-in caching is hard: Learn how to automate it to make the pain go away 2

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

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 4

No Spoon Feeding 5

Don t Panic scotduke.com 6

Reasons for Caching Lots of hits, speed is king Google 7

8

Reasons for Caching Lots of hits, speed is king Google Complex hits, richness is king OpenLogic s OLEX 9

10

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 11

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. 12

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. 13

Overview Non-cached Cached Rails plumbing Rails plumbing Your code Cache helper Database Render View memcached 14

Cache = Hash Key Value /packages/1 <html><body>rails</body></html> /packages/2 <html><body>ruby</body></html>

Generational Cache Key Value /gen/1/packages/1 <html>rails</html> /gen/1/packages/2 <html>ruby</html>

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>

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 18

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) 19

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 20

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

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 22

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 23

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 24

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

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

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

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

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

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

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)... 31

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 32

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 33

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! 34

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 35

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 36

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 37

Bonus Asynchronous pre-caching Event recording and playback 38

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 39

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 40

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 41

Any questions for Rod? 42

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 43

Credits Unless otherwise indicated, photos were licensed from BigStockPhoto.com 44

OpenLogic is hiring! Rails guru? Live near Denver, Colorado? Love Open Source? Come see me! 45