Better Programming

Advice for programmers.

Follow publication

Member-only story

How to Use Sidekiq in Rails 6

Catalin Ionescu
Better Programming
Published in
4 min readJul 10, 2020

Updated February 2021 for Ruby on Rails 6.1 and Sidekiq 6.1.3 while also adding a step-by-step guide for deploying to Heroku.

How to use Sidekiq in Rails 6

In September 2019, Sidekiq released version 6.x. Sidekiq comes in three flavours: the open-source version, Sidekiq Pro, and Sidekiq Enterprise.

Sidekiq uses threads to handle many jobs at the same time in the same process and is integrated tightly with Rails, although Rails isn’t a requirement. If you are looking for something language-agnostic, you should check out Faktory, also created by Mike Perham (the creator of Sidekiq).

At Organisely, we use Sidekiq for any long-running where the user doesn’t require immediate feedback. We also use Sidekiq for jobs that are likely to fail due to network errors and we want to leverage the automated retry mechanism.

Organisely sends a daily batch of text messages (using Twilio’s SMS API) as reminders for customers’ appointments. Although Twilio has insanely reliable APIs, network errors are inevitable and must be taken into account. We wrap each Twilio API call into a Sidekiq job that’ll retry if the API call is unsuccessful.

Setup

If you didn’t already install Ruby on Rails for development on macOS, check out this tutorial.

Install Sidekiq by adding the gem to your Gemfile.

gem 'sidekiq', '~> 6.1.3'

Sidekiq has only one dependency, and that’s Redis. If you are using a Mac, you can install Redis using Homebrew:

$ brew install redis

Create a new job in app/jobs to process jobs asynchronously:

class MyJob
include Sidekiq::Worker

def perform(args)
# block that will be retried in case of failure
end
end

You can invoke this job as MyJob.perform_async(args).

Start the Sidekiq process with bundle exec sidekiq.

Invoker

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Catalin Ionescu
Catalin Ionescu

Written by Catalin Ionescu

Director of Applications Engineering. Bristol University alumnus.

Responses (4)

Write a response