Skip to main content
You can use Celery with Upstash Redis to build scalable and serverless task queues. Celery is a Python library that manages asynchronous task execution, while Upstash Redis acts as both the broker (queue) and the result backend.

Setup

Install Celery

To get started, install the necessary libraries using pip:

Database Setup

Create a Redis database using the Upstash Console. Export the UPSTASH_REDIS_HOST, UPSTASH_REDIS_PORT, and UPSTASH_REDIS_PASSWORD to your environment:
You can also use python-dotenv to load environment variables from a .env file:
.env

Example Application

Setting up Celery with Upstash Redis

tasks.py
Note that we should use the rediss:// protocol to connect to redis over TLS and set ssl_cert_reqs=required to enforce certificate validation.

Running the Worker

Start the Celery worker to execute tasks:

Using the Task

You can now use the add task to perform background computations:
main.py

Conclusion

To see a more detailed example of using Celery with Upstash Redis, check out the Job Processor with Celery example on our website. For more details on Celery, visit the Celery Documentation. For Upstash Redis, check out the Upstash Redis Documentation.