InfoQ

News

Smooth HTTP Caching With Rack::Cache

Posted by Sebastien Auvray on Nov 16, 2008 06:00 PM

Community
Ruby
Topics
Performance & Scalability ,
Web Frameworks ,
Ruby on Rails
Tags
Ruby on Rails ,
Scalability ,
Rails ,
Caching ,
HTTP

The ways to cache a web application are numerous and often complex. Basic Rails page caching can become tedious to manage as your application infrastructure grows.
Rails 2.2 introduced conditional GET through the use of http headers: last_modified and etag. Following internet standard caching section of RFC2616, Ryan Tomayko introduced Rack::Cache.

Rack::Cache is a piece of Rack middleware that implements most of RFC 2616's caching features with a basic set of storage options (disk, heap, and memcached) and a configuration system for tweaking cache policy. It should work equally well with any Rack-enabled Ruby web framework and is tested on Ruby 1.8.6 and 1.8.7.

Part of its design was inspired by the Python cache framework Django.

Rack::Cache acts as a gateway proxy (Varnish , Squid) without all the pain to settle it. Rack::Cache supports expiration-based caching, validation model, and Vary header fields.

As Ryan King states, you can migrate smoothly to real gateway proxy if your application really needs it:

Once your application gets larger and more complex, it makes sense to use an http reverse proxy cache like squid or varnish, but transitioning from rails-style page caching to HTTP caching is non-trivial. You have to change your deployment and your application in significant ways. Not fun.

With Rack::Cache you only have to change your deployment. You can even do it incrementally. You could start by using Rack::Cache to cache in the heap, later switch to the filesystem and finally to memcache. When that reaches its scalability limits you can add squid or varnhish in front of your application, then remove Rack::Cache. Deployments that involve only one major change at each step are much easier to get right than trying to make several big changes in a single operation.

It will be interesting to have a look at Ryan's upcoming benchmarks.

No comments

Reply

Educational Content

JRuby: The Pain of Bringing an Off-Platform Dynamic Language to the JVM

Charles Nutter discusses bringing JRuby to the JVM, why Ruby is hard to implement, JIT compilation, precompilation, core Ruby implementation, Java library access, library challenges and future plans.

Performance Anti-Patterns in Database-Driven Applications

Alois Reitbauer specifies several architectural anti-patterns that one should stay away from and which can downgrade an application’s performance.

Making TDD Stick: Problems and Solutions for Adopters

Teams in large organizations still struggle to adopt TDD. In this article Mark Levison shares problems he uncovered when he surveyed teams, and his own strategy to introduce TDD into an organization.

Testing is Overrated

In this talk from RubyFringe, Luke Francl asks: is developer-driven testing really the best way to find software defects? Or is the emphasis on testing and test coverage barking up the wrong tree?

VM Optimizations for Language Designers

John Pampuch discusses the HotSpot compiler, the history of Java performance, HotSpot development philosophies and challenges, optimization, JVM library improvements, and tips for better performance.

Keith Braithwaite, an Agile Skeptic

In this interview, Keith Braithwaite, an Agile developer, consultant and trainer, says that we should show a good deal of skepticism towards today’s Agile practice.

Workflow Orchestration Using Spring AOP and AspectJ

This article demonstrates how to build and orchestrate highly configurable and extensible yet light-weight embedded process flow using AOP techniques with Spring AOP and Aspect J.

Embrace Uncertainty

Jeff Patton explains why one needs to embrace uncertainty in order to succeed with his/her Agile project and how to avoid some of the common mistakes leading to project failure.