Ruby, Rails & 5 Ways to Improve Performance

When I was speaking last night at the BIMA & Ultraspeed Virtualization event on virtualization and web application development, I was surprised to find that not a lot of people know about the simple ways that you can improve performance without much development or systems administration time at all.

In fact, most of the techniques that I’m going to go over aren’t new, and are pretty well publicized. Both Google and Yahoo! both go over them in tons of detail.

1. Utilise entity tags (“Etags”)

Some web servers (like nginx, which is what I tend to use in production) do not support entity tags natively on the server-side. Rails (as of version 2.2) has added entity tag support to its middleware, which helps to save a little bandwidth.

2. Use multiple asset hosts

Most major browsers by default only allow two concurrent connections, which means that if you have a content-heavy or rich site, chock full of JavaScript, CSS files, and images, you’re going to be waiting a while to download all of these files. This is especially true if you’re not using any caching methods, which a lot of websites (sadly) don’t.

Asset host diagram

You can create as many asset hosts that you want using aliases in your web server. For example, I could create asset0-8.voxxit.com, and point it to the same Rails application.

Once you’ve done that, all that needs to be done is setting up the code in your production environment file:

Be aware that you’ll need to use the asset tags (such as javascript_include_tag, stylesheet_link_tag, image_tag, etc.) in order to take advantage of this.

3. (Intelligently) bundle JavaScript and CSS files together

In production, less is more. Basically, if you have thirty CSS files along with 10 JavaScript files running around in an unorganised fashion, not only will it make your life a nightmare as your site grows, but it will make your site incredibly slow and give you a terrible load time.

GitHub has written a cool article which I have taken up in our production environments about bundling these files together, and it has worked quite well for us. Once they are bundled on deployment, they are cached, and are only downloaded by the user if they have changed. Also, when they need to download them again, its only done once, and their entire experience is brilliant from that point on.

It should be noted that this process minifies the JavaScript with the Google Closure Compiler and CSS with YUI Compressor. This is due to the benefits that each does for the different file formats. Trust me, it works great!

4. Utilise Google’s AJAX Libraries API

Using Google’s highly available, distributed, and most importantly, cached content network, you can possibly speed up your site even more.

Let’s say you’re using jQuery on your site. If you’re using Google’s hosted version of jQuery, and a user has encountered that file before, it will already be included in their browser’s cache. They won’t need to download the file again, and your site will instantly be faster.

5. Enable compression

On the server side, you can enable compression of static files such as HTML, XML, CSS and JavaScript files. Here is what our setup looks like in nginx:

Here are a couple more resources for compression on different web servers:

  • Apache: http://gist.github.com/282826
  • IIS: http://bit.ly/6bHc5v

Conclusion

There are many more things you can do in your Rails app to increase performance, such as using NewRelic to chase long-running database queries, and caching. I hope this helps at least one person in their quest to gain performance from their app. As always, I’m here to help in the comments, or @voxxit.

blog comments powered by Disqus