Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Celery, Redis and ConnectionPool

Tags:

I recently had a problem with Celery and Redis : I was using too many connections to my cloud Redis account.

I seem to have fixed the problem for now with better settings and a bigger Redis plan.

However, it lead me to some experimentation on how Redis and Celery work together, and there is something I don't understand : the number of Redis ConnectionPool that are created by the Python Redis module.

I configured Celery to use Redis as a broker and a result backend.

Here is my Celery config :

CELERY_TIMEZONE = 'Europe/Paris' CELERY_BROKER_URL = REDIS_URL CELERY_RESULT_BACKEND = REDIS_URL CELERY_TASK_SERIALIZER = 'pickle' CELERY_SEND_EVENTS = False CELERY_IMPORTS = ('task1', 'task2') CELERY_ACCEPT_CONTENT = ['pickle'] CELERY_REDIS_MAX_CONNECTIONS = 2 BROKER_POOL_LIMIT = 1 BROKER_HEARTBEAT = None BROKER_TRANSPORT_OPTIONS = {    'max_connections': 30 } 

I run Celery with the following command :

celery worker --without-gossip --without-mingle --without-heartbeat --concurrency=1 --app=backZest.celery

Right after Celery has booted, I already have 10 connections to my Redis instance : is that normal?

I checked how many redis ConnectionPool were created : to do this, I just modified the Python Redis module connection.py file, in the __init__ method of the ConnectionPool class:

import sys print "ConnectionPool instance %s with %s max_connections" % (id(self), self.max_connections) sys.stdout.flush() 

As you can see, there are many (8) pools created, all of them with the same max_connections, 30, which is my setting in BROKER_TRANSPORT_OPTIONS :

ConnectionPool instance 4412957392 with 30 max_connections ConnectionPool instance 4412959888 with 30 max_connections ConnectionPool instance 4420369616 with 30 max_connections ConnectionPool instance 4420370320 with 30 max_connections ConnectionPool instance 4420367056 with 30 max_connections ConnectionPool instance 4420491792 with 30 max_connections ConnectionPool instance 4422318224 with 30 max_connections ConnectionPool instance 4422319504 with 30 max_connections 

I don't understand why there are so many of them. I want to control the number of connections to my Redis. I can control the number of max_connections per ConnectionPool but isn't it a bit useless if I don't control the number of ConnectionPool created ?

Thanks a lot for your help !

like image 339
Noé Malzieu Avatar asked Feb 26 '16 14:02

Noé Malzieu


People also ask

How does Redis connect to celery?

Configure CeleryEdit the file with your text editor and add the code. In the code shown above, start by importing the required modules. Next, we set the BROKER_URL which holds the URL to the Redis database. Then, we create an instance of the Celery class and pass the current module and URL as the parameters.

What is Redis and celery?

Celery is an asynchronous task queue/job queue based on distributed message passing. It is focused on real-time operation, but supports scheduling as well. What is Redis? An in-memory database that persists on disk. Redis is an open source, BSD licensed, advanced key-value store.


1 Answers

Yes, this is normal. Pool creates connections beforehand so they are ready when you need them. Establishing a connection may take a lot of time, so pool helps you save on that. Use max_connections to restrict the pool as you need it. And don't worry that pool opens connections early.

like image 103
Imaskar Avatar answered Oct 06 '22 20:10

Imaskar