Redis & caching for Django in production
Caches, sessions, and Redis itself: what to cache, where it lives, and what happens when memory fills.
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.
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.
- Fix Redis Connection Refused in Django
Fix Redis connection refused errors from Django caches, sessions, Celery, and Channels: service status, bind addresses, Docker hostnames, TLS URLs, auth, and limits.
- Fix Redis "OOM command not allowed" in Django
Fix Redis OOM command not allowed when used memory exceeds maxmemory in Django: find what fills Redis, choose the right eviction policy for cache versus Celery, set TTLs, and split instances.
- Fix Redis "max number of clients reached" With Django
Fix the Redis ERR max number of clients reached error in Django and Celery: count connections per process, cap connection pools, set timeouts for idle clients, and raise maxclients and file limits safely.
- Fix Redis MISCONF RDB Snapshot Errors in Django
Fix the Redis MISCONF error that blocks writes from Django and Celery: find why background saves fail (disk full, permissions, memory overcommit), restore writes safely, and decide whether the instance needs persistence at all.
- Fix Django Cache Not Working in Production
Fix a Django cache that never hits in production: confirm the configured backend, avoid LocMemCache across processes, understand why Vary Cookie and CSRF break per-view caching, and check keys, versions, and timeouts.
- Fix Redis "LOADING Redis Is Loading the Dataset in Memory" in Django
Fix the Redis error LOADING Redis is loading the dataset in memory after restarts or failovers in Django and Celery: understand RDB and AOF load time, make the cache degrade gracefully, add retries, and keep cache data out of persistence.
- Fix Redis "READONLY You Can’t Write Against a Read Only Replica"
Fix the Redis error READONLY You can’t write against a read only replica in Django and Celery: point clients at the primary endpoint, handle failovers with Sentinel or managed endpoints, and stop stale DNS and connection pools from writing to replicas.
- Fix Redis "NOAUTH Authentication Required" and WRONGPASS in Django
Fix Redis NOAUTH Authentication required and WRONGPASS invalid username-password pair errors in Django and Celery: put the password in the Redis URL correctly, URL-encode special characters, use ACL usernames, and keep cache and broker settings in sync.
- Fix Redis SSL CERTIFICATE_VERIFY_FAILED With rediss:// in Django and Celery
Fix SSL CERTIFICATE_VERIFY_FAILED errors when Django, Celery, or Channels connect to Redis over rediss:// on Heroku, Upstash, ElastiCache, or Azure: configure ssl_cert_reqs, CA bundles, and broker_use_ssl without disabling verification blindly.
- Fix Django Cache Errors After Deploy: "Can’t Get Attribute" When Unpickling
Fix Django errors after a deploy such as AttributeError Can’t get attribute on module, ModuleNotFoundError, or TypeError when reading cached objects: version cache keys per release, avoid pickling model instances, and clear or namespace the cache safely.
- Fix Celery Tasks Executing Twice (Redis Visibility Timeout)
Fix Celery tasks that run two or three times with a Redis broker: understand visibility_timeout, ETA and countdown redelivery, acks_late, and make long or scheduled tasks safe from duplicate execution.
- Fix Django "Session data corrupted" Warnings
Fix the Django warning Session data corrupted and users being logged out: find SECRET_KEY changes and mismatches between servers, use SECRET_KEY_FALLBACKS for rotation, and align session engines and serializers.