Home » Blog » Magento 2 performance – the definitive guide

Magento 2 performance – the definitive guide

Magento 2 performance as it is, out of the box, is not really that bad. Especially if we’ve taken some basic steps – that are outlined in this article as well – and if we have a decent hosting environment.

But we can do better. So we’re going to discuss a few basic and some advanced tips when it comes to improving Magento 2 performance, that I hope will help you get something more out of your Magento 2 store.

This is going to be a long read, so grab your snacks.

Basic stuff regarding Magento 2 performance

1.Use proper hosting, backed by up to date softwaremagento 2 php 7.1

You can’t blame Magento if you’re trying to run in on a cheap shared hosting plan. It’s just not made for that. You can probably get away with a small WooCommerce shop on some decent shared server if hosting costs are such a big deal to your business. But hosting is not the place to be cheap.

So, I’d aim to have a server that checks the following marks:

  • Nginx;
  • HTTP/2;
  • PHP 7.1.x (php-fpm);
  • MySQL 5.7;
  • > 1 GB max_memory;
  • > 3GB system RAM;
  • valid SSL certificate (Let’s Encrypt or a paid one);
  • REDIS (optional but recommended);

I must say that Magento 2 works on PHP 7.0.13–7.0.x but PHP 7.1 is just faster – check the graph from Kinsta’s tests – and it’s officially supported on the latest Magento 2 versions. So why not use it?

magento 2 performance with php 7

2.Keep Magento 2 up-to-date

New Magento 2 versions generally bring bug fixes, new features and different adjustments that might improve the store. But looking at some change logs from different versions, we can see that sometimes we can get some performance wins too. So why stick to an older version?

Just take a look at the latest Magento 2 version – 2.2.6, released 18 September 2018:

  •  Category product indexer logic has been optimized, and re-indexing time has decreased up to 98%, from 40 minutes to one minute for 100,000 categories. Previously, when your store contained many categories (100,0000), Magento could take up to 40 minutes to re-index product catalogs.
  • The catalog rule re-indexing operation has been optimized, and the average re-indexing time (which depends on rule conditions) has improved by more than 80%. Previously, a full catalog rule re-index operation on a medium B2C store took more than 20 minutes.
  • The time required to load a store’s home page has been reduced noticeably when the top menu contains many categories. (Load time is still affected by the number of categories and the structure of the top menu.).

And yes, you don’t see things like this in every change log and new release. But just avoiding nasty/upsetting bugs and improving security should be enough of a reason to keep your software up to date.

Quick Magento 2 performance wins

These are things that you can probably just set yourself directly from the administrative panel or with basic SSH usage.

3. Disable unused extensions

It’s a great idea to run a 3rd party extension audit. By 3rd party I’m referring to extensions that don’t belong to the original Magento installation.

Even if you don’t use some long-forgotten extension, it’s code is still loaded. And if you’re lucky and it was a high quality extension, it will just affect the back-end (the administrative panel). But isn’t it enough of a reason to get rid of it?

Quick and painless way to get a speed boost by just being organised.

In order to turn off an extension, you’ll need SSH access, and once logged in you must perform the following commands:

To disable VendorName_PluginName plugin:

php bin/magento module:disable VendorName_PluginName

To turn it back on run:

php bin/magento module:enable VendorName_PluginName

To see what modules were disabled or enabled run this command:

php bin/magento module:status

4. Check for poor-performing extensions

Not every piece of code is quality code. If it works, it doesn’t mean it’s fast. Code can be written poorly, using bad practices or whatever.

If you lack the technical know-how or Magento debugging skills/tools, you can still use the exclusion method. If your store is sluggish while having a certain extension installed, and without it the store is lightning fast you can look into an alternative – it may just be the extension is poorly made.

But make sure that it is the extension’s fault and not a server issue. (under-powered hardware, etc.)

5. Enable the cachemagento 2 cache

Magento 2 comes bundled with a very good FPC cache, unlike Magento 1, where you needed to buy/make a separate extension.

Make sure to enable all the caches available to Magento 2. You can check and do this straight from the admin panel at: System->Cache Management->Select all-> Enable-> Submit

This is a must-do if you want to get a huge boost regarding Magento 2 performance. It’s so basic it shouldn’t even be mentioned. Just enable them.

6. Switch to production mode

Magento 2 comes with several ‘states’ depending on the scope of the installation. So we have the default mode, developer mode, and production mode.

As its name suggests, production mode is for… production. For a live store that is expected to sell products.

Switching to production mode improves store responsiveness and reduces the initial page load times.

To switch to production mode, you must enter the following command in SSH:

bin/magento deploy:mode:set production

7. Enable CSS and JS files minification

Another thing were Magento 2 outshines Magento 1. If in the past you needed to ‘manually’ do stuff like CSS/JS file minification or get a dedicated extension, now it’s all just about two switches in the admin panel.

How great is that?

Just turn them to Yes, at the menu Stores > Configuration > Advanced > Developer 

Why would you do this? This process – minification – makes files smaller by removing white space and unnecessary things from CSS and JS files. This means less data to download for your store’s visitors, and that translates into faster page loads.

css and js performance magento 28. Never use JS bundling or CSS bundling

This setting unites several smaller files into one big file. In order to reduce the number of requests, the browser makes to the server when visiting a page.

It sounds great, except that it isn’t.

Why?

1.We now have HTTP/2. This means that your web server supports multiplexing (multiple requests allowed at the same time, on the same connection – contrary to HTTP/1.1) – among other cool HTTP/2 features.

This means that multiple resource loading is not only not a problem anymore. It’s faster.

2.You’ll get huge files (5Mb or more depending on your extensions, theme etc.) that can negatively impact page load time.

3. What if a JS file that is used only on a page (let’s say, a fancy code for you Google Maps integration on the contact page) is bundled with other files? People will download/get that code even if it isn’t necessary to display the page they’re actually visiting.

Double check for JS bundling at the following menu: Stores > Configuration > Advanced > Developer

9. Enable Flat Catalog

This Flat Catalog setting will help you speed up the product collection. Basically, instead of managing catalog data in many tables by the Entity Attribute Value (EAV), things get generated on the fly and more efficiently.

In certain conditions, we’ve seen this setting speed up catalog processing and cart price rules. Pretty neat for just a couple of switches!

How to enable Flat Catalog in Magento 2:

  1. In the admin panel, go to Stores > Settings > Configurations
  2. Left panel: under Catalog, select Catalog
  3. Open the Storefront section and chose YES for Use Flat Catalog Category and Use Flat Catalog Product fields.
  4. Save
  5. Flush the caches.

10. Enable asynchronous e-mail sending for order confirmation emails

If certain emails can be sent upon a cronjob task running, emails regarding orders are a different type of animal.

You want your customers to instantly receive emails regarding their placed orders. If someone placed an order, they need to be informed now that it has been placed.

Enter Asynchronous sending for sales emails. This allows those emails regarding sales, be sent instantly. Of course, processing them later would remove some extra server load from the place order request process. But the performance also means sales performance.

You can enable this setting in the Admin panel at: Stores > Configuration > Sales > Sales Emails > General Settings. Just switch Asynchronous sending to Enable.

11. Enable asynchronous indexing for re-indexing sales_order_grid table

Enabling asynchronous indexing for the sales_order_grid table can improve performance on stores that receive a lot of orders.

You can set this to Enabled in the admin panel at: Stores > Configuration > Advanced > Developer > General Settings 

12. Optimize images

This one is easy. You don’t need 42 Megapixel images for your products. You need web-sized images, that are sharp and provide valuable info and details about the products.

The same goes for the store banners (on sliders or whatever).

image optimization magento 2 performance
Thanks Mr. panda!

Bigger images mean bigger in size. Lots of high resolution images means more data for your customers to download in order to actually see them or the page.

And take into account that a lot of your traffic probably comes from smartphones – which not only means that the device is slower, but the internet connection speed is slower… see 3G speeds.

Instead, try to properly size images and banners and then optimize them with automated tools/extensions or using free tools like Tinypng.com

When banners, products and sliders start to pile up, things like image optimization can make a dramatic difference in page load times.

13. There are some good performance oriented extensions out there

Even if you no longer need to buy a FPC cache extension, there are still things where you can make some good investments.

A couple of extensions worth looking at:

  • Full Page Cache Warmer by Mirasvit;
  • Improved Asynchronous Re-indexing by Mirasvit;
  • Performance Optimization Tool by Potato Commerce;

note: we’re not affiliated with any of the above extension vendors or their companies. We used them, our clients are using/have used them, and we just think they are ok.

14. Lots of traffic? Go for a CDN

A CDN or Content Delivery Network can be amazing if you have lots of traffic and things start getting really expensive in terms of hosting costs.

What role does a CDN have? Basically, a CDN works by providing additional server nodes for users to download resources – most of the time stuff like images and JavaScript files.

Possible advantages of using a CDN:cdn cloudflare for magento

  • better speed and lower latency – a node of your CDN can be closer to your user than your actual server, therefore the content the CDN provides is downloaded faster;
  • lowers your server’s usage;
  • can increase reliability;
  • most of them have some form of DDoS protection;
  • decent costs when compared to alternative solutions;

I strongly recommend you to go only for a paid CDN plan. Cloudflare and KeyCDN are pretty good CDN providers. Plans usually start from as low as 20$/month.

Advanced Magento 2 performance tuning

These tips require some technical know-how. Ask your developer if you are unsure of how or what to mess about with. Most of them can cause instability or other problems. Some of them bring more performance improvement, some less. But as performance is something that impacts sales directly, we need to strive for the best we can.

15. Basic stuff to cover – GZIP, browser caching and other stuff

These things help improve Magento 2 performance quite a bit, and most performance scoring websites/tools like Google PageSpeed Insights look for these too.

Depending on your web server – Apache or Nginx – how you set these up may vary. But just by googling most of them you can find all you need to know in under 20 minutes.

But the internet is full of tutorials for these things so I’ll just mention them:

  • make sure you have GZIP compression enabled;
  • leverage browser caching (just google this, it’s great!);
  • defer parsing of JavaScript – before the browser can render the actual page, it needs to parse JS code. Some scripts can be delayed in order to speed this up;
  • specify image dimensions (width and height for each image can be defined in order to speed up browser rendering);
  • enable Keep-Alive;
  • serve scaled images (don’t make the browser resize a 1000px X 1000px to 100px X 100px, but rather provide the properly sized image from the start);
  • try to use rel=”preload” for valuable external resources (more info here);
  • add rel=”noopener” to external links as a performance and security measure;

16. Use PHP OPcache and tune it

PHP OPcache improves PHP performance quite a lot by storing precompiled scripts in your shared memory. This eliminates the need for PHP to load and parse scripts on each request.

And guess what? Magento is a PHP application.

Enabling OPcache is a must even if you have a small server that is quite low on memory. Just set the opcache.memory_consumption to a conservative value such as 64 or 128 (it refers to max Mb used by OPcache).

Magento also has some good tips on what settings to use:

  • opcache.enable_cli=1
  • opcache.memory_consumption=512
  • opcache.max_accelerated_files=100000
  • opcache.validate_timestamps=0
  • opcache.consistency_checks=0

This feature can have a dramatic effect on your TTFB.

17. Other PHP settings worth looking at

These settings can be considered as… micro-optimizations but hey! Every millisecond counts!

Some stuff worth looking at:

  • memory_limit – Magento recommends > 1Gb. I like 2 Gb for heavily customized stores;
  • realpath_cache_size – as PHP states, for applications where PHP opens many files, increasing this setting can bring PHP performance improvements. And Magento qualifies. Test before setting this blindly. I found out that on most stores values from 128k to 150k is pretty good. If you are low on memory stick to 32k or 64k;
  • realpath_cache_ttl – how long does the above cache persist. Usually setting this to 3600 or 7200 is pretty ok. You need to test after messing around with these settings;
  • max_execution_time and max_input_time – you might want to make sure these are set at about 90;

18. Let’s work on those MySQL settings

But before adjusting, you need to have the server running for a while and then use something like mysqltuner.pl. We usually set the my.cnf parameters individually per machine, because there are a lot of configurations out there.

Just make sure to give MySQL enough RAM, this is not really a place to be conservative.

19. Use Lazy Loading

When you usually open a web page, the entire page is downloaded and then rendered. If the user never looks at the whole page pointless resources are downloaded. And even if he does, he has to wait for that image in the footer to load first.

Lazy loading makes sure to download only the resources that are currently in the view port – what the user can initially see upon entering a page. Needless to say, this improves page loading times quite a bit.

Lazy loading can both improve performance but reduce server load as well. And usually, it just leads to a better experience on the store when done right.

So make sure your Magento 2 store uses lazy loading. Most decent themes do.

20. Mind that theme. Check for bad code

We’ve seen quite a bunch of poorly coded Magento themes. Sometimes the theme developers fix problems, sometimes they don’t. While not that easy to spot problems, we had a particular case where the top menu was pretty quirky and causing a huge waste of resources.

Even if it can prove to be quite a time consuming, this step is worth checking out as it can sometimes save a lot of frustrations.

21. Using a premium theme with 1000 features? Get rid of what you don’t use

Some themes have a lot of features. And that’s great! But if you don’t use some of them or the theme bundles them weirdly, you might load a lot of unnecessary code.

This process can be tedious and boring. But on some themes we managed to save quite a bit of CSS/JS code. (even up to 230kb of unused code!)

It’s a good practice to load only used code. Less code = better Magento 2 performance.

22. Go for HSTS with the preload directive

HSTS comes for HTTP Strict Transport Security. It’s a web security policy mechanism that helps to protect websites against threats like protocol downgrade attacks and cookie hijackings.

Protocol downgrade attacks and Cookie hijacking are the primary types of man-in-the-middle attacks. So… you might say that this is a must-have if you care about your security.

It allows web servers to declare that browsers should interact with them only using HTTPS connections and never via the insecure HTTP protocol.

Setting this to something like max-age=63072000; includeSubDomains; preload will validate your web server as HSTS compliant.

You can check if this header is properly set up along with other useful security headers at securityheaders.com.

Headers worth setting: X-XSS-Protection, X-Content-Type-Options and X-Frame-Options.

Don’t neglect security.

And yes, HSTS can improve performance. With HSTS browsers don’t make a HTTP request and then get redirected to the HTTPS version of the page. They make a direct connection to the HTTPS version of your store.qualys ssl test

Bonus tip: you can submit your store’s address to the Chrome HSTS preload list. This can help improve performance as most major browsers also have the preload lists based on the Chrome list. You can do this here. You can also do a free scan using Qualys SSL Labs over here. Quite a lot of useful information about your web server’s SSL setup.

23. You actually need a cache warmer or cache primer

Yes, Magento 2 does come with a FPC. But in order for a page to get cached, it must be visited. This means that after the FPC cache is cleared, the first person to visit that page will not get a great experience.

Enter the cache warmer. This guy practically preloads pages into the FPC cache. So cool. There are various tools and extensions for this, so take your pick.

I must say that along with a guaranteed fast loading time, you might get a SEO boost as well – because if the Google crawlers hit your cached pages… they also get the page content faster. Win.

24. Improve the logic behind the layout, go hard on CSS/JS refactoring

This is a bit more complicated.

What I mean with improving the logic behind the layout: you might have to many pointless HTML elements. Code may be written by inexperienced people or just… in a rush.

This can hurt your performance. And even if going as heavy as customizing a footer for example, to have fewer HTML elements might take some time and effort, it might be worth it. Especially if you have a traffic heavy store.

CSS/JS refactoring refers to improving the way the code is written. Duplicate rules like declaring a color in a thousand places, over-complicated CSS selectors and pointless rewrites can hurt performance. And yes, bad selectors, when abused, can really hurt performance.

Using expensive properties or universal selectors carelessly can really take a toll on the browser. And just imagine what abusing simple stuff like this do to a slow smartphone user – a low to mid-range user that deserves to have a decent experience on your store.

Conclusions – Magento 2 Performance

A lot of these things apply to more than just Magento 2. Some work very well when thinking about a Magento 1 store or a WooCommerce store. But it’s important not to overlook them, as we want to sell. And in order to sell, we need a fast store.

If these tips and ideas helped you improve your store, give it a share! Other Magento store owners might get some ideas about getting more out of this awesome eCommerce platform.

If you need experienced help in optimizing your Magento 2 performance in order to squeeze the absolute best loading times from your store, feel free to contact us. We can help you get the speed you need.