redis_client = redis.Redis(host='redis', port=6379, db=0) celery_app = Celery( "worker", broker="redis://redis:6379/0", backend="redis://redis:6379/0" ) does this create the Redis DB?

does redis.Redis create Redis database when using Celery with Redis broker 2025

Refined the query by specifying the context of using Redis with Celery and the focus on whether it creates a Redis database, while adding the current year to capture up-to-date discussions.

To determine whether the provided code snippet creates a Redis database, it's essential to understand the functionality of the Redis client and the integration with Celery.

Understanding Redis and Celery Integration

What the Code Does

The code snippet initializes a Redis client and creates a Celery application with Redis as both the message broker and the backend for task result storage:

redis_client = redis.Redis(host='redis', port=6379, db=0)
celery_app = Celery(
    "worker",
    broker="redis://redis:6379/0",
    backend="redis://redis:6379/0"
)
  1. Redis Client Initialization: redis_client = redis.Redis(host='redis', port=6379, db=0) establishes a connection to the Redis server running at the specified host and port. It connects to the database identified by db=0.

  2. Celery Application Creation: The Celery application is configured to use the Redis server as its broker and backend. This means Celery will use Redis to handle task message queuing and to store results.

Does This Create a Redis Database?

The code does not explicitly create a new Redis database. By default, Redis allows access to databases indexed from 0 to 15, with database 0 being the default one accessed when you connect unless specified otherwise. Here’s a breakdown:

  • Pre-existing Database: If a Redis database (in this case, database 0) already exists, the code simply connects to it. If no data has ever been stored in database 0, it will appear empty, but it's still considered "created" as part of Redis's structure.
  • Database Creation: Redis does not require explicit database creation. The databases are created implicitly when keys or data are added. Thus, if you insert data into the Redis server using redis_client, it will automatically create the structure if it doesn't exist.

Task Coordination with Celery

When you configure Celery to use Redis:

  • If you specify a broker URL (broker="redis://redis:6379/0"), you tell Celery to use database 0 for queuing tasks.
  • The backend URL (backend="redis://redis:6379/0") indicates where task results will be stored.

This setup enables task management through Redis but does not specifically create a new database beyond what already exists upon connection.

Conclusion

In summary, the code does not create a Redis database in the conventional sense; rather, it connects to an existing database and operates within it. If you begin adding keys to this database using the redis_client, Redis will handle the underlying structure without requiring manual database creation. For precise configurations or for adding multiple queues, you may consider defining separate databases if needed Celery Documentation.

Sources

10
1
Backends and Brokers — Celery 5.5.3 documentation
Docs

Redis can be both a backend and a broker. As a Broker: Redis works well for rapid transport of small messages. Large messages can congest the ...

2
Using Redis — Celery 5.5.3 documentation
Docs

Broker is not a database, so if you are in need of scheduling tasks for a more distant future, database-backed periodic task might be a better choice.

3
Using different Redis databases for different queues in ...
Stack Overflow

There's no way to use different DBs for Redis broker. You can create different Celery applications with different DBs but then you cannot set dependencies.

4
Redis as a Message Broker: Deep Dive
Dev

In addition to being a super fast data store, Redis can also be used as a message broker for distributed task queues such as Celery. Here's how ...

5
Custom Task Scheduling in Python using Redis and Celery
Medium

In this blog, we'll guide you through creating a custom task-scheduling system using Redis and Celery in Python.

6
Celery just stops running tasks : r/django
Reddit

So since I was using Redis for Cache (database 0); for my Celery broker (database 1) and my Celery backend results (database 2). This all ...

7
Celery doesn't connect Redis - Deployment
Forum

I have docker container running for Redis and Celery Broker, and I can see that Celery accepts tasks but can't connect to Redis and doesn't turn task over for ...

8
Integrating Redis With Message Brokers
Dzone

Celery is a tool for managing tasks. We can use Redis as a broker for Celery. This helps us manage background tasks and scheduling better. Code ...

9
Step-by-Step Guide to Tracking Celery Tasks with Redis
Moldstud

Start by configuring Redis as a broker within your application. This setup allows you to monitor queues directly from Redis, providing you with ...

10
How to set up Celery and Redis - Django Background Tasks
YouTube

In this video we will set up Celery with Redis as the message broker to send emails to subscribers.