Magento 2.2 Performance – Extremely Slow with 100% Processor Usage After Setup:Upgrade

magento2performance

I'm currently running Magento 2.2, php7, Apache2 on an Amazon AWS EC2 c4.large instance, but even the t2.micro instances are usually fine when I'm using it as a development server.

For some reason every once in a while when I run setup:upgrade after updating one of the setup files in one of my custom modules or after installing a third party module my server will become extremely slow, staying at 100% cpu usage whenever I try to load a page, the page loading takes 1 minute+, and will stay at 25% cpu usage when not loading pages.
It only affects the magento website where I called setup:upgrade, pages on other magento installs on the same server will still load at normal speed.

Sometimes the problem is fixed by removing the module I just upgraded, restarting the server and then reinstalling the module without any changes to the code, sometimes a second setup:upgrade fixes the problem, and sometimes it seems the only way I can fix it is by completely reinstalling Magento 2 and the modules.

I have had this occur on Magento 2.1.6, 2.1.8, 2.1.9 and 2.2, all kinds of different combinations of themes and modules that none else seems to have any problems with, on default, developer and production mode.

EDIT: Important note
If you are having this issue and like me are certain you never disabled your caches, there is an acknowledged issue as of right now (Magento 2.3) where running composer update occasionally disables all of your caches. So even if you think your caches are enabled, it's worth to double-check.

Best Answer

TL;DR: Just switch on the config caches.

Longer story:

I've had the same issue and have been playing around a bit.

Steps to reproduce (in developer mode):

  1. cache:disable
  2. setup:upgrade
  3. reload frontend or backend in a browser

When reloading and monitoring with htop, the system 'spams' some PHP processes, totally utilizing all CPUs. This is when I realized that it must depend on some cache settings. And I started to switch off some of them. After switching off the config caches, the problem re-appeared instantly.

After switching off every cache except the config cache, everything runs fast again.