fix(sessions): Clean old expired sessions #11770
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I witnessed a situation when session table
django_session
increased to a size of 3.43GB with 10M tuples. It was the largest table in the whole DefectDojo.After investigation, I found out that until the user does not log out proactively, the session is not removed even if it is expired:
As soon I ran
./manage.py clearsessions
, the number of tuples jumped from 10M to 44.So Inspired by one blog, I implemented weekly clean-up.
Why "dirty SQL" migration if you already added cronjob?
Great question. When I ran
clearsessions
, it killed my Pod on OOM (multiple times with a gradual increase of memory limit). When I disabled the limiter for memory, Pod was willing to consume 9GB of memory and it was running over 3 hours. This procedure is not possible to execute in a regular migration Job. The reason for the long and memory-consuming procedure is the current implementation ofclearsessions
:https://github.com/django/django/blob/0bac41fc7e4a842e8d20319cba31cc645501c245/django/contrib/sessions/management/commands/clearsessions.py#L16
https://github.com/django/django/blob/0bac41fc7e4a842e8d20319cba31cc645501c245/django/contrib/sessions/backends/db.py#L192
Not sure what is the real reason why
....filter(...).delete()
consumes that much memory and takes that much time (fetching all data before removal? or many SQL logs?) but as soon as I performed raw cleanup (used in migration) on the same dataset (restored from backups) removal jumped for 3 hours to 2 or 3 minutes.