Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mikaeledstrom's avatar

Laravel file based sessions - move to Redis or prune outside requests?

Hello!

We currently use file based sessions in our Laravel app. We have default configuration; life time is 14 days and the lottery is 2%. We have roughly 750.000 files in sessions directory.

I troubleshoot some slow requests and found out that StartSession middleware could take up to 7 seconds. After simulating the code where garbage control happens and deletes all older session files, that could take 7 seconds.

Two options I see could help me improve the performance for these slow requests;

  1. Move pruning outside the requests Basically, change session config ('lottery' => [0, 100]) so that the built in pruning never takes place and instead have a bash script or Laravel command to delete older sessions in the background.

  2. Move sessions to Redis Instead of file based sessions, completely move to Redis.

What are your thoughts? Do you use Redis or file sessions (or anything else)?

0 likes
16 replies
mikaeledstrom's avatar

@vincent15000

I do not think the two posts are similar.

The only similarity is session storage. The other post is about changing without logging out the users. My post is related to performance and not necessary changing.

1 like
Snapey's avatar

is 750K in line with the number of visitors over the last 14 days?

It could be argued that you could increase the lottery, but this shoukd scale with the number of requests.

Are you doing anything to cause sessions to last longer than normal?

1 like
mikaeledstrom's avatar

@Snapey yes, everything is stock laravel. 14 days until purge.

Increase lottery? Why? For me it makes more sense to move the slow purge from the visitors.

1 like
JussiMannisto's avatar

@mikaeledstrom Redis can handle a huge number of reads and writes. Just make sure you have the PhpRedis PHP extension installed and you're using that as the Redis client, not the predis/predis package. See the redis connection in config/database.php.

1 like
mikaeledstrom's avatar

@JussiMannisto Great insights! According to composer.lock, we seem to use predis/predis package. Why do you recommend not to use that one?

1 like
Snapey's avatar

a session is created for every visitor, not just those that login. The session is issued for two hours and reset each time the person comes back with the same cookie.

So after the visitor has gone away for more than 2 hours the session is considered stale, however the session remains. There is no background job to clear stale session files.

Laravel uses a lottery to decide if the visitor gets to wait whilst Laravel does housekeeping. As standard two requests out of 100 will be delayed whilst laravel checks for stale sessions.

There is no way I could imagine you having a site that gathers 750,000 session files. you would need very high traffic, so something is going wrong.

You could start by just clearing any files not created in the last day, and then watch how it grows

1 like
mikaeledstrom's avatar

@Snapey

Sessions expire at 14 days, not 2 hours (standard Laravel). We have no sessions older than 14 days.

We have quite much traffic yes.

1 like

Please or to participate in this conversation.