LUMINA TECH Logo
TRENDINGTECH BLOGABOUTCONTACT

LUMINA TECH

Cutting-edge technology insights and solutions

Quick Links

  • Trending
  • Tech Blog
  • About Us
  • Contact

Contact

Japan, Tokyo-to, Edogawa-ku

Nishi-Kasai 6-4-4

Email: nguyen.nhuhieu.dev@gmail.com

Tel: +81-70-3612-8980

© 2025 LUMINA TECH. All rights reserved.

About the Author

NGUYEN NHU HIEU

NGUYEN NHU HIEU

Writer at LUMINA TECH

Popular Tags

Ruby on Rails (2)Microsoft Dynamics (1)C# (1)Power Platform (1)Redis/ElastiCache (1)
Coding

Background Job Processing in Ruby on Rails: Active Job vs. Sidekiq

04.07.2025
NGUYEN NHU HIEU
32 views

In Ruby on Rails applications, background jobs are an essential tool for managing time-consuming tasks that don't need to happen in real-time during a user request. By offloading these tasks, you can improve application performance, prevent response timeouts, and provide a smoother user experience. In this article, we'll explore when to use background jobs in Rails, compare using Active Record alone versus integrating Sidekiq, and provide setup instructions with demo code for each approach.

When to Use Background Jobs in Rails

Background jobs are ideal for operations that are resource-intensive, time-consuming, or don't require immediate user feedback. Using them helps avoid HTTP request timeouts (e.g., the default 30-second limit in many web servers) and keeps your application responsive. Here are some common scenarios where background jobs shine:

  1. File Imports/Exports: Processing large CSV or Excel files, such as importing user data or generating reports.
  2. Email Delivery: Sending transactional emails (e.g., welcome emails, password resets) or bulk email campaigns.
  3. API Calls: Interacting with slow or rate-limited third-party APIs (e.g., payment gateways, social media APIs).
  4. Data Processing: Heavy computations like image resizing, video encoding, or machine learning model inference.
  5. Scheduled Tasks: Periodic jobs like cleaning up old records or sending daily summaries.

For example, if a user uploads a CSV file with 10,000 rows to import into your database, processing it synchronously in the controller could take minutes and timeout the request. Moving this task to a background job ensures the user gets an immediate response (e.g., "Import started!") while the heavy lifting happens behind the scenes.

Option 1: Using Active Record with Active Job

Rails provides a built-in framework called Active Job for handling background jobs, and it integrates seamlessly with Active Record. This is a lightweight solution suitable for simpler use cases or smaller applications where you don’t want to introduce additional dependencies.

When to Use Active Record with Active Job

  • You have simple, low-volume tasks (e.g., sending a few emails).
  • You want to avoid external dependencies like Redis or additional gems.
  • You're using a basic queue system (e.g., Rails' default async adapter) or a database-backed queue (e.g., delayed_job).

Setup for Active Job with Active Record

  1. No Gem Required: Active Job is included in Rails by default.

  2. Configure the Queue Adapter: For development, you can use the async adapter. Edit config/application.rb or config/environments/development.rb:

    config.active_job.queue_adapter = :async

Generate a Job

rails generate job import_csv

This creates app/jobs/import_csv_job.rb.

Define the Job

class ImportCsvJob < ApplicationJob queue_as :default def perform(csv_file_path) require 'csv' CSV.foreach(csv_file_path, headers: true) do |row| User.create!( name: row['name'], email: row['email'], age: row['age'] ) end end end

Trigger the Job: Call it from a controller or elsewhere:

class ImportsController < ApplicationController def create file_path = params[:file].path ImportCsvJob.perform_later(file_path) redirect_to root_path, notice: "CSV import started!" end end

Using Active Job: Pros and ConsPros and Cons

  • Pros: Simple setup, no external services required, good for small-scale apps.
  • Cons: The async adapter isn't suitable for production (it’s in-memory and lost on restart). Scaling or retrying failed jobs requires additional setup (e.g., switching to delayed_job with a database table).

Option 2: Using Sidekiq for Background Jobs

For more robust and scalable background processing, Sidekiq is a popular choice. It uses Redis as a queue backend, offering better performance, reliability, and features like retries, scheduling, and a web UI for monitoring.

When to Use Sidekiq

  • You need high performance for a large volume of jobs.
  • You require advanced features like job retries, scheduling, or priority queues.
  • You’re running a production app where reliability and monitoring are critical.

Setup for Sidekiq

  1. Install Redis: Sidekiq requires Redis. On macOS/Linux, install it with:

    brew install redis redis-server # Start Redis
  2. Add Sidekiq to Your Gemfile

    gem 'sidekiq'
  3. Install Gems

    bundle install
  4. Configure Active Job to Use Sidekiq: In config/application.rb:

    config.active_job.queue_adapter = :sidekiq
  5. Generate a Job:: Same as before:

    rails generate job process_csv
  6. Define the Job:: Edit app/jobs/process_csv_job.rb:

class ProcessCsvJob < ApplicationJob queue_as :default def perform(csv_file_path) require 'csv' CSV.foreach(csv_file_path, headers: true) do |row| User.create!( name: row['name'], email: row['email'], age: row['age'] ) end rescue StandardError => e # Sidekiq will retry the job automatically (default: 25 retries) Rails.logger.error "CSV processing failed: #{e.message}" raise # Re-raise to trigger Sidekiq retry end end
  1. Trigger the Job:: Same as before:

    ProcessCsvJob.perform_later(file_path)
  2. Run Sidekiq:: Start the Sidekiq worker in a separate terminal:

    bundle exec sidekiq
  3. Optional: Monitor with Sidekiq Web UI:

    • Add to Gemfile: gem 'sinatra', then mount in config/routes.rb:

      require 'sidekiq/web' Rails.application.routes.draw do mount Sidekiq::Web => '/sidekiq' end
    • Visit /sidekiq in your browser (add authentication in production).

Using Sidekiq: Pros and Cons

  • Pros: High performance, reliable retries, scalable, great monitoring tools.
  • Cons: Requires Redis, adds complexity compared to Active Job alone.

Active Record vs. Sidekiq: How to Choose

  • Use Active Record with Active Job if:

    • Your app is small or in early development.
    • You’re handling light workloads with no need for retries or scheduling.
    • You want to keep dependencies minimal.
  • Use Sidekiq if:

    • Your app is in production with significant traffic or job volume.
    • You need reliability (e.g., retries for failed jobs) or advanced queue management.
    • You’re comfortable managing Redis.

Conclusion

Background jobs in Ruby on Rails are a powerful way to handle long-running tasks and prevent response timeouts. For simple needs, Active Record with Active Job provides a quick, dependency-free solution. For production-grade applications, Sidekiq offers superior performance and features at the cost of added setup. Choose based on your app’s scale and requirements, and enjoy the flexibility Rails provides to adapt as your needs evolve!

Ruby on Rails

Comments

Related Articles

Avoiding CookieOverflow in Ruby on Rails with Redis Session Store (Single & Cluster Mode Setup)
Avoiding CookieOverflow in Ruby on Rails with Redis Session Store (Single & Cluster Mode Setup)

Mar 31, 2025