Sidekiq is a Ruby library for processing background jobs, helping you move time-intensive tasks out of your web requests. It excels at handling work that would otherwise slow down your application, including image processing, external API calls, sending emails, and generating reports.
Unlike other job processing libraries, Sidekiq uses threads instead of processes, allowing it to handle thousands of jobs concurrently with minimal memory overhead. It relies on Redis for job storage and coordination, providing durability that survives application restarts. Companies like GitHub, Shopify, and Stripe trust Sidekiq to process millions of jobs daily in production environments.
This tutorial walks you through setting up Sidekiq, configuring workers, and implementing reliable background processing in a Ruby application.
Let's dive in!
Prerequisites
You'll need Ruby 3.0 or later installed on your system. Check the official Ruby installation guide if you haven't set it up yet. Sidekiq depends on Redis for storing jobs, so you'll need Redis running locally. The Redis installation documentation covers the setup process for different operating systems.
After installing these dependencies, verify Redis is operational:
A successful connection returns:
If you see Could not connect to Redis, start the Redis server:
Step 1 — Setting up the project
With Redis confirmed working, create a new directory for your Sidekiq project:
Initialize a new Ruby project with Bundler:
This creates a Gemfile where you'll specify your dependencies. Add Sidekiq and the Redis client to the Gemfile:
Install the dependencies:
Sidekiq is now available in your project. In the following steps, you'll create workers and configure job processing.
Step 2 — Creating your first background worker
Background workers in Sidekiq are Ruby classes that include the Sidekiq::Job module and define a perform method containing your job logic.
Create a file for your first worker and add the following code:
The Sidekiq.configure_client block establishes the Redis connection that Sidekiq uses for storing jobs. The WelcomeWorker class includes Sidekiq::Job, which provides the methods needed for job processing.
The perform method contains your actual job logic. In this example, it accepts an email address and logs a message. Real applications would include code for sending emails, calling APIs, or processing data.
The perform_async method enqueues a job for background processing. Sidekiq serializes the arguments and stores the job in Redis, where a worker process will pick it up.
Run the file to enqueue a job:
This adds a job to Redis, but nothing processes it yet. You need to start a Sidekiq worker process that pulls jobs from Redis and executes them.
Create a configuration file for the Sidekiq worker:
This configuration tells Sidekiq to process up to 5 jobs concurrently and to watch the default queue for work.
Start the Sidekiq worker process:
You'll see output showing Sidekiq starting up and processing your job:
The worker processed the job from Redis and executed your perform method. Jobs are now running in the background, separate from your main application.
Step 3 — Working with multiple queues and priorities
Applications often need different types of jobs with varying urgency levels. Sidekiq lets you organize jobs into named queues and configure which queues workers process first.
Create a new worker for data export tasks:
The sidekiq_options method assigns this worker to the low_priority queue instead of default. This separation allows you to control how workers allocate their resources across different job types.
Now create a notification_worker.rb high-priority worker for urgent notifications:
Update your sidekiq.yml to process queues in priority order:
The numbers represent weights that determine how often Sidekiq checks each queue. With these weights, Sidekiq checks the critical queue 8 times, default 4 times, and low_priority once per polling cycle. Critical jobs get processed faster, while low-priority work happens when resources are available.
Create a workers.rb file that requires all your workers:
Start Sidekiq with the updated configuration:
Enqueue jobs from different queues in a separate terminal and observe how Sidekiq prioritizes the critical queue. First, enqueue the jobs:
Return to the first terminal, you'll see output similar to this:
Sidekiq processes jobs concurrently using multiple threads (notice the different tid values), so all three jobs start nearly simultaneously.
The highlighted sections show critical notifications being processed—you can see multiple NotificationWorker jobs executing as Sidekiq's weighted queue configuration checks the critical queue more frequently. With the weights set to [critical, 8], [default, 4], and [low_priority, 1], critical jobs receive 8 times more attention than low-priority tasks, ensuring urgent notifications are handled promptly even under heavy load.
The error shows that when you start Sidekiq with backup_worker.rb, it tries to process the previously enqueued ReminderWorker job but can't find the class definition. You need to require the reminder_worker.rb file or clear old jobs from Redis.
Step 4 — Scheduling jobs for future execution
Beyond immediate background processing, Sidekiq handles delayed execution and recurring jobs. The perform_in and perform_at methods schedule jobs for future execution.
Create a reminder worker:
Run the file to schedule these jobs:
The jobs are stored in Redis with their scheduled execution times. Start Sidekiq to process them:
Wait for 30 seconds and you'll see the first reminder execute:
For recurring jobs, you'll need the sidekiq-cron extension. Add it to your Gemfile:
Install the new dependency:
Create a worker for daily backups:
The cron expression 0 2 * * * follows the standard cron format: minute, hour, day of month, month, day of week. This particular expression means "at 2:00 AM every day."
Here are more examples of cron expressions you might use:
Start Sidekiq to activate the scheduled job:
You'll see Sidekiq confirm the cron job registration:
The backup job now runs automatically according to its schedule, with no additional intervention required.
Step 5 — Monitoring jobs with the Web UI
Sidekiq includes a web dashboard that provides visibility into your job processing. The dashboard shows active jobs, retry queues, scheduled jobs, and historical metrics.
Add the necessary gems to your Gemfile for the web UI:
Install the dependencies:
Generate a session secret key for CSRF protection:
Create a configuration file for the web UI:
Start the web server:
Open your browser and navigate to http://localhost:9292. The dashboard displays an overview of your Sidekiq system:
The Overview page shows processed and failed job counts, queue sizes, and retry statistics. The Busy tab displays currently executing jobs with their arguments and execution time. The Queues page lists all queues with their job counts and lets you manage individual queues.
The Retries section shows jobs that failed and are waiting for their next retry attempt. You can manually retry or delete these jobs. The Scheduled tab displays jobs waiting for their execution time, including one-time delayed jobs.
The Dead page contains jobs that exhausted all their retries. These jobs remain in Sidekiq for 180 days before permanent deletion, giving you time to investigate failures and manually retry if needed.
Final thoughts
Sidekiq is a powerful Ruby library that makes background job processing simple, fast, and reliable. By using threads and Redis, it handles thousands of concurrent jobs efficiently while keeping memory usage low.
With features like multiple queues, job scheduling, and monitoring through its web UI, Sidekiq helps developers offload slow or heavy tasks, improve application performance, and maintain smooth, scalable workflows in production.