5 Ways to Optimize Magento 2 Performance

From time to time we have guest posts on the blog. Today’s post is from Konstantin Gerasimov at goivvy.com.

Magento 2 comes as the new era of eCommerce. The updated backend, the latest code patterns and software libraries – these are just some of the improvements introduced in version 2 of the popular platform.

The question I hear often: is it faster than Magento 1? The answer might not be so obvious. Even though Full Page Cache is now included in Community Edition there are still slow M2 complaints floating around on the internet.

In this article I will share my experience with optimizing Magento 2 performance. The tips below are field proven and helped me a lot in fixing slow M2 sites.

5 tips to speed up Magento 2

  1. Inspect every custom plugin installed.
  2. Audit TTFB with a profiler.
  3. Try better hosting plan.
  4. Make sure you are in production mode.
  5. Merge, bundle, minify JS and CSS.

3rd party plugin audit

The Magento 2 core is written by programming experts. There is little to no room for improvement in this area. On the other hand various custom plugins you buy in most cases are poorly coded.

In my experience the higher the number of 3rd party extensions installed the slower the Magento 2 site is. That said, the first thing you should do in a struggle with a slow M2 store is to perform a plugin audit.

How would you do that? Easy: deactivate extensions one by one and benchmark site performance. How to deactivate? Just delete extension’s folder from app/code and run these commands:

php bin/magento setup:upgrade
php bin/magento deploy:mode:set production

How to benchmark? I personally use JMeter tool.

 

Jmeter

(click image to enlarge)

It might take some time to install and setup. Once it is running it is pretty ease to generate, save and compare test results.

Once you identify an extension that slows Magento 2 down, contact its vendor. Notify them of a problem and ask for a patch. Or find an alternative plugin.

When you add new extensions to your store – always benchmark. When you modify existing code in any way – always benchmark. You never know what performance damage a simple code change could cause.

TTFB profiler audit

TTFB or time to first byte is a metric that shows how long a browser waits for a first byte from a site’s server. Good TTFB is around 0.5s. In fact that is the time to the first byte of a fresh copy of Magento 2.

magento-2-fresh-copy

(click image to enlarge)

One of the sites to measure TTFB ( and other speed metrics ) is webpagetest.org.

Time to first byte includes download time, network latency time and server response time. Server response time is how long Magento generates HTML. If it is greater than 0.5s it is time to do a TTFB audit.

What do you need for that? You need a profiler. Magento 2 has one already built-in. To enable it set the MAGE_PROFILER server variable to html. You can also do that in the first lines of index.php:

$_SERVER[‘MAGE_PROFILER’] = ‘html’;

Then clear cache and reload any frontend page. Scroll to the bottom and inspect the profiler trace.

(click image to enlarge)

(click image to enlarge)

Find function calls and template files with the most time spent and inspect them. Look for unoptimized php code like loading a model in a loop:

<?php foreach($category->getProductCollection() as $product): ?>

<?php $mode = $productModel->load($product->getId()); ?>

<?php endforeach; ?>

You will be surprised but I see the ‘model-in-loop’ performance hog way too often in clients sites.

Try better hosting plans

Magento 2 requires more CPU power and memory resources than Magento 1. If you struggle with a slow M2 site, the reason might be your hosting plan.

Sign up for a better server. Try more CPU cores, more RAM. Benchmark. If site speed difference between old and new plans is significant – there is something wrong with your existing hosting. Ask your system administrator to double check the server configuration. If you hardly notice the difference – stay where you are.

Put Magento 2 in production mode

Magento 2 has different running modes: default, developer and production. Production is the fastest one.

To find out in what mode you are now issue this command in magento main folder:

php bin/magento deploy:mode:show

To change to production run:

php bin/magento deploy:mode:set production

Merge/bundle/minify CSS and Javascript

Magento 2 can minify, merge and bundle CSS and Javascript. Should I say it is good for performance?

Take advantage of those options. Go to site backend Stores > Configuration > Developer > Javascript Settings and CSS Settings and enable merge/minify/bundle:

(click image to enlarge)

(click image to enlarge)

Please note that minification only works in production mode. But you have already set it in the previous section, haven’t you?

Summary

Magento 2 site speed directly depends on how many 3rd party extensions you installed. A clean fresh copy of M2 runs pretty fast. Thus you need to keep the number of custom plugins to a minimum.

Do performance benchmarks every time you make a change to a site’s codebase. Sign up for Magento optimized hosting because hosting is the foundation for any speedy site.

About the author:

Konstantin Gerasimov is a Magento Certified Developer. He has been working with Magento for over 5 years. His main interests are extension development, performance optimization and backend improvements.

Looking for a web host that understands ecommerce and business hosting?
Check us out today!

Leave a Reply