Use Rack::Deflater to get faster first time loads of your app

Is your Ruby on Rails application slow for end users? Maybe you are sending a lot of data through the network. As we rarely test performance on our basic server to client setup, and I don’t mean the maxed out broadband speeds we like to have at our places of work, but regular DSL, or mobile internet user, with the minimal internet speed available.
We also have the false security while browser testing the application on the same machine we develop it on. And it’s really fast to fetch 10MB from localhost:3000 into the browser.
Do you think that pulling even a 1MB response is nice when the DSL speed is 2mbps and that means around 4 seconds to fetch it. Only your assets can grow to that size if you are not careful, and this is far from the response size of larger websites. For example, youtube.com has a payload of ~1.5MB (at least at the time of writing, my front page). And it takes around 4.55 seconds to render on my DSL which is around 10mbps. For a regular DSL user (~2mbps) the time it takes is 8 seconds, and the difference is, of course, the time it takes to receive the 1.5MB of data. For a mobile user it takes even longer because let’s be honest, no one can achieve those claimed 150mbps LTE speeds.
There is a very quick solution to reduce the payload of your Rails (and other Rack based apps) by including just one line of code in yours config/application.rb.

config.middleware.use Rack::Deflater

That will automatically deflate your server responses (which is a fancy word for compression that the browser knows how to uncompress), and you will be serving substantially smaller responses. Of course, your web server must be configured to enable compression, and there is a great guide for that here.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.