magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.54k stars 9.32k forks source link

Database sessions pile up #26308

Closed amenk closed 4 years ago

amenk commented 4 years ago

Preconditions (*)

Magento 2.2.x & 2.3.x & 2.4-develop

Steps to reproduce (*)

  1. Configure store to save session in DB(edit env.php) image
  2. Change Admin Session Lifetime to 60s for example
  3. Flush cache and logout
  4. Login again and check data in DB session table(note the last added data about your session)
  5. Wait 1 minute and click somewhere in admin
  6. Redirect to login page
  7. Login again
  8. Check data again

Expected result (*)

  1. Expired sessions were deleted.

Actual result (*)

  1. Tons of old entries in "sessions" table

Additional information

See also: https://magento.stackexchange.com/questions/300525/is-the-session-table-supposed-to-be-cleaned-up-automatically

Garbage collect is there, but seems to be never called:

https://github.com/magento/magento2/blob/0a1a283cd6a0cf4b98a051867f90150c9490fcec/lib/internal/Magento/Framework/Session/SaveHandler/DbTable.php#L154

shall we make a PR to add this to the core?

m2-assistant[bot] commented 4 years ago

Hi @amenk. Thank you for your report. To help us process this issue please make sure that you provided the following information:

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, please, review the Magento Contributor Assistant documentation.

@amenk do you confirm that you were able to reproduce the issue on vanilla Magento instance following steps to reproduce?


m2-assistant[bot] commented 4 years ago

Hi @engcom-Hotel. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

magento-engcom-team commented 4 years ago

:white_check_mark: Confirmed by @engcom-Hotel Thank you for verifying the issue. Based on the provided information internal tickets MC-30631 were created

Issue Available: @engcom-Hotel, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

amenk commented 4 years ago

@sdzhepa what is the progress of the internal ticket? Can I check that in any way or is it private?

sdzhepa commented 4 years ago

Hello @amenk

I do not see any progress in the internal ticket MC-30631 Jira ticket. Unfortunately, it still in "Ready for Dev" status

ioweb-gr commented 4 years ago

I've noticed that in customer sessions too. I've had two websites in 2.3.1 and the sessions table reached 2.5m entries with none of them being cleared even though they were long past their expiration date.

After running a cleanup sql command to delete all sessions that the expiry timestamp had passed only 27 sessions were left. I think this is really important because the size of the DB increases really fast in popular eshops.

amenk commented 4 years ago

@ioweb-gr which cleanup command did you use? And how long did it run?

amenk commented 4 years ago

Also each and every crawling attempt seems to open a new session - is that normal?

ioweb-gr commented 4 years ago

@amenk I think it was pretty straightforward.

I went to https://www.unixtimestamp.com/index.php to get the timestamp for the date before which I wanted to clear the sessions. For example anything that should have expired 15 days ago e.g. 15/5/2020 = 1589500800

I then executed

DELETE FROM `session` where `session_expires` < 1589500800;

But yeah they do pile up really fast. I might have to create a cron job for this at some point.

mischabraam commented 4 years ago

https://github.com/weprovide/magento2-module-sessioncleaner This might help.

amenk commented 4 years ago

@mischabraam Thank you the module :)

I just realized that it might be an option to store the sessions in Redis as well ...

ioweb-gr commented 4 years ago

Was this fixed? What's the relevant pull request?

ihor-sviziev commented 4 years ago

@sidolov @sdzhepa @gabrieldagama was this issue fixed?

gabrieldagama commented 4 years ago

@ihor-sviziev @ioweb-gr The issue was fixed by internal pull request and it is already merged, please let us know if you still face the issue on 2.4-develop branch.

Thanks.

ihor-sviziev commented 4 years ago

Ok, Seems like here is related commits: https://github.com/magento/magento2/commit/aa034d06859b1bff72121d98d094ec0944084140 https://github.com/magento/magento2/commit/d2d6c607bece5c137b1a5a8119974fc24571e6e3

amenk commented 4 years ago

So this will be in 2.4.2, right?

gabrieldagama commented 4 years ago

Correct @amenk

sdzhepa commented 4 years ago

Fixed and delivered in https://github.com/magento/magento2/search?q=MC-30631&type=commits

amenk commented 2 years ago

Still happens on the latest Magento version, maybe we just switch to Redis...

ioweb-gr commented 2 years ago

Redis has it's own set of issues when heavy traffic on the site

hostep commented 2 years ago

@amenk: did you read the new documentation for the fix @sdzhepa mentioned before?

amenk commented 1 year ago

@hostep Good point. As mentioned in the docs, Ubuntu uses a gc_probability of 0 which was the case for us, so it did not work. We did a short test on staging now 1 / 1 / 10 seconds and sessions are deleted.

So this seems to work now I believe.