Skip to content
Latchkey

Celery "Cannot connect to redis://..." broker/backend in CI

Celery could not reach the Redis broker or result backend in its URL. In CI this is a Redis service that has not finished starting, or a URL whose host/port does not match the service.

What this error means

Celery logs "consumer: Cannot connect to redis://127.0.0.1:6379//: Error 111 connecting to 127.0.0.1:6379. Connection refused." and retries without becoming ready.

python
[ERROR/MainProcess] consumer: Cannot connect to redis://127.0.0.1:6379//:
Error 111 connecting to 127.0.0.1:6379. Connection refused..
Trying again in 2.00 seconds... (1/100)

Common causes

Redis service is not ready

The worker connected before the Redis container started accepting connections.

The Redis URL is wrong for CI

broker_url or result_backend points at a host/port the runner cannot reach.

How to fix it

Add a Redis service with a health check

  1. Run a Redis service that reports healthy via redis-cli ping.
  2. Set broker and backend URLs to the service host and port.
  3. Start the worker after Redis is healthy.
.github/workflows/ci.yml
services:
  redis:
    image: redis:7
    ports: ['6379:6379']
    options: --health-cmd "redis-cli ping" --health-interval 5s --health-retries 10
env:
  CELERY_BROKER_URL: redis://127.0.0.1:6379/0
  CELERY_RESULT_BACKEND: redis://127.0.0.1:6379/1

Run tasks eagerly for unit tests

Tests that do not exercise the broker can run tasks synchronously and skip Redis entirely.

python
app.conf.task_always_eager = True

How to prevent it

  • Gate the worker on a Redis health check.
  • Drive broker/backend URLs from env.
  • Use task_always_eager where a real broker is not needed.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →