Speed up Rails on Heroku by using Rack::Zippy

I really like Heroku for fast deployments and everything they offer for a small Ruby on Rails or node.js apps. It’s so easy to deploy a web app there, and it’s pretty much free unless you are doing some serious stuff, by when you should already be paying either Heroku, or have your own dedicated solution when you’re all grown up.

There is one thing that I personally dislike about Heroku, and that is their asset serving. As we are all sitting behind a desktop(laptop) computer, and have pretty good network connections, we don’t see how slow the app is for someone on 3G mobile network, or something even slower. And in a well written app, the main culprit for large data transfers are the assets.

We have a lot of JavaScript, Css and images in our apps, because we want to make them beautiful, but that takes it’s toll on network bandwidth. Luckily most browsers support compressed assets(js and css) and know how to decompress them. And when you build your own server, there is a very high probability that assets won’t be served through Rails at all. You will either use NGINX or Apache httpd to act as a reverse proxy, and also serve your assets for you. The web server is smart enough to serve compressed assets to browsers that support them, and uncompressed to others.

Rails has a pretty good ActionDispatch::Static rack middleware that does all the work for us while in development, or on heroku by changing a config flag, or using their rails_12factor gem in production. To get it running you have to change the static flag inside the config/environments/production.rb

 # rails 4.1 and below
 config.serve_static_assets = true
 # rails 4.2 and above
 config.serve_static_files = true

For those on a budget, and not able to afford to serve assets from Amazon S3 or another CDN (will update here when my post on how to do that is finished), there is an option on serving the compressed assets from rails. They are already generated by heroku on deploy, but just aren’t being served, which considerably increases your initial page load time, and every additional one, if the assets aren’t being properly cached. ` You can solve this pretty big issue with including one gem that will serve compressed assets for you. It’s called Rack::Zippy and it’s really easy to configure. You have to add it to the Gemfile first:

gem 'rack-zippy'

Run the bundle command and create the initializer for Rack::Zippy in config/initializers/rack_zippy.rb like this:

Rails.application.config.middleware.swap(ActionDispatch::Static, Rack::Zippy::AssetServer)

This will use Rack::Zippy to server gzipped assets instead of uncompressed ones and really reduce your initial load time, especially on slow connections. It also adds pretty sane cache expiration headers which can also help with the browser caching.

A not so funny orphaning issue or Rails’s has_one relationships

If you are using ActiveRecord and keeping your models pretty thin and properly normalized, you will surely reach for the very nice has_one relationship. It’s basically a one to many relationship, but Rails thinks for you and keeps only one child model available for use.

It’s pretty easy to set up, given that you have two models, let’s say Post and Post::Metadata

class Post < ActiveRecord::Base
  has_one :post_metadata
end

class Post::Metadata
  belongs_to :post
end

OK, this part is pretty straightforward, you create your records either by using Post::Metadata.create(post_id: 42, other_attrs) or by using Post.find(42).create_post_metadata(attrs). If there already is a has_one child record, and you create a new one, the old one will be orphaned, which is a default option, and you should be thinking of it before you start messing around. This can also be the desired option, maybe you are storing some photo url’s that aren’t being used anymore, and have a callback on them to destroy the record after you remove the file from the third party storage.

There is one tiny kink that cost me a few hours of life today, although you create a child record in a has_one relationship, the mere action of building an association (just instantiating it, not creating) is enough to orphan the previous record, without it rolling back if the newly built record is destroyed.

Automated testing will add value to your software project

We have read enough about TDD and it’s demise in the last year. Since David published his post about how TDD is dead there have been a couple of flame wars concerning TDD and testing in general.

I believe that TDD is a good thing, but I don’t always practice it, as sometimes you don’t have the time to do it. I know, some of you reading this will say that there you must make time for TDD and that TDD is the only way. Maybe you are correct, but in a startup world there is rarely any time for testing at all.

With deadlines and churning new features each week, one can’t make the time to do proper TDD. And sometimes it seems that TDD is some relic from the past, from the really distant past. There is a nice report called Why Most Unit Testing is Waste that sums it up fairly good.

However, I believe in automated testing, at least having a full integration suite, following the application happy path, and any edge case you find later on. Also I’m not against unit testing, if it makes sense. Payment processing code, of course you will test it. Some code deciding if the label class is blue or red, well, you can probably skip that test if you have no time to write it. Rails controller tests are a great example of procrastination in tests. I don’t have anything smart to work on, let’s write a couple useless controller tests.

Unit testing external libraries is another thing, they should be properly tested, to ensure that their API behaves as it claims. Especially if your library is public, then you have to test it.

Having a thorough test suite increases the application value, and decreases the breakability because any subsequent change you make on an application that isn’t tested is like walking through a minefield. You never know what will break.

I would have liked that I learned this lesson the easy way, by listening to other people having issues when some of their code wasn’t tested and they had to change just one little thing, and something completely unrelated broke. However, that wasn’t the case, I learned it the hard way. With a really bad client, who constantly changed their mind about features (another red flag) we were implementing a lot of stuff, and changing it on a daily basis. Having no tests meant that you expected something else will break after deploy, because you just don’t know what can go wrong.

After I got burned by that, I started writing tests, trying to do TDD, but at least covering the process with integration tests as I went along. And it helped a lot, the sheer confidence when deploying the app that nothing will go wrong is really enough. And the client is better off in the long run, because there is no chance that the code breaks, and no one notices it.

Lesson learned: Don’t obsess with TDD or the proper way to test, but try to test the code as well as you can, have an integration/acceptance suite that you run before deploying, and try to cover as much of the app as possible with it. Don’t overdo it, and don’t test the language or framework you are using. Shaving Yaks is really fun sometimes, but don’t do it on a production application, because someone will read it later on, and think that you have to test every little thing.

Automating personal and business workflows

In an average month, there is a lot of stuff you will have to do. Stuff like paying the bills, taking care of inventory for home/company, budgeting, and your day to day work.

The best thing I have found to do it is to automate as much work as possible. I’m not only talking about programming something to do the work for me, but to write down all the processes that I’m using in my work and life.

By writing everything down, you are able to generate check lists, for every process. Although this sound pretty boring and lame, the alternative is panicking when you’ve forgotten to take the health insurance card to your vacation. Or your drivers licence for example. Or kids (OK, I sincerely hope that this one only happened in that movie) :).

Having a printed out check list does wonders when I have to set up a new server environment from scratch, although everything is scripted, there is always manual work involved. Checking each box gives you confidence that everything is working as it should be. If a process fails though, you are able to retrace the steps, figure out what went wrong, and fix the step, or add a few in between.

When you document everything, it’s a great time to hire someone to do the menial work for you. You don’t have to waste your precious time by paying the bills, or ordering water, or doing anything except the one thing you do best, whatever that is. That thing got you to where you are now, but most of us have businesses to run, and that also takes it’s toll.

Tolls don’t matter here, start with pen and paper, then see if anything else fits better. I use Markdown and ia Writer Pro to accomplish my goals, to write the process down, but lately I’m experimenting with something bigger, as I want to focus on writing more.

Don’t obsess with analytics too much

If you are only an occasional writer, as I surely am, you don’t have to look at the analytics screen all the time. It’s a really nice thing to look at the real time data when you publish a new post, and advertise it on Twitter and other social networks.
But that one is a waste of time. Real time data doesn’t mean anything real. The main reason is that aside from satisfying your own weird obsession, you are not accomplishing anything that will help you with your goals in life.
The way I fight with it (when I eventually write and publish something), is scheduling it to be published in the future. When I set and forget, the urge to look at real time analytics data vanishes, because if I’m not personally involved in pressing the Publish button, then I’ll most probably forget that it’s being published at the moment, and work on something else.
As the best time to publish depends on many other factors, I tend to publish when I’m even not sitting in front of the computer. I’m trying to set a dedicated time to write each day, and it will take me some time to accomplish that. But your publishing time should always be the same.
I’m not advocating on complete analytics denial, because analytics are one of the best investigation tools you have in your work. You can see what posts people are most interested in, and focus on those topics, instead of those not being read much.
In a business setting analytics and lead tracking is really crucial, but real time data won’t help you with anything. Once someone is visiting the site, then it’s too late to change anything, so relax, don’t fuss about it and have fun.
Spring is coming, go outside and catch some sun, it’s good for you. And it’s much better than constantly looking who is on your website right now.