Reputation: 15804
In a Python/Django application, is retrieving a value stored in redis slower than retrieving one stored in the request.session
dictionary?
Background:
I have a Django app where I use DB-based sessions. I.e., instead of using django.contrib.sessions
, I used this nifty little 3rd party library.
I recently ran a benchmark whereby I saved a test value in a local redis instance via the redis-py wrapper (i.e. my_server.set('test','1')
). I saved the same test value in request.session['test']
.
I then retrieved the test value from each, and compared the time taken. request.session
out performed redis by a factor exceeding 2x in this scenario.
Problem: The application is not distributed in any way, everything is shared and happens on the same machine - very vanilla set up.
The result appears counter-intuitive to me. Why? Because my sessions are DB based, and I thought redis would be faster than whatever Django has to offer. Clearly, I am wrong.
Can an expert explain what's actually going on here? Maybe the python wrapper on redis' core API is slowing things down?
In case you need more information, or are skeptical about how I ran the benchmark, please do ask.
P.s. I simply put the two competing ways in a for loop for 100K iterations and measured the time taken to complete.
Upvotes: 0
Views: 329
Reputation: 599490
The session is stored as a single blob, not as individual keys. It has almost certainly already been loaded and decoded by the time you get into your view, most likely by the auth middleware. Once it is loaded it is stored locally as a dictionary, which is all that your timing tests will measure.
Upvotes: 2