Topic · Data & state

Redis & caching for Django in production

Caches, sessions, and Redis itself: what to cache, where it lives, and what happens when memory fills.

the cache sits in front of slow workRequestRedis?get(key)hit: returnabout a millisecondmiss: query, then setthe slow pathEviction, timeouts, and invalidation decide whether a cache helps or hurts

Redis serves Django as a cache, a session store, and a Celery broker. Each role needs different settings, and mixing them on one instance is a common source of lost tasks and surprise evictions.

The guides cover setting Redis up for each role, caching strategy in Django, and the connection and memory errors that follow from getting it wrong.

8Set it up
9Run it well
2Check it
12Fix it

Set it up

Guides that take this from nothing to running in production.

  • Set Up Redis Caching for Django in Production

    Configure Django's Redis cache for production: memory limits, eviction, authentication, key prefixes, timeouts, cached sessions, and checks.

  • Configure Memcached for Django in Production

    Configure Memcached as Django’s cache in production: choose PyMemcacheCache, size memory and item limits, run several nodes with consistent hashing, secure the port, handle failures gracefully, and know what not to store there.

  • Redis vs Memcached for Django Caching

    Compare Redis and Memcached as the Django cache backend: data structures, persistence, eviction, multi-purpose use for sessions, Celery, and rate limiting, memory efficiency, scaling, and managed options, with a recommendation.

  • Use Valkey With Django for Caching, Sessions, and Celery

    Use Valkey, the open-source Redis fork, with Django: connect Django’s Redis cache backend or django-valkey, run Celery and Channels against it, migrate from Redis with replication or snapshots, and use managed Valkey on AWS, Google Cloud, and DigitalOcean.

  • Use AWS ElastiCache (Redis OSS or Valkey) With Django

    Connect Django to Amazon ElastiCache for caching, sessions, and Celery: choose Valkey or Redis OSS, serverless or node-based clusters, enable TLS and AUTH or IAM, use the primary endpoint, configure security groups, and handle failovers and cluster mode.

  • Use Upstash Redis With Django

    Use Upstash serverless Redis with Django for caching, sessions, rate limiting, and Celery: connect over rediss:// with TLS, understand per-request pricing and connection limits, choose regions, and know when the REST API suits serverless deployments better.

  • Put Varnish Cache in Front of Django

    Use Varnish as an HTTP cache in front of Django: send correct Cache-Control headers from views, write VCL that bypasses logged-in users and POST requests, strip tracking cookies, purge or ban pages on content changes, and run Varnish with Nginx for TLS.

  • Configure Redis and Celery for Django Production

    Running background jobs in Django production is not just a matter of installing Celery and pointing it at Redis.

Run it well

Tuning, reliability, and operations once it works.

  • Django Caching Strategy for Production

    Choose what to cache in a Django app: HTTP caching, per-view and fragment caches, low-level caching, invalidation, stampede protection, and hit rates.

  • Configure Redis Persistence for Django and Celery

    Decide which Redis data must survive restarts in a Django stack: AOF versus RDB for Celery queues and sessions, no persistence for caches, fork memory, Docker volumes, and verifying recovery.

  • Speed Up Django With Template Fragment Caching

    Use Django template fragment caching in production: the cache tag with vary-on keys, per-user and per-language fragments, cached template loaders, invalidation with make_template_fragment_key, and avoiding stale or leaked content.

  • Automatic ORM Query Caching in Django With Cachalot or Cacheops

    Cache Django ORM queries automatically with django-cachalot or django-cacheops: how invalidation works, when it speeds up read-heavy apps, which tables to exclude, multi-process and raw SQL pitfalls, and how to measure hit rates before trusting it in production.

  • Prevent Cache Stampedes in Django

    Stop cache stampedes (dogpile effects) in Django when a popular cache key expires and hundreds of requests recompute it at once: use locks, stale-while-revalidate, jittered timeouts, background refresh, and probabilistic early expiration.

  • Choose the Right Redis maxmemory-policy for Django

    Pick Redis maxmemory and maxmemory-policy settings for Django workloads: allkeys-lru or allkeys-lfu for caches, noeviction for Celery brokers and locks, volatile policies for mixed use, and how to split instances so evictions never drop queued tasks or sessions.

  • Cache Django Responses with Nginx proxy_cache

    Use Nginx proxy_cache in front of Django safely: cache zones, bypassing logged-in users, respecting Cache-Control, stale serving, and purging.

  • Configure Django Sessions for Production

    Choose and tune Django's session backend for production: database, cached_db, cache, or signed cookies; cookie security flags; expiry; write frequency; and cleaning up expired sessions.

  • Configure Django REST Framework Throttling for Production

    Set up Django REST Framework throttling that works in production: shared Redis cache, AnonRateThrottle and UserRateThrottle defaults, ScopedRateThrottle per endpoint, correct client IPs behind proxies with NUM_PROXIES, Retry-After headers, and custom throttles per API key.

Check it

Review lists to run before launch or after a change.

  • Redis Production Checklist for Django

    A production checklist for Redis in Django stacks: separate roles for cache, sessions, and Celery; memory limits and eviction; persistence; network security and TLS; client timeouts; monitoring; and upgrades.

  • Wagtail Production Checklist

    A production checklist for Wagtail CMS sites: WAGTAILADMIN_BASE_URL and Sites, image renditions and storage, search backend, cache invalidation and frontend caching, admin security, scheduled publishing, backups of media, and upgrade routine.

Fix it

Errors and symptoms, each traced to a cause and a fix.

Learning paths through this topic

Related topics