Redirect caching deep dive

July 23, 2010 12:34 am | 4 Comments

I was talking to a performance guru today who considers redirects one of the top two performance problems impacting web pages today. (The other was document.write.) I agree redirects are an issue, so much so that I wrote a chapter on avoiding redirects in High Performance Web Sites. What makes matters worse is that, even though redirects are cacheable, most browsers (even the new ones) don’t cache them.

Another performance guru, Eric Lawrence, sent me an email last week pointing out how cookies, status codes, and response headers affect redirect caching. Even though there are a few redirect tests in Browserscope, they don’t test all of these conditions. I wanted a more thorough picture of the state of redirect caching across browsers.

The Redirect Caching Tests page provides a test harness for exercising different redirect caching scenarios.

You can use the “Test Once” button to test a specific scenario, but if you choose “Test All” the harness runs through all the tests and offers to post the results (anonymously) to Browserscope. Here’s a snapshot of the results:

I realize you can’t read the results, but suffice it to say red is bad. If you click on the table you’ll go to the full results. They’re broken into two tables: redirects that should be cached, and redirects that should not be cached. For example, a 301 response with an expiration date in the future should be cached, but a 302 redirect with an expiration date in the past shouldn’t be cached. The official ruling can be found in RFC 2616. (Also, discussions Eric had with Mark Nottingham, chair of the Httpbis working group, indicate that 303 redirects with a future expiration date should be cached.)

Chrome, iPhone, and Opera 10.60 are doing the best job, but there’s still a lot of missed opportunities. IE 9 platform preview 3 still doesn’t cache any redirects, but Eric’s blog post, Caching Improvements in Internet Explorer 9, describes how that’ll be in the final version of IE9. If you use redirects that don’t change, make sure to use a 301 status code and set an expiration date in the future. That will ensure they’re cached in Chome, Firefox, iPhone, Opera, and IE9.

Please help out by running the test on your browser (especially mobile!) and contributing the results back to Browserscope so we can help browser vendors know what needs to be fixed.

4 Responses to Redirect caching deep dive

  1. A key point in all of this is to ensure that your site works correctly when redirects are cached. For instance, if you set a Cookie on your redirect response, that cookie must either be optional, or you need to set a Vary: Cookie response header. If you fail to do that, visitors to your site may find themselves in a redirect loop. The reason that browser vendors are so reluctant to improve caching of redirects is the fear that incorrectly coded real-world sites will have such problems.

    (Also, it almost goes without saying, you should avoid redirects wherever possible. For instance, instead of using A HREF=, use A HREF= if that’s the real URL!)

  2. Great post but I’m still confused about the redirects. Can I also use htaccess to run redirect commands? Thanks.
    Web Design Springfield MO

  3. The full results page seems to be broken at the moment? gives

    Warning: Invalid argument supplied for foreach() in /home/sowrock/ on line 65

    and no tables are rendered.

  4. James: Thanks for pointing this out. There’s a problem with Browserscope. Investigating now.