How to Use Sidekiq in Rails for Background Jobs
To use
Sidekiq in Rails, add the sidekiq gem to your Gemfile, create a worker class that includes Sidekiq::Worker, and enqueue jobs with perform_async. Run the Sidekiq process alongside your Rails server to process jobs asynchronously.Syntax
Sidekiq uses worker classes to define background jobs. Each worker must include Sidekiq::Worker and define a perform method with the job's code. To enqueue a job, call perform_async on the worker class with any arguments.
- Worker class: Defines the job logic inside
perform. - perform_async: Adds the job to Sidekiq's queue to run later.
- Sidekiq process: Runs separately to execute jobs in the background.
ruby
class HardWorker include Sidekiq::Worker def perform(name, count) puts "Doing hard work for #{name} #{count} times" end end # Enqueue job HardWorker.perform_async('Bob', 5)
Example
This example shows how to create a Sidekiq worker that prints a message and how to enqueue a job from a Rails controller.
ruby
# Gemfile gem 'sidekiq' # Run bundle install # app/workers/hard_worker.rb class HardWorker include Sidekiq::Worker def perform(name, count) count.times do puts "Hello, #{name}!" end end end # app/controllers/welcome_controller.rb class WelcomeController < ApplicationController def index HardWorker.perform_async('Alice', 3) render plain: 'Job enqueued' end end # To start Sidekiq, run in terminal: # bundle exec sidekiq # Then start Rails server: # rails server
Output
Job enqueued
# Sidekiq console output:
# Hello, Alice!
# Hello, Alice!
# Hello, Alice!
Common Pitfalls
Common mistakes when using Sidekiq include:
- Not running the Sidekiq process separately, so jobs never run.
- Using blocking or long-running code inside
perform, which can slow down job processing. - Not configuring Redis properly, as Sidekiq requires Redis to queue jobs.
- Trying to access request-specific data inside workers, which run outside the web request.
ruby
## Wrong: Calling perform instead of perform_async class WrongWorker include Sidekiq::Worker def perform(name) puts "Hello, #{name}!" end end # This runs job immediately and blocks WrongWorker.new.perform('Bob') ## Right: Enqueue job asynchronously WrongWorker.perform_async('Bob')
Quick Reference
| Action | Code Example | Description |
|---|---|---|
| Add Sidekiq gem | gem 'sidekiq' | Include Sidekiq in your Gemfile and run bundle install |
| Create worker | class MyWorker include Sidekiq::Worker def perform(args) # job code end end | Define background job logic in perform method |
| Enqueue job | MyWorker.perform_async(args) | Add job to Sidekiq queue to run asynchronously |
| Start Sidekiq | bundle exec sidekiq | Run Sidekiq process to process jobs |
| Configure Redis | Sidekiq uses Redis by default | Ensure Redis server is running and accessible |
Key Takeaways
Add the sidekiq gem and create worker classes with perform methods to define jobs.
Use perform_async to enqueue jobs for background processing.
Run the Sidekiq process separately alongside your Rails server to execute jobs.
Avoid blocking code inside workers and ensure Redis is properly configured.
Do not call perform directly; always enqueue jobs with perform_async.