Search code examples
djangodjango-channelsdjango-cachedjango-redis

Is it okay to use the same Redis store for both cache and django-channels channel layer in Django?


I have a Django 3.1 app that uses Redis for its cache backing store (django-redis). I wish to use django-channels, which has the ability to use Redis for channel layers.

Is it safe or unsafe to use the same Redis store for both the cache and the django-channels channel layer at the same time? In other words, I wish to have the following in my settings.py, and I want to know if that's okay.

import environ

env = environ.Env()

REDIS_HOST = env('REDIS_HOST', default='127.0.0.1')
REDIS_PORT = env('REDIS_PORT', default='6379')

CACHES = {
    'default': {
        'BACKEND': 'django_redis.cache.RedisCache',
        "LOCATION": "redis://" + REDIS_HOST + ":" + REDIS_PORT + "/0",
        'OPTIONS': {
            'CLIENT_CLASS': 'django_redis.client.DefaultClient',
            'CONNECTION_POOL_KWARGS': {'max_connections': 30},
            'IGNORE_EXCEPTIONS': True,
        }
    }
}

CHANNEL_LAYERS = {
    "default": {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        "CONFIG": {
            "hosts": [(REDIS_HOST, int(REDIS_PORT))],
        },
    }
}

Solution

  • It is safe unless your cache have name collisions, which is rarely to happen since keys are prefixed by default in channels:

    By default, channel key is prefixed with asgi: and group key is prefixed with asgi:group: You can read more here: channel key prefix