Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom prefix for redis keys with Celery

I am using redis as a broker between Django and Celery. The redis instance I have access to is shared with many other applications and so the broker is not reliable (the redis keys it uses are deleted by others, the messages often get sent to workers in other applications). Changing redis database does not solve the problem (there are few databases and many applications).

How can I configure Celery to prefix all the keys it uses with a custom string? The docs mention ways to add prefixes to queue names, but that does not affect the redis keys. The underlying library (Kombu) does not seem to let the user prefix the keys it uses as far as I can tell.

like image 426
pintoch Avatar asked Apr 28 '18 13:04

pintoch


People also ask

What is Redis Keyspace?

Keyspace refers to the internal dictionary that Redis manages, in which all keys are stored. Normally Redis keys are created without an associated time to live. The key will simply live forever, unless it is removed by the user in an explicit way, for instance using the DEL command.

Why is Celery Redis broker?

A common message broker that is used with celery is Redis which is a performant, in memory, key-value data store. Specifically, Redis is used to store messages produced by the application code describing the work to be done in the Celery task queue.


2 Answers

This is not supported by Celery yet. A pull request on this subject is currently stalled due to a lack of workforce:

https://github.com/celery/kombu/pull/912

like image 106
pintoch Avatar answered Sep 21 '22 13:09

pintoch


You can just override the prefix value of your celery task.

@shared_task(bind=True)
def task(self, params):
    self.backend.task_keyprefix = b'new-prefix'
like image 26
Grigori Kartashyan Avatar answered Sep 21 '22 13:09

Grigori Kartashyan