After 2 hours the session file is no longer valid, but then it stays there until the garbage cleanup runs which is random and will be dependent on your traffic.
You could temporarily change the odds so that it is more agressive in clearing stale session files
config/session.php
'lottery' => [2, 100],
Don't forget to reload config cache if you make any changes
Yes, if you delete the files then sessions will be lost. If the users have 'remember me' they will be silently logged back in, unless they were in the middle of filling a form, in which case when they press submit they would get a csrf error
We have the "remember me" enabled by default so I suppose it's a good thing then. However, the session lifetime is set to only 2 hours so I think I will wait to see if it helps before purging them.
I notice that there is the option "expire_on_close". If I enable that, will this help avoid this from happening in the future? What are the downsides of using this?
AFAIK It only tells the browser that it should expire the session cookie when the user closes their browser. It does nothing at the server side. Its just a flag that gets set on the session cookie.
Do you have any recommendations on how I can avoid this problem in the future? Whenever I get a DDOS attack, apache's CPU usage goes sky high (I'm only just realising that this is probably caused by the garbage collection checking every single session file).
I'm assuming memcached would be a better solution?