Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 42 ยท Working with Redis

Redis Caching in Django and Flask

Cache with Redis in Django and Flask: the CACHES setting, per-view and low-level cache API, sessions in Redis, Flask-Caching and hand-written cache-aside with invalidation.

Python web frameworks treat Redis as a pluggable cache backend. You describe where the server is, and the framework's own cache API (cache.get, cache.set, a view decorator) does the rest. This lesson shows the configuration and the calls in Django and Flask, then the habits that keep a cache correct: key design, TTLs and invalidation.

The Django and Flask code needs a running server and the framework, so it appears as read-only blocks. The runnable examples are JavaScript models of the same logic over a fake clock, in the style of the other lessons in this chapter. For the client library underneath, see Using Redis from Node.js, Python, PHP and Java; for the theory, see caching patterns.

Package names and settings change between releases. The examples assume Django 4.0 or later (which includes a Redis cache backend), redis-py 5 or later, and a current Flask-Caching.

Django: the CACHES setting

Install the client (pip install redis) and add a Redis cache to settings.py:

Python
CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.redis.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",   # database 1, so it never mixes with other data
        "KEY_PREFIX": "shop",                       # keeps several apps on one server apart
        "TIMEOUT": 300,                             # default TTL in seconds
    }
}

LOCATION can be a list of URLs for a primary and replicas, and a password goes in the URL (redis://:secret@host:6379/1) or an environment variable. Before Django 4.0 the usual choice was the third-party django-redis package ("BACKEND": "django_redis.cache.RedisCache"), which adds options such as compression, custom serializers and cache.keys()-style helpers; it is still popular and uses the same cache API below.

Django builds the real Redis key from the prefix, a version number and your key: shop:1:product:42. Changing the version (an argument to cache.get/set, or the VERSION setting) makes every old entry unreachable at once, a cheap way to invalidate a whole cache after a deploy that changes the stored shape.

Django: the cache API

The low-level API is the same for every backend:

Python
from django.core.cache import cache

cache.set("product:42", product_dict, timeout=600)   # SET with an expiry
product = cache.get("product:42")                      # None on a miss
cache.get("missing", "fallback")                        # a default for misses
cache.get_or_set("config", load_config, timeout=60)   # call the function only on a miss
cache.delete("product:42")                             # invalidate
cache.incr("page_views")                               # atomic counter (the key must exist)
cache.set_many({"a": 1, "b": 2}, 60); cache.get_many(["a", "b"])

Values are pickled by default, so any picklable Python object works, but they are readable only from Python. Never unpickle data an attacker can write into Redis. A write-heavy or shared-with-other-languages cache is better off with a JSON serializer (django-redis lets you choose one).

The per-view cache stores a whole rendered response keyed by URL:

Python
from django.views.decorators.cache import cache_page
from django.views.decorators.vary import vary_on_headers

@cache_page(60 * 15)                       # cache this view for 15 minutes
@vary_on_headers("Accept-Language")        # one entry per language
def product_list(request):
    ...

Do not use it for pages that differ per user (a logged-in header, a cart), or one user's page may be served to another. For those, cache the expensive fragment or query result with the low-level API and keep the user-specific part dynamic. Template fragments have {% cache 300 sidebar request.user.id %}.

Django: sessions in Redis

By default Django stores sessions in the database, which means a query on almost every request. Moving them to the cache removes it:

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

This is fast, but a cache can evict entries, and eviction logs the user out. Two safer choices: use "django.contrib.sessions.backends.cached_db" (reads from Redis, writes through to the database, so nothing is lost) or give sessions their own Redis cache alias with persistence and the noeviction policy, separate from the disposable page cache. See sessions and rate limiting for the data-model side.

Flask: Flask-Caching

Flask has no built-in cache, and the usual extension is Flask-Caching:

Python
from flask import Flask
from flask_caching import Cache

app = Flask(__name__)
app.config.update(
    CACHE_TYPE="RedisCache",
    CACHE_REDIS_URL="redis://localhost:6379/0",
    CACHE_DEFAULT_TIMEOUT=300,
    CACHE_KEY_PREFIX="shop:",
)
cache = Cache(app)

@app.route("/products/<int:product_id>")
@cache.cached(timeout=60, query_string=True)     # key includes the URL and query string
def product(product_id):
    ...

@cache.memoize(timeout=120)                       # caches by function arguments
def top_sellers(category):
    return run_expensive_query(category)

cache.delete_memoized(top_sellers, "books")       # invalidate one argument set

cached stores a whole view response, so it has the same per-user warning as Django's cache_page. memoize caches a function result keyed by its arguments.

Flask without an extension: cache-aside by hand

The extension is optional. Many teams write the cache-aside pattern directly with redis-py, which makes key names, serialization and failure handling explicit:

Python
import json
import redis

r = redis.Redis(host="localhost", port=6379, decode_responses=True, socket_timeout=0.5)

def get_product(product_id: int) -> dict:
    key = f"v1:product:{product_id}"
    try:
        cached = r.get(key)
        if cached is not None:
            return json.loads(cached)
    except redis.RedisError:
        pass                                   # cache down: fall through to the database
    product = load_product_from_db(product_id)
    try:
        r.set(key, json.dumps(product), ex=300)
    except redis.RedisError:
        pass
    return product

def update_product(product_id: int, data: dict) -> None:
    save_product_to_db(product_id, data)
    r.delete(f"v1:product:{product_id}")        # invalidate; the next read repopulates

The three details that matter are all there: a versioned key (v1:), a TTL as a safety net even though you also delete on write, and a try/except so a Redis outage becomes slower responses rather than errors, as discussed in cache invalidation and stampedes.

Try it: Django-style keys, versions and get_or_set

The first model reproduces how Django's cache builds keys from prefix, version and name, how get_or_set runs the loader only on a miss, and how bumping the version invalidates everything at once.

JS Django cache API: prefix, version, get_or_set

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The MISSING marker matters: get_or_set must tell "not cached" from a cached None. Django uses a sentinel object internally for the same reason. The bump from version 1 to 2 leaves the old keys in Redis until their TTL runs out, which is the price of never having to scan and delete.

Try it: cache-aside with invalidation and a Redis outage

The second model runs the hand-written Flask pattern against a fake database and a cache that can be switched off. It counts database reads, so you can see what caching saves and what a missing invalidation costs.

JS Cache-aside, write invalidation and a cache outage

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first update shows why the TTL alone is not enough: for up to five minutes the page shows the old price. The invalidating update fixes it, and the outage turns into extra database reads, not errors.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In Django, what does bumping the cache VERSION do?

    Django builds keys as prefix:version:key. A new version reads different keys, so old entries are never found and simply expire.
  2. 2. Why is @cache_page risky for pages that show the logged-in user's name?

    The cached response is keyed by URL (and any Vary headers), not by user. Cache the shared parts and keep the user-specific part dynamic.
  3. 3. What is the risk of putting Django sessions in a plain cache that can evict entries?

    A cache may drop entries under memory pressure. Use cached_db or a separate Redis instance with persistence and the noeviction policy for sessions.
  4. 4. In hand-written cache-aside, why wrap the Redis calls in try/except?

    The cache is an accelerator. If it fails, fall back to the database and keep serving.

Summary

  • Django configures Redis in CACHES (the built-in backend since 4.0, or django-redis), builds keys as prefix:version:key, and offers cache.get/set/get_or_set/delete/incr, @cache_page and template fragment caching.
  • Move sessions to the cache with SESSION_ENGINE, but prefer cached_db or a dedicated persistent Redis so evictions do not log people out.
  • Flask-Caching adds @cache.cached and @cache.memoize with CACHE_TYPE="RedisCache"; plain redis-py cache-aside is equally valid and more explicit.
  • Version your keys, set a TTL as a safety net, delete on write, and treat a cache failure as a miss.
  • Never cache per-user pages as shared responses, and never unpickle untrusted cache data.

Next, step back and see the design as a whole in Redis in System Design.