OpenMage / magento-lts

Official OpenMage LTS codebase | Migrate easily from Magento Community Edition in minutes! Download the source code for free or contribute to OpenMage LTS | Security vulnerability patches, bug fixes, performance improvements and more.
https://www.openmage.org
Open Software License 3.0
870 stars 436 forks source link

Lazy Start/Load Sessions #1543

Open joshua-bn opened 3 years ago

joshua-bn commented 3 years ago

Description (*)

The session gets loaded/started in the controller but it might not ever need to be started. A lot of routes, like the category controller, don't have any need for the session. We can't be sure that they won't though, so we can't just use the FLAG_NO_START_SESSION flag to disable them on certain routes.

The block of code found in the following link is what needs to be placed elsewhere. Then, when the singleton is created, it can load that. https://github.com/OpenMage/magento-lts/blob/18cb71ad5b543bb3b83314813415714580ae6a6f/app/code/core/Mage/Core/Controller/Varien/Action.php#L506

Expected behavior (*)

Sessions don't start/load until something tries to read a value from them or write to them.

Benefits

Potentially less session starting because we don't need to open sessions on all pages. This means less locking. Meaning potentially less waterfall issues in loading and thus faster pages.

Potentially less RAM used in the cache. This is especially prevalent for bots that don't go to pages that don't use the session.

Additional information

fballiano commented 3 years ago

isn't it always needed to render the minicart for example? or the "myaccount/login" topbar?

tmotyl commented 3 years ago

There are more cases in default themes of openmage which will trigger session start (like visitor log), but having the session being lazily initialized is a first step to make it possible to optimize even more cases/places.

joshua-bn commented 3 years ago

Yeah. I also use https://github.com/JayBizzle/Crawler-Detect to make sessions read only for bot. Google has said it will start attempting to place orders on sites and I also use the session cookie as a way of knowing if a bot is good or bad (bad bots almost never save cookies). On category pages I don't start the session. Soon I'll be doing that for CMS pages as well. Still not sure if I want to do it for PDP, but probably.

Also it reduces the amount of time the session will be locked for. If I don't need to open the session until 1/2 through the request, that's 1/2 the time it would be locked for.

Also, then I can see in APM where the session is actually getting started more clearly. Then I can start optimizing for that.