Customise Session Engine to distribute session data storage

47 views
Skip to first unread message

Web Architect

unread,
Aug 27, 2018, 3:52:06 AM8/27/18
to Django users
Hi,

Right now we are using the Django cache_db session engine for persistent sessions and also for some speed. Is it possible to customise this where I can store some session data in cache and some in DB? For example, I want to store all login sessions in DB and rest like some user specific data - basket etc in cache .

Looking forward to your support....

Thanks. 

Jason

unread,
Aug 27, 2018, 7:31:08 AM8/27/18
to Django users
there's an option to use redis as a session backend, which would be faster than using a regular db, even a well tuned mysql/postgres instance

Web Architect

unread,
Aug 28, 2018, 1:40:38 AM8/28/18
to Django users
Hi Jason,

Thanks for the response. Why not use Django's cache backend and setting cache alias to be redis?

Thanks. 

Jason

unread,
Aug 28, 2018, 7:07:46 AM8/28/18
to Django users
Actually, there's nothing stopping you from using redis for cache and session cache.  

SESSION_ENGINE = 'django.contrib.sessions.backends.cache'

will use your cache to store sessions.  and if you use cached_db instead of cache, that data will persist in redis and survive restarts.

Jason

unread,
Aug 28, 2018, 7:09:00 AM8/28/18
to Django users
and redis-sessions seems to be a wrapper around with more functionality, but the lib is a little out of date.  Got confused by the latest commit in master making it seem it was under maintenance but the latest release is over a year ago.

Web Architect

unread,
Aug 30, 2018, 8:24:16 AM8/30/18
to Django users
We were actually cache_db but the sessions were consuming too much of DB. Hence, we want some sessions to be persistent in DB and some to be volatile and not stored in DB. Hence, I was looking for some way to achieve that. Like I want all login sessions to be stored in DB whereas rest in cache. 

Jason

unread,
Aug 30, 2018, 8:53:10 AM8/30/18
to Django users
https://github.com/adw0rd/django-multi-sessions

that's a non-working project but might give you some clues how to route sessions to different backends.

Web Architect

unread,
Sep 3, 2018, 10:36:39 AM9/3/18
to Django users
Thanks. This seems to be quite helpful. Will look into it. 
Reply all
Reply to author
Forward
0 new messages