Performance is key in web applications. Snappy websites make for better user experiences, higher conversion rates and better user retention. A swift application response causes less stress on servers trying to respond to many users too. There are many ways to improve the performance of a web application in Rails and I want to look at one of those today.

Performing long running, blocking tasks during the course of a request is a top way to slow down responses for any web application. I’m talking things like sending emails, generating PDF or CSV files or making HTTP requests to 3rd party APIs. All of these things take a relatively long time compared to other actions normally performed during the course of a request. If you’re using Twilio within an application the last point might stick out at you.

The best thing to do with long running tasks like this is move them away from the request itself and perform them in the background. This allows your application server to respond to requests swiftly and get the job done without affecting performance for your site’s users or tying up web server processes. Since version 4.2 Rails has included Active Job, a library which makes it easy to delay long running tasks and perform them in a background queue. In this post we will explore how we can Active Job to speed up our application.

Let’s queue!

In order to show how we can speed up an application’s response times by queueing tasks to happen in the background, we’re going to need an application to test against. Rather than build one up right now, we’re going to use one of the example applications available in the Twilio tutorials. Let’s take a look at Click to Call, a simple form on your website that takes a user’s phone number and calls them back. You can build the application up using the tutorial or grab the application from GitHub.

What you’ll need

To complete this you’ll need a few things:

A Twilio account (sign up for a free Twilio account if you don’t have one already)

A Twilio number that can make phone calls

A way to tunnel from a public URL to your localhost (I really like ngrok)

Ruby and bundler installed

Redis (check out the quick start guide for redis to get it up and running)

To run the application, grab your Twilio credentials from your account dashboard and the phone number, then: