Published on January 30, 2010 by Jesse Storimer

Delayed_paperclip is a rubygem that works with Paperclip and delayed_job. It allows you to easily process Paperclip attachments (such as resizing images) in the background with delayed_job.


Install the gem:

sudo gem install delayed_paperclip

Add it to your environment.rb:

config.gem 'delayed_paperclip'

Or, even better, to your Gemfile:

source ""
gem 'delayed_paperclip'

Dependencies: Paperclip && delayed_job



Make sure that you have DJ up and running.

In your model:

  class User < ActiveRecord::Base
    has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
    process_in_background :avatar

Use your Paperclip attachment just like always in controllers and views.


Every resource that talks about background processing seems to mention typical tasks that should be handled in the background. Image resizing is always one of these tasks.

It makes perfect sense because you want the user experience to be fast, they shouldn’t be waiting while you resize images and upload them to s3. DJ should pick up and process the job almost immediately anyway, so its win-win.

I have used Paperclip on a number of projects now and have never processed my images in the background (I’m sure I’m not the only one). There were a few guides out there with instructions on how to add the right callbacks and methods to your model to resize your images in the background, but I wanted something simpler, so I wrote this gem.

In the Wild

The project that I wrote this gem has a form that allows users to upload screenshots and dynamically add more screenshot upload fields. I did some benchmarks with this form with and without using this gem.

Specs: The project is running on Heroku and using s3 for file storage. I tested the gem by uploading 6 screenshots, each has 3 different styles.

Without using this gem, it took the server 8.5 seconds to process the form and images. Let’s think about the operations for a second here. There were 6 screenshots, each of those had to be resized to three different sizes. Then each of those image styles, plus the original, were uploaded to s3, that makes for 6 × 4 = 24 s3 uploads, all while the user is waiting for the form to submit.

With this gem the server tool only 1.2 seconds to process the form and images. Let’s look at the operations again. There were again 6 screenshots, this time there was no resizing going on. Then each of the original images was uploaded to s3, so a total of 6 uploads to s3. Meanwhile we can present the next page to the user 7 seconds faster, in this case, 1/8 of the time.

Like what you read?

Join 2,000+ Ruby programmers improving their skills with exclusive content about digging deeper with sockets, processes, threads, and more - delivered to your inbox weekly.

I'll never send spam and you can unsubscribe any time.

comments powered by Disqus