Opened 7 years ago

Closed 6 years ago

Last modified 6 years ago

#6791 closed (fixed)

Cached DB Session Backend

Reported by: jhenry Owned by: jacob
Component: contrib.sessions Version: 1.0
Severity: Keywords:
Cc: pytechd@…, jdunck@…, waylan@…, gabor@…, peter_kese Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Right now, I consider the "cache" session backend to be broken, especially when used with the memcached session backend.

In a large memcached environment, servers enter and leave the memcached pool frequently. Each time this happens, the pool mapping changes and automatically logs users out (and loses their session data).

Attached is a new backend which is basically a copy of the DB session backend with a django.core.cache wrapper on accesses to the session data; all writes are write through to the database and cache and all reads come from memcache except in case of a memcache failure when it reads from the database.

Attachments (5)

cached_db.diff (2.3 KB) - added by jhenry <jhenry@…> 7 years ago.
cached_db_with_documentation.diff (3.5 KB) - added by mcroydon 7 years ago.
Added documentation to patch against trunk
cached_db_docs_and_tests.diff (5.5 KB) - added by jdunck 7 years ago.
Changed default session backend to cached_db. Changed docs to be betterer. ;-) Added tests.
cached_db_docs_and_tests-2.diff (4.7 KB) - added by jdunck 7 years ago.
Last patch didn't include the actual code for cached_db backend. :-/
cached_db.py (1.4 KB) - added by aurelio 7 years ago.
Modified cached_db.py used to work with django-1.0-final

Download all attachments as: .zip

Change History (25)

Changed 7 years ago by jhenry <jhenry@…>

Changed 7 years ago by mcroydon

Added documentation to patch against trunk

Changed 7 years ago by jdunck

Changed default session backend to cached_db. Changed docs to be betterer. ;-) Added tests.

comment:1 Changed 7 years ago by jdunck

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to jdunck
  • Patch needs improvement unset
  • Status changed from new to assigned

Changed 7 years ago by jdunck

Last patch didn't include the actual code for cached_db backend. :-/

comment:2 Changed 7 years ago by jdunck

After talking with Jacob, I reverted the default session backend to DB; changing the default is controversial, and this should be a simple win for interested people.

Anyway, cached_db_docs_and_tests-2.diff is the best; I somehow managed to leave cached_db.py out of my prior diff.

comment:3 Changed 7 years ago by Simon Greenhill

  • Triage Stage changed from Unreviewed to Ready for checkin

comment:4 Changed 7 years ago by mrts

  • milestone set to 1.0

Looks to be in scope for 1.0.

comment:5 Changed 7 years ago by Eric <pytechd@…>

  • Cc pytechd@… added

comment:6 Changed 7 years ago by jdunck

  • Cc jdunck@… added
  • Owner changed from jdunck to jacob
  • Status changed from assigned to new

comment:7 Changed 7 years ago by jdunck

  • Owner changed from jacob to jdunck
  • Status changed from new to assigned

comment:8 Changed 7 years ago by jdunck

  • Owner changed from jdunck to jacob
  • Status changed from assigned to new

comment:9 Changed 7 years ago by russellm

  • milestone changed from 1.0 to 1.0 maybe

comment:10 Changed 7 years ago by russellm

Forgot to include an explanation with the status change: This isn't a bug, it's a completely new cache backend. It's a nice-to-have, but not essential.

comment:11 Changed 7 years ago by korpios

  • milestone changed from 1.0 maybe to post-1.0

comment:12 Changed 7 years ago by korpios

  • Cc korpios@… added

comment:13 Changed 7 years ago by korpios

  • Cc korpios@… removed

Changed 7 years ago by aurelio

Modified cached_db.py used to work with django-1.0-final

comment:14 Changed 7 years ago by aurelio

  • Version changed from SVN to 1.0

Hi, thanks for the patch. It was exactly what I was looking for but had to make minor changes in order to get it to work with django 1.0. Specifically added the must_create parameter for the save method and added a flush method that passes the session_key argument for deletion.

Sorry for not posting it as a diff, just wanted to quickly put this up for anyone that might find it useful.

comment:15 Changed 7 years ago by anonymous

  • Cc waylan@… added

comment:16 Changed 7 years ago by gabor

  • Cc gabor@… added

comment:17 Changed 7 years ago by peter_kese

  • Cc peter_kese added

I am running a cron job every night to delete expired sessions.
How does this fit with the cached_db session store strategy?

I'd suggest to add some signals to the Session's save() and delete() functions and update the cache accordingly.

comment:18 Changed 7 years ago by jdunck

I think you're talking about reaping through a database query. Just as model delete signals don't fire when doing that, cache isn't cleared when deleting cached_db from with a database query. Firing a signal per model instance is about as slow as deleting a record at a time-- it defeats the purpose of doing the query rather than looping over queryset results.

Still, mass deletes are a frequent need. I guess there could be a mass invalidation method, but I don't see a good place to do that.

comment:19 Changed 6 years ago by jacob

  • Resolution set to fixed
  • Status changed from new to closed

(In [9727]) Fixed #6791: added a write-through cache session backend: session data is written through the cache to the database, but read from the cache for speed. Thanks to jhenry, mcroydon, and jdunck.

comment:20 Changed 6 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

Note: See TracTickets for help on using tickets.
Back to Top