Cache Money 2009 - Rails Caching & Optimization

  • Uploaded by: Nathan Bertram
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Cache Money 2009 - Rails Caching & Optimization as PDF for free.

More details

  • Words: 1,163
  • Pages: 36
Cache Money

Rails Caching & Optimization

By: Nathan Bertram

Thursday, July 16, 2009

What is Caching? - Ruby is an Interpreted Language. - Every-time a request is made code gets executed on the fly possibly with some database requests. - Caching is the art of taking this “process” and storing in temporary location. DRY. - Pick your strategies and stick to them. - The goal is to increase page responsiveness and to consume less resources.

Thursday, July 16, 2009

What is Caching? Page Responsiveness - Simply the amount of time that it takes to load a webpage on a browser.

Faster loading pages = Happier users

=)

Two Questions we should be asking: A. How can we measure responsiveness? B. How can we improve responsiveness?

Thursday, July 16, 2009

Before we begin. Maybe I should explain how the browser works?

How can we measure responsiveness? Firefox: Firebug Plugin

Thursday, July 16, 2009

How can we measure responsiveness? Safari: Web Inspector

Thursday, July 16, 2009

How can we measure responsiveness? Firefox: Firebug Plugin with Yahoo! YSlow Plugin

Thursday, July 16, 2009

How can we improve responsiveness? In addition to Code & Query Optimization. We can improve: 1. Client Side Caching - Asset Caching & Optimization - Headers 2. Server Side Caching - Page Caching - Action Caching - Fragment Caching Focus on the greatest return in performance/responsiveness for time invested.

Thursday, July 16, 2009

Client Side Caching

What are assets? Any file we load in our page: blah.css, blah.js, blah.jpg, blah.swf & etc.

Thursday, July 16, 2009

Asset Caching Client Requests Overview Joe Client Visits: kmansblog.com

Thursday, July 16, 2009

Asset Caching Client Requests Overview Joe Client Visits: kmansblog.com

Server: Sends HTML File

Thursday, July 16, 2009

Asset Caching Client Requests Overview Joe Client Visits: kmansblog.com

Server: Sends HTML File

Thursday, July 16, 2009

Joe Client Reads HTML File

Asset Caching Client Requests Overview Joe Client Visits: kmansblog.com

Server: Sends HTML File

Thursday, July 16, 2009

GotStyle.css

Joe Client Reads HTML File

Asset Caching Client Requests Overview Joe Client Visits: kmansblog.com

Server: Sends HTML File

GotStyle.css

jquery.js

Joe Client Reads HTML File

lolcat.jpg

jumpingWookie.png

Thursday, July 16, 2009

Default.js

PirateFail.jpg

Asset Caching Reduce the Total Number of Requests - Combine CSS files where possible - Combine JS files where possible - Use CSS Sprites where possible Out of the box combines your assets:

Thursday, July 16, 2009

Verify what the IE6 cap is. What % of our users use IE6?

Asset Caching Reduce the Total Number of Requests Bundle-Fu

- It will minify all your js for you.

Thursday, July 16, 2009

Asset Caching Quick Mention: CDN - Content Delivery Networks

Serve up assets from closest geographically CDN in relation to user.

Thursday, July 16, 2009

Expires Header HTTP Header: Expires: Wed, Jan 1 2012 18:00:00 GMT Joe Client

Thursday, July 16, 2009

Server

Expires Header HTTP Header: Expires: Wed, Jan 1 2012 18:00:00 GMT Joe Client

Server

Browser Cache

1. User Requests Asset from the Server 2. User Receives & Stores the file in the local browser cache 3. Next time the user visits that website, the browser checks the local cache. 4. If the local browser cache has the same file and it hasn’t expired the browser simply doesn’t bother downloading a new copy of that file because it already has that given file in its local cache.

Thursday, July 16, 2009

Expires Header On Apache you can:

Thursday, July 16, 2009

Expires Header On Apache you can: Expires: Wed, Jan 1 2012 18:00:00 GMT

But..... There is a problem with this. What happens if you decide to update your content. Your screwed.

Thursday, July 16, 2009

Expires Header

Thursday, July 16, 2009

Expires Header

Rails Tricks the Browser.

Problem solved.

Thursday, July 16, 2009

Find how this can be overridden.

Others Headers Max-Age - Amount of time from when the file was recieved to when it expires. etag - MD5 Hash of the output of the page. Just like git commits. last_modified - Compares the modified date in cached to the one in the server. Just like etags except using date instead of MD5 hash.

Useful with Fragment & object caching. Useful for Reverse-Proxy-Caching [End] Thursday, July 16, 2009

Server Side Caching Page Caching - The simplest form of rails caching. - Saves generated files .html, .json, .iphone to the file system. - Subsequent requests Apache then serves up these files without the involvement of the dispatcher. - Mongrels ~20-60 Requests Per Second (~2 million hits/day) / instance. - Apache 1000+ Requests Per Second (~86 million hits/day)/instance. - Will not write to your production.log file. - By default files are stored in /public/controller/action.format

Thursday, July 16, 2009

Page Caching Usage Use when content on entire page doesn’t change very often. This can even be an entire page that only changes once every hour.

Thursday, July 16, 2009

Page Caching Usage You can use it with pagination by adding a custom route.

Thursday, July 16, 2009

Page Caching Usage You can use it with pagination by adding a custom route.

Say we want to DRY this logic up a bit. We can: A. Add logic to applicaiton.rb B. Add logic to some kind of shared object.

Thursday, July 16, 2009

Sweepers - An ActiveRecord Observer object. - Specialized for expiring cached content. - Sweepers may implement all ActiveRecord::Observer callbacks.

Thursday, July 16, 2009

Server Side Caching There is a problem with page caching. Often we need to execute controller filters prior to serving the cached files.

Thursday, July 16, 2009

Server Side Caching Problem with page caching is often we need to execute controller filters prior to serving the cached files. Cache Storage FileStore - Keeps cached files on file system. MemoryStore - Keeps cached files in memory. *warning* DrbStore - Keeps cached files in memory of a seperate, Drb process. Drb == Distributed Ruby. MemCachedStore - Keeps cached files in proven cache server memcached.

Thursday, July 16, 2009

Server Side Caching Action Caching - Almost like page caching except the controller filters are executed prior Example: Run Authentication. If authenticated, serve cached page up to client

Thursday, July 16, 2009

Server Side Caching Fragment Caching - When you need more granular control over what parts of the page need to be cached. Probably because certain parts of the page are unique to the user.

Thursday, July 16, 2009

Server Side Caching

It’s alot of work maintaing all this expiration. Memcached.

Thursday, July 16, 2009

Server Side Caching Memcached. Simply a {} in memory. 1. Object Store 2. Fragment Cache Store [Action & Fragment Caching] sudo port install memcached (runs as seperate process) memcached -m 1500 (1.5 Gigs).

Thursday, July 16, 2009

Server Side Caching Memcached.

Thursday, July 16, 2009

The End. Summary. 1. Client Side Caching - Asset Caching & Optimization - Headers 2. Server Side Caching - Page Caching - Action Caching - Fragment Caching Other Related Topics: - Reverse-Proxy-Caching - Rack/Middleware - Load Testing (autobench)

Thursday, July 16, 2009

Related Documents

Http Caching & Cache-busting
November 2019 15
Caching
November 2019 47
Cache
May 2020 12
Cache
November 2019 21
Cache
May 2020 20

More Documents from ""