📜 ⬆️ ⬇️

PHP: first introduction to the garbage collection

I recently faced a small problem: the data from the session randomly disappeared during a simple session for more than 24 minutes (as it turned out later).

Here is what the manual told me:
Overdue sessions are not actually destroyed immediately after 24 minutes. Here's how it happens: at the beginning of each request that uses the session (due to a prior call of the session_start () function or setting session / auto_start to on ), there is a 1% chance that the PHP interpreter will scan all the sessions on the server and remove any of the expired ones. “1% of opportunity” for a computer program sounds completely unpredictable. The way it is. But such unpredictability improves overall performance. An intensively working site, busy at the start of each request, searching for expired sessions to remove them, will use up too much server resources.

This explains the “randomness” of the data being deleted.
But how to solve this problem, since in my project idle time can easily be more than 24 minutes.

Again, refer to the manual:
The session.gc_maxlifetime configuration directive controls the setting of the maximum idle time between requests, during which session activity is maintained. Its default value is 1440 - just as many seconds in 24 minutes. It is possible to change the value of session.gc_maxlifetime either by setting the server configuration, or by calling the ini_set () function directly from the program. The call to this function must take place before the call to the session_start () function.

Ok, now everything is clear with this, but I still want to learn more about this unfortunate 1% probability. Is it possible to somehow change this value if the project requires different logic regarding the session data?
Do not rely on the 1% probability if you wish to delete an overdue session for sure. The configuration directive session.gc_probability sets the percentage of the probability of launching the subroutine “delete obsolete sessions” at the beginning of processing a request. To start this process at the beginning of processing each request, the directive value is set to 100. Also, as in the case of session.gc_maxlifetime , the call to ini_set () to change the value of session.gc_probability must occur before the call to the session_start () function.

But, we remember that if you set the probability value to 100, then each request will be searched for outdated sessions, which can greatly affect the speed and bend the server.

In my case, I only need to increase the idle time before deleting these sessions, so that the data I need is not erased. Perhaps, if in your project you need to work more strictly with session data, you will have to find the perfect balance between probability values ​​and downtime.
')
I hope this information will be useful for someone ...

Link to the manual mentioned above: physics.grsu.by/seti/doc/PHP/PHP5/g8_4.html

UPDATE

From myself I would like to add more about the session.gc_divisor directive, which was not considered in this manual. By default, it is equal to 100, and it turns out that the value of session.gc_probability really coincides with% probability. In fact, the probability is calculated as: gc_probability / gc_divisor . Those. at values:
gc_probability = 1
gc_divisor = 1000
The probability is already not 1%, but 0.1% .

It should be noted that most hosting providers in Russia (in order to reduce the load on their servers) put this ratio: 1/1000, which gives a very small chance that after 24 minutes (by default), the data will still be deleted. Take note if you need to configure your application to work harder with these sessions.

Source: https://habr.com/ru/post/13248/


All Articles