When Should a Low-Traffic Django Site Switch to Signed-Cookie Sessions?
0 reputation · 08 Mar 2023, 08:27 UTC
0 reputation · 08 Mar 2023, 08:27 UTC
For a low-traffic Django project, the default database-backed session engine writes to the django_session table on every request that touches the session, which can dominate database costs even when overall traffic is modest. The signed_cookies backend moves session data into a signed cookie, eliminating that per-request database write.
However, that change introduces constraints: session data is signed but not encrypted, so clients can read the payload; server-side revocation is impossible before natural expiry; cookie size is practically limited to roughly 4 KB; and rotating SECRET_KEY invalidates all outstanding sessions at once. These behaviors are documented, but their practical impact depends on the installed Django version and how the application uses sessions.
Given these tradeoffs, what signals indicate that a low-traffic site should move from db or cached_db sessions to signed_cookies? Which session usage patterns make the loss of server-side invalidation unacceptable? How should the ~4 KB cookie ceiling and SECRET_KEY rotation policy factor into the decision?
32775 reputation · 08 Mar 2023, 12:59 UTC
Switch to signed_cookies only when three conditions hold: profiling shows session writes are a large share of database writes, the session payload is small and stable, and your application can tolerate no server-side logout before cookie expiry. If any condition fails, keep db or cached_db.
The likely explanation for high cost is that the django_session table write volume dominates your database bill. The confirmed facts are the backend's documented constraints: signed cookies are readable by the client (signing prevents tampering, not disclosure), server-side revocation is impossible before natural expiry, the cookie must fit within roughly 4 KB, and rotating SECRET_KEY invalidates all outstanding sessions at once unless your Django version supports staged fallback keys.
CaptureQueriesContext or query logging around representative requests to count INSERT/UPDATE statements on django_session and inspect request.session.modified. With db or cached_db, Django saves only when the session is modified or SESSION_SAVE_EVERY_REQUEST is True; do not assume every request writes.cached_db, a cache tier, or a read replica is safer than moving to signed cookies.SESSION_COOKIE_NAME and attributes, and keep the signed value well under 4 KB. Verify with your actual browser and proxy stack because limits vary.signed_cookies is the wrong backend.SECRET_KEY rotation is routine or compliance-driven, confirm your installed Django version supports fallback keys or keep server-side sessions.SESSION_ENGINE effectively abandons existing sessions; expect forced re-login. After the change, test login, logout, CSRF, and any code that writes request.session.Do you require immediate session invalidation for security events? If yes, do not switch. If no and writes dominate, proceed only after the payload-size and rotation checks pass.
Version assumptions: the constraints above reflect Django's documented session framework. Exact serialization, compression, and SECRET_KEY fallback behavior vary by version, so inspect the installed backend before committing.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.