Django caching with Redis backends

David
3 min readJul 20, 2020

How to setup django cache backends with Redis, Redis Sentinel, and Redis cluster

Django supports many different backends for various caching needs. One of the most popular solutions is Redis, an in-memory key-value database. While integration with a single Redis node is quite straight forward, integration with high-availability (HA) Redis solutions, can be more challenging. Luckily there are packages available to simplify this. In this post we’ll look at using Redis, Redis Sentinel, and Redis cluster as caching backends in a Django project.

The django setup for all three redis caching solutions are fairly similar, but note the differences in the location strings and the client_class used for each.

A quick note on versions:
The configurations/setups described below were performed using Centos 7 servers, Redis 6.0.5, python 3.6.5, and Django 2.2.14. Your experience may vary based on different operating systems, package versions, and environment. The python packages used are all available on pypi; availability and support are subject to their maintainers.

First let’s take a look and caching using a single Redis instance.

We’ll need the following (pip-install-able) packages to use redis in the django project:

 django-redis==4.12.1
redis==3.5.3

In the django settings.py configuration for caches we’ll set the backend, location, and client_class. It should look like the following:

CACHES = {
'default: {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': 'redis://my_redis_node:6379/1',
'OPTIONS': {
'CLIENT_CLASS': 'django_redis.client.DefaultClient',
'PASSWORD': 'my_password',
},
'KEY_PREFIX': 'my_key_prefix',
}
}

Note the “/1” at the end of the LOCATION string indicates the database. The KEY_PREFIX parameter is optional and the PASSWORD parameter is only needed if you have a password set in the configuration on your redis host.

Next let’s look at using redis sentinel. We’ll need the following packages to use redis sentinel in the django project:

django-redis==4.12.1
redis==3.5.3
django-sentinel==0.1.0

In the djano settings.py we need to configure the following:

CACHES = {
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': 'my_primary/my_first_redis_node:26379,my_second_redis_node:26379,my_third_redis_node:26379/1',
'OPTIONS': {
'CLIENT_CLASS': 'django_sentinel.sentinel.SentinelClient',
'PASSWORD': 'my_password',
},
'KEY_PREFIX': 'my_key_prefix',
}
}

Note the contents of the LOCATION string. This includes the name of the primary sentinel, ALL nodes running sentinel (including the port) comma separated, and the database, separated by slash. As with the single-node example above, the KEY_PREFIX parameter is optional and the PASSWORD is only needed if you have a password configured on your redis setup.

Next let’s look at using redis cluster. We’ll need the following packages to use redis in the django project:

django-cluster-redis==1.0.5
django-redis==4.12.1
redis==3.5.3

The settings.py configuration will contain the following:

CACHES = {
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': [
'redis://my_first_redis_node',
'redis://my_second_redis_node',
'redis://my_third_redis_node',
],
'OPTIONS': {
'PASSWORD': 'my_pasword',
'REDIS_CLIENT_CLASS': 'django_cluster_redis.cache.ClusterRedis',
},
'KEY_PREFIX': 'my_key_prefix'
}
}

Note the contents of the LOCATION string. For redis cluster this is a list containing all nodes in the cluster.
As with the examples above, the KEY_PREFIX parameter is optional and the PASSWORD is only needed if you have a password configured on your redis setup.

There are many packages available to aid with caching, and general support between python the redis. What works properly for you might depend on your system, environment, deployment, etc.
Verification on your configuration can be performed in your django shell by performing simple cache operations like set and get. For example:

from django.core.cache import cache
cache.set('my_key', 'my_value)
cache.get('my_key')

It’s as simple as that! Using the applicable setup above your django project should be all set for caching with your preferred redis solution.

--

--