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

Billing Optimization

Celery workers poll Redis continuously, even when there is no queue activity. This can incur extra costs because Upstash charges per request on the Pay-As-You-Go plan. With our Fixed plans, we recommend switching to a Fixed plan to avoid increased command count and high costs in Celery use cases.

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.