How It Works Features Pricing Blog Error Guides
Log In Start Free Trial
Sidekiq · Ruby

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

Redis::CannotConnectError (Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)): redis (5.1.0) lib/redis/client.rb:68:in `connect' sidekiq (7.2.1) lib/sidekiq/redis_connection.rb:45:in `create' sidekiq (7.2.1) lib/sidekiq/config.rb:85:in `redis' sidekiq (7.2.1) lib/sidekiq/client.rb:68:in `raw_push' sidekiq (7.2.1) lib/sidekiq/client.rb:42:in `push' app/controllers/orders_controller.rb:15:in `create'

Here's what each line means:

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.

Before (broken)
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
After (fixed)
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:

  1. Notice the error alert or see it in your monitoring tool
  2. Open the error dashboard and read the stack trace
  3. Identify the file and line number from the stack trace
  4. Open your IDE and navigate to the file
  5. Read the surrounding code to understand context
  6. Reproduce the error locally
  7. Identify the root cause
  8. Write the fix
  9. Run the test suite locally
  10. Fix any failing tests
  11. Write new tests covering the edge case
  12. Run the full test suite again
  13. Create a new git branch
  14. Commit and push your changes
  15. Open a pull request
  16. Wait for code review
  17. Merge and deploy to production
  18. 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:

  1. Captures the stack trace and request context
  2. Pulls the relevant source files from your GitHub repo
  3. Analyzes the error and understands the code context
  4. Generates a minimal, verified fix
  5. Runs your existing test suite
  6. Pushes through your CI/CD pipeline
  7. 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)

  1. Ensure Redis is running and accessible.
  2. Set the REDIS_URL environment variable.
  3. Verify Sidekiq starts and connects to Redis.
  4. Open a pull request.
  5. 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.