Back to Blog

28 Mar 2026

Setting Up Redis in Django

Learn how to set up Redis in your Django app the simple way. From installing on Ubuntu to caching, sessions, and even a touch of WebSockets - without the usual overcomplicated explanations.

Setting Up Redis in Django

Redis is one of those things that gets mentioned all the time when people talk about scaling Django apps, and just like a lot of topics in web development, it’s usually explained in a way that makes it sound far more complicated than it actually is.

In reality, it’s very straightforward to set up, and once you’ve got it running, it opens the door to things like caching, faster sessions, background tasks and even real-time features.

In this tutorial, I’ll walk you through getting Redis installed on Ubuntu, wiring it into Django, and briefly touching on how it fits into WebSockets later on.

Let’s get it installed first.

SSH into your server and run the following commands:



# Update packages
sudo apt update

# Install Redis
sudo apt install redis-server

Now let’s start Redis and make sure it runs on boot:



sudo systemctl start redis
sudo systemctl enable redis

Quick check to make sure it's working:



redis-cli ping

You should see PONG. If you do, nice — Redis is up and running.

Before we hook it into Django, let’s quickly configure it properly.

Open the Redis config file:



sudo nano /etc/redis/redis.conf

Make sure Redis is bound to localhost:



bind 127.0.0.1 ::1

And if you want to be a bit safer, you can also set a password:



requirepass yourstrongpassword

Save the file and restart Redis:



sudo systemctl restart redis

Nice. That’s Redis configured.

Now let’s get it into Django.

Activate your virtual environment and install the required packages:



pip install redis django-redis

Now head over to your settings.py and add the following:



# ~/myproject/settings.py

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "PASSWORD": "yourstrongpassword",
        }
    }
}

That’s all Django needs to start using Redis as a cache.

Let’s quickly test it.



# python manage.py shell

from django.core.cache import cache

cache.set("test_key", "hello world", timeout=60)
print(cache.get("test_key"))

If you see hello world printed back — you’re good.

Cache sorted.

Now let’s actually use it for something useful.

A common use case is caching expensive queries:



from django.core.cache import cache
from .models import Product

def get_products():
    products = cache.get("products")

    if not products:
        products = Product.objects.all()
        cache.set("products", products, timeout=60)

    return products

Now instead of hitting your database every time, Redis will serve it instantly.

Next up — sessions.

If you want Django sessions to run through Redis instead of your database, add this to your settings.py:



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

This means authentication, sessions and messages are all handled by Redis instead of writing to your database constantly.

If you want a safer approach with persistence, you can use:



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

That gives you the best of both worlds.

Now just briefly touching on something you might want later — WebSockets.

Redis is what Django Channels uses to communicate between connections, so if you ever want real-time features like chat or live updates, this is where it comes in.

Install the required packages:



pip install channels channels-redis

Then add this to your settings.py:



ASGI_APPLICATION = "myproject.asgi.application"

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("127.0.0.1", 6379)],
        },
    },
}

That’s all you need for Redis to act as a message broker.

We won’t go deeper into Channels here, but now you know where Redis fits into that picture.

And that’s pretty much it.

Redis is one of those things that seems like a “later problem”, but honestly it’s worth putting in early. It’ll make your app faster, reduce database load, and set you up nicely for scaling.

Give it a go, play around with caching different parts of your app, and you’ll quickly see where it shines.

Happy coding! 😁