Last update on .

Django Redis Tutorials and documentation On Caching and Sessions

Django redis is a Full featured redis cache backend for Django. Django has many caching framework some which include memache

Introduction to Django Redis

Django Redis a Full featured redis cache/session backend for Django. Django's official cache backend and loved happens to be memcached it's fast and widly used, But there are now a support for django cache/session using redis.

Django redis features and Why you should use django-redis?

  • In active development.

  • Uses native redis-py url notation connection strings.

  • Pluggable clients.

  • Pluggable parsers.

  • Pluggable serializers.

  • Master-Slave support in the default client.

  • Complete battery of tests.

  • Used in production in several projects as cache and session storage.

  • Supports infinite timeouts.

  • Facilities for raw access to Redis client/connection pool.

  • Highly configurable (can emulate memcached exception behavior, for example).

  • Unix sockets supported by default.

  • With support for python 2.7, 3.4, 3.5 and 3.6

Django Redis User Guide


The simplest way to use django-redis in your project is to install it with pip:

pip install django-redis

To start using django-redis, you should change your Django cache settings to something like this After a successful installation here is how you can configure the cache.

    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",

django-redis uses the redis-py native url notation for connection strings, it allows better interoperability and has a connection string in more "standard" way.

This is a examples of url format


Three URL schemes are supported:

  • redis://: creates a normal TCP socket connection

  • rediss://: creates a SSL wrapped TCP socket connection

  • unix:// creates a Unix Domain Socket connection

There are several ways to specify a database number:

  • A db querystring option, e.g. redis://localhost?db=0

  • If using the redis:// scheme, the path argument of the url, e.g. redis://localhost/0

In some circumstances the password you should use to connect redis is not URL-safe, in this case you can escape it or just use the convenience option in OPTIONS dict:

    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "PASSWORD": "mysecret"

Take care, that this option does not overwrites the password in the uri, so if you have set the password in the uri, this settings will be ignored.

Configure as session backend

Django can by default use any cache backend as session backend and you benefit from that by using django-redis as backend for session storage without installing any additional backends:

SESSION_ENGINE = "django.contrib.sessions.backends.cache"

Testing with django-redis

django-redis supports customizing the underlying Redis client. This can be used for testing purposes, e.g., by replacing the default client with mockredis. Doing so allows you to run your integration tests without depending on a real Redis server.

In case you want to flush all data from the cache after a test, add the following lines to your TestCase:

def tearDown(self):
    from django_redis import get_redis_connection



Comments are closed.