sessions table not pruning? #52123
Replies: 7 comments
-
Session data is pruned depending on a random number generated at each request. This mechanic is referred as lottery. It is configured on your app's If your app has a very low request count, you can try increasing the odds of session data being pruned (aka garbage collected). As an alternative, you can write an artisan command that runs this garbage collection mechanics, and use the command scheduler to run it periodically. |
Beta Was this translation helpful? Give feedback.
-
This should help you out <?php // ./routes/console.php
use Illuminate\Support\Facades\Artisan;
Artisan::command('session:gc', function () {
session()->getHandler()->gc(60 * config('session.lifetime'));
})->twiceDaily(); |
Beta Was this translation helpful? Give feedback.
-
@rodrigopedra My application gets around 750 - 1,000 requests per minute with each request ranging from maybe a few ms to seconds. I'm slightly confused on the sessions table then because I thought this table would exclusively be for "users" of the application, e,g: those that log in to the system. I have some publically accessible API endpoints that are consumed but other systems that are no-auth endpoints, it would appear that these too enter the sessions table but are never cleared? |
Beta Was this translation helpful? Give feedback.
-
Every request that goes under the If your public facing site does not need any kind of session data, you can create a new middleware group and apply to those routes. Regarding the API endpoints, those should not have any session attached. Run: php artisan route:list -v And check which middleware are applied to these routes. The Also, only define API endpoints on your |
Beta Was this translation helpful? Give feedback.
-
API routes are only defined in my API routes file. These are the public ones. I've implemented that session garbage handler command which I'll deploy next week. Just to clarify the purposes of doing the My session lifetime variable is 1440, I'm presuming I could reduce the 60 down to something like 2 or 3? |
Beta Was this translation helpful? Give feedback.
-
The In the configuration file, you define it in minutes. The Internally, it will delete sessions that are stale for more than that threshold. framework/src/Illuminate/Session/DatabaseSessionHandler.php Lines 279 to 282 in f5f9383 |
Beta Was this translation helpful? Give feedback.
-
That is the same that the framework/src/Illuminate/Session/Middleware/StartSession.php Lines 170 to 180 in f5f9383 framework/src/Illuminate/Session/Middleware/StartSession.php Lines 254 to 257 in f5f9383 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Laravel Version
11.15.0
PHP Version
8.3.7
Database Driver & Version
MySQL 8
Description
I'm using the sessions table for authentication. I only have a user or two in my database, yet have over 9,000 sessions stored in the sessions table. I'm using Laravel Livewire and everything in one system.
Really the sessions table should be pruned
Steps To Reproduce
env config:
Beta Was this translation helpful? Give feedback.
All reactions