Introducing Active Job in Rails

Tram Ho

Are you always trying to give users a better experience when using your website or app? One of the most important ways to achieve this is by reducing the server response time. In this article, we will explore Active Job – which allows you to do so by queuing backends. You can also use the queue to help reduce traffic or upload to the server, allowing work to be done when the server is “free”.

What is Active Job?

Active Job in Rails is a framework that helps create tasks and allows them to run on a number of different queueing backends. The tasks can be regular periodic cleaning, uploading images to the server, sending mail … The most common queue systems used in Rails applications are Sidekiq, Resque and Delayed Job.

Use Active Job

Active Job has a fairly simple interface and installer. Here is how to use its features:

Create Job

Active Job when created via command will include job and necessary stubs.

In the generated Job Class, the #perform method is called when the Job is executed, we can pass arbitrary parameters to this method.

Add a Job to the queue

We have the option to add jobs to the queue as below:

Execute Job

If no adapter is set, the job will be executed immediately.


Active Job provides adapters available for some queue systems such as: Sidekiq, Resque, Delayed Job, … You can see details in the document of ActiveJob :: QueueAdapters

Install Backend

You can set the queue system for your application.


Most adapters support multiple queues. You can schedule jobs to run on a specific queue

If you want to more clearly manage the queue the job will run, you can use the #set method as follows:

Or want to manage from Job level, you can pass 1 block to the method #queue_as. The block will be executed in the job context (called self.arguments) and must return the queue name.


Active Job provides hooks for the lifetime of a job. Callback allows to attach logic to the job life cycle.

Callbacks are available

  • before_enqueue
  • around_enqueue
  • after_enqueue
  • before_perform
  • around_perform
  • after_perform

How to use callbacks

Some tasks should use Active Job

Send email

Emailing is the most common task possible and should be done in the background job. There is no reason to send an email immediately (before the response is displayed), all emails should be delivered to the queue. Even if the email server responds in 100ms, there are still 100ms that you are causing users to wait unnecessarily. Sending emails through a background job is extremely simple with Active Job, as it is built into ActionMailer. By changing the deliver_now method to deliver_later, the Active Job will automatically send emails in the queue asynchronously.

Image processing

Images may take some time to be processed. It takes more time if you have several (or more) different photo styles and sizes to create. Fortunately, both Paperclip and CarrierWave have additional gems that can help process these images in the queue instead of at the time of upload.

Paperclip uses a Delayed Paperclip gem, supports Active Job, and CarrierWave uses the CarrierWave Backgrounder gem. With Delayed Paperclip , you just need to call an additional method to let it know what you want to handle in the background and the gem will handle the rest. You can ask it to handle some types immediately, while others handle them in the queue.

The example above allows image processing: small immediately, while: medium and: large are done in the background.

Upload content

Usually when you have a user uploading content, it needs to be processed. This may be a CSV file that needs to be imported into the system, an image to create a thumbnail image, or a video to be processed. A large CSV file may take several minutes to process, during which time the connection may be timed out. You should handle most asynchronous upload data in the queue. The usage process is as follows:

  1. Accept the file and upload it to S3 (or wherever you are hosting user-generated content).
  2. Add a job to the queue to process this file.
  3. Users will immediately see a successful page telling them that their file has been submitted for processing.
  4. The system will download the file, process it and mark it as processed. Another note is that you will want to store an import report in the database. It may include any records not processed due to invalid data. What to do is to create an error message file for each import that the user can download.

Use external API

External APIs may be unstable, slow, and the user experience should not depend on them whenever possible. For example, below where we use the IP address to find out some geographic information using the Telize API. It usually responds in 200ms to 500ms, added to your current response time, which can make a big difference. All external APIs should be handled in the same way: Use background jobs if possible. First, we schedule a job execution, passing in the IP address of the current request.

Here we perform the actual work to be done. We will make a real remote request to the API to show how long this request could take.

You can see what happened in Rails logs:


Active Job is a great addition to Rails. It provides a clear and unique interface for adding jobs and handling jobs. If you are starting a new Rails project or adding a queuing system to an existing project, be sure to consider using Active Job instead of queuing directly. Using queues can increase website availability (by reducing response time), providing more responsive server and load times (by transmitting various jobs and servers).


Share the news now

Source : Viblo