Manage User Sessions using Redis

In the previous blog, we discussed the basics of redis & the implementation of its Python APIs.

Mostly the session files include information regarding user & its relevant context for the session. So, the session files are expected to be small in size. A simple template is used for session file. A new session file is created (using that session template) with the new session containing all relevant information about the user. Application can have specific information in the session file as needed.

Here, we will see how Redis can further be used to manage sessions.



We are taking Odoo as an example application here. Similar idea can be implemented for other applications too. There can be some extra steps or considerations depending on the Application.

So, we will try to manage Odoo sessions in Redis server. It by default manages session files under a specific folder.

Odoo stores the session information against the session ID. So, we can make Odoo store information in Redis using the session ID as key & session information as value. This is a high level idea which needs Odoo specific implementation.


So here, lets see how the same can implemented using Redis Python APIs.


Above, we just tried to create some random data for our example application i.e. Odoo. The context defined above may include some other information also. We created a random key with value containing some bogus values. We used the set() function to create a pair. You can use get() function in your implementation to retrieve the session data. Different data templates can be used for other application.

Note:- The code shows a very basic implementation, which can/should further be enhanced as needed by the specific application.

There are some other features available (e.g. setting expiry to key-value pair,etc) which can make the session management easier.



There are some advantages like Redis offers faster Read/Write operations on Redis server compared to other databases. Storing session key-value pair saves space as some metadata is also stored along with file content. One can set key-value pair to expire after specific time. Otherwise, you may have to set cron jobs externally or may have to run db queries manually to delete expired session files/entries.



As we know that data is stored in RAM instead of disk, there are chances of running out of memory in case of any memory leakage. So, we can do a bench-mark testing to check how the RAM consumption increases with increase in session counts.

We can again use the above script with some changes for bench-marking.


Server Configuration :-

  • Server: – Ubuntu 16.04 LTS
  • RAM : – 32 GB
  • CPUs : – 8
  • Odoo Version :-  11
  • Cloud Platform :- OVH

Note: – We did the testing on new redis installation with no previous data.

The results are as follows.


We started with around 100k sessions & went up-to 8M. Our bench-marking shows that space consumption grows uniformly with session data. The same amount of data could have consumed around 30GB of disk space. This also helped us in countering some unexpected issues with EXT4 filesystem faced when Odoo created 8M session files under a folder.

Note:- The space may differ depending on the size of session value.


We would really appreciate if you can share your implementation.

Thanks for reading!! Hope it helps.

. . .


Add Your Comment

Be the first to comment.