Skip to content

Use cached_db sessions by default#710

Closed
pyanderson wants to merge 4 commits intomainfrom
560-use-cache-db-sessions-by-default
Closed

Use cached_db sessions by default#710
pyanderson wants to merge 4 commits intomainfrom
560-use-cache-db-sessions-by-default

Conversation

@pyanderson
Copy link
Copy Markdown
Contributor

@pyanderson pyanderson commented Apr 24, 2026

Description

This PR improves the backend session setup by configuring Django to use Redis as the default cache and cached_db
as the session engine, bringing the application closer to its intended runtime behavior. It also improves
development visibility by enabling more detailed Gunicorn access logs, making it easier to inspect requests and
debug backend behavior locally. Since the new session setup introduces a cache dependency, the test configuration
was updated to use an in-memory cache to keep tests isolated and independent from Redis.

Motivation and Context

The changes in this PR are grouped because they all support the same backend infrastructure improvement:

  1. Session/cache setup: configure Redis as the default Django cache and switch sessions to cached_db.
  2. Developer visibility: improve Gunicorn access logging in development to make request behavior easier to
    inspect.
  3. Test isolation: override the cache backend in tests to use in-memory storage instead of Redis.

Overall, this brings development and runtime behavior closer together while keeping the test environment
lightweight and deterministic.

Screenshots (if appropriate):

N/A

Steps to reproduce (if appropriate):

  • Create a new project using this branch as the template:
django-admin startproject new_styling --extension py,json,yml,yaml,toml --name Dockerfile,README.md,.env.example,.gitignore,Makefile,.npmrc --template=https://github.com/vintasoftware/django-react-boilerplate/archive/refs/heads/560-use-cache-db-sessions-by-default.zip

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires documentation updates.
  • I have updated the documentation accordingly.
  • My change requires dependencies updates.
  • I have updated the dependencies accordingly.

Closes #560

@pyanderson pyanderson self-assigned this Apr 24, 2026
@pyanderson pyanderson marked this pull request as ready for review April 24, 2026 18:30
@pyanderson pyanderson requested a review from fjsj April 24, 2026 18:30
Copy link
Copy Markdown
Member

@fjsj fjsj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Redis is unnecessary here? cached_db means using the DB as cache, not Redis.

@pyanderson
Copy link
Copy Markdown
Contributor Author

pyanderson commented Apr 24, 2026

I think Redis is unnecessary here? cached_db means using the DB as cache, not Redis.

cached_db doesn't mean “use the DB as cache”. It means sessions are persisted in the database, with a
cache layer in front for performance.

That cache layer does not have to be Redis specifically. It could also be DatabaseCache, Memcached, or
local-memory, but Django generally recommends Redis or Memcached for production and discourages
local-memory cache in multi-process setups

https://docs.djangoproject.com/en/5.2/topics/http/sessions/#using-cached-sessions

Warning
You should only use cache-based sessions if you’re using the Memcached or Redis cache backend. The local-memory cache backend doesn’t retain data long enough to be a good choice, and it’ll be faster to use file or database sessions directly instead of sending everything through the file or database cache backends. Additionally, the local-memory cache backend is NOT multi-process safe, therefore probably not a good choice for production environments.

@pyanderson pyanderson requested a review from fjsj April 24, 2026 21:33
@fjsj
Copy link
Copy Markdown
Member

fjsj commented Apr 27, 2026

Yep, I got confused, but what I wanted was what we had in other simple projects by using locmem. I see docs now don't recommend locmem, as you quoted: "The local-memory cache backend doesn’t retain data long enough to be a good choice, and it’ll be faster to use file or database sessions directly instead of sending everything through the file or database cache backends.". So I guess the best is to drop this. In general having Redis as a failure-point to get some little performance gain is not the best option for most projects. Thanks anyway for the PR! Dropping it.

@fjsj fjsj closed this Apr 27, 2026
@fjsj fjsj deleted the 560-use-cache-db-sessions-by-default branch April 27, 2026 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use cached_db sessions by default

2 participants