[Answered ]-Changing Django session engine without destroying existing sessions

1👍

Yes, you can make this change without logged in users suddenly finding themselves being logged out. That’s because cached_db checks memcache first for the key and if it cannot be found in it, goes to the database. Thus making this change will not cause a loss of session data. Fragment of code from cached_db

def load(self):
    try:
        data = self._cache.get(self.cache_key)
    except Exception:
        # Some backends (e.g. memcache) raise an exception on invalid
        # cache keys. If this happens, reset the session. See #17810.
        data = None

    if data is None:
        # Duplicate DBStore.load, because we need to keep track
        # of the expiry date to set it properly in the cache.

However please note that cached sessions backends are a bit over rated. Depending on the middleware that you have, the session object may be updated very often, as often as every request if only to change the expire date. In that case you will find that the database is being written to all the time. Which means the cached value has to be discarded too.

👤e4c5

1👍

You should be able to. cached_db backend is just a write-through cache to a database backed, persistent, db backend which speeds up your read queries. It will not speed up your write queries, so you should try and find out how much you are reading and writing the session data.

Your Django SECRET_KEY setting determines your session key hashing parametrs along with Session settings that determine the cache you will use for sessions and session your TTLs, so if you are not changing those variables, you should be good.

Leave a comment