Fix Redis::CannotConnectError: Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED) for Sidekiq in Sidekiq
This error means Sidekiq cannot connect to Redis, which it uses as its job queue backend. Redis may not be running, the connection URL is incorrect, or the connection pool is exhausted. Start Redis, verify the REDIS_URL environment variable, and ensure your Sidekiq configuration has the correct Redis connection settings.
Reading the Stack Trace
Here's what each line means:
- sidekiq (7.2.1) lib/sidekiq/redis_connection.rb:45:in `create': Sidekiq attempts to create a Redis connection from its connection pool and fails.
- sidekiq (7.2.1) lib/sidekiq/client.rb:68:in `raw_push': The Sidekiq client tries to push a job to Redis but the connection fails.
- app/controllers/orders_controller.rb:15:in `create': The controller action enqueues a Sidekiq job which triggers the Redis connection attempt.
Common Causes
1. Redis not running
The Redis server process is not started on the machine or container.
# $ redis-cli ping
# Could not connect to Redis at 127.0.0.1:6379: Connection refused
# Sidekiq cannot start without Redis
2. Wrong Redis URL
The REDIS_URL points to the wrong host, port, or includes incorrect authentication.
# config/initializers/sidekiq.rb
Sidekiq.configure_server do |config|
config.redis = { url: 'redis://wrong-host:6379/0' }
end
3. Connection pool too small
The Sidekiq concurrency is higher than the Redis connection pool size.
# config/sidekiq.yml
:concurrency: 25
# But Redis pool is only:
Sidekiq.configure_server do |config|
config.redis = { url: ENV['REDIS_URL'], size: 5 } # Too small for 25 threads
end
The Fix
Use the REDIS_URL environment variable for configuration flexibility across environments. Add timeout settings for resilience. Ensure both server and client configurations point to the same Redis instance.
Sidekiq.configure_server do |config|
config.redis = { url: 'redis://wrong-host:6379/0' }
end
Sidekiq.configure_client do |config|
config.redis = { url: 'redis://wrong-host:6379/0' }
end
Sidekiq.configure_server do |config|
config.redis = {
url: ENV.fetch('REDIS_URL', 'redis://localhost:6379/0'),
pool_timeout: 5,
network_timeout: 5
}
end
Sidekiq.configure_client do |config|
config.redis = {
url: ENV.fetch('REDIS_URL', 'redis://localhost:6379/0'),
pool_timeout: 5,
network_timeout: 5
}
end
Testing the Fix
require 'rails_helper'
require 'sidekiq/testing'
RSpec.describe 'Sidekiq Redis connection' do
it 'enqueues jobs successfully' do
Sidekiq::Testing.fake!
expect {
OrderProcessingJob.perform_async(1)
}.to change(OrderProcessingJob.jobs, :size).by(1)
end
it 'processes jobs without errors' do
Sidekiq::Testing.inline!
order = create(:order, status: 'pending')
OrderProcessingJob.perform_async(order.id)
expect(order.reload.status).to eq('processing')
end
end
Run your tests:
bundle exec rspec spec/jobs/order_processing_job_spec.rb
Pushing Through CI/CD
git checkout -b fix/sidekiq-redis-connection,git add config/initializers/sidekiq.rb,git commit -m "fix: configure Sidekiq Redis with env var and timeouts",git push origin fix/sidekiq-redis-connection
Your CI config should look something like this:
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: postgres
ports: ['5432:5432']
redis:
image: redis:7
ports: ['6379:6379']
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: '3.3'
bundler-cache: true
- run: bin/rails db:setup
- run: bundle exec rspec
The Full Manual Process: 18 Steps
Here's every step you just went through to fix this one bug:
- Notice the error alert or see it in your monitoring tool
- Open the error dashboard and read the stack trace
- Identify the file and line number from the stack trace
- Open your IDE and navigate to the file
- Read the surrounding code to understand context
- Reproduce the error locally
- Identify the root cause
- Write the fix
- Run the test suite locally
- Fix any failing tests
- Write new tests covering the edge case
- Run the full test suite again
- Create a new git branch
- Commit and push your changes
- Open a pull request
- Wait for code review
- Merge and deploy to production
- Monitor production to confirm the error is resolved
Total time: 30-60 minutes. For one bug.
Or Let bugstack Fix It in Under 2 minutes
Every step above? bugstack does it automatically.
Step 1: Install the SDK
gem install bugstack
Step 2: Initialize
require 'bugstack'
Bugstack.init(api_key: ENV['BUGSTACK_API_KEY'])
Step 3: There is no step 3.
bugstack handles everything from here:
- Captures the stack trace and request context
- Pulls the relevant source files from your GitHub repo
- Analyzes the error and understands the code context
- Generates a minimal, verified fix
- Runs your existing test suite
- Pushes through your CI/CD pipeline
- Deploys to production (or opens a PR for review)
Time from error to fix deployed: Under 2 minutes.
Human involvement: zero.
Try bugstack Free →No credit card. 5-minute setup. Cancel anytime.
Deploying the Fix (Manual Path)
- Ensure Redis is running and accessible.
- Set the REDIS_URL environment variable.
- Verify Sidekiq starts and connects to Redis.
- Open a pull request.
- Merge and verify job processing in staging.
Frequently Asked Questions
BugStack runs the fix through your existing test suite, generates additional edge-case tests, and validates that no other components are affected before marking it safe to deploy.
BugStack never pushes directly to production. Every fix goes through a pull request with full CI checks, so your team can review it before merging.
Sidekiq needs concurrency + 5 connections for the server process and 5 connections for each client process. Set your Redis maxclients accordingly.
Yes, configure different REDIS_URL values for Sidekiq and Rails cache. This isolates job queue performance from cache operations.