đź“… Updated: May 8, 2024 (Originally published: February 27, 2016)
Magento 2 is good and great, but it is a very large framework with many files. If installed on a misconfigured server, Magento 2 performance can lead to a store that your customer will leave immediately.
Let’s review best practices and software required to run Magento 2 with excellent performance.
1. Use latest PHP 7
Magento 2 is 100% compatible with the latest versions of PHP 7, which is all about performance. PHP 7 performs twice as fast compared to previous versions.
We are really excited that Magento 2 supports Varnish out of the box. You absolutely must use it if you want lightning fast performance. Here is our fine tuned Varnish VCL for Magento 2.
Make sure to set your TTL setting to highest possible, e.g. 2 weeks.
3. Use HTTP2 and its Server Push
Magento 2 doesn’t support HTTP 2 server push but you can make it. This should be coupled with a server side solution (Varnish VCL or nginx) to track down repeat visitors in order to avoid unnecessary pushes.
4. Build your own CDN
Cloudflare provides simply inconsistent results. You may be getting as much as 600ms for a cached asset in Cloudflare, tested from the same location (e.g. LAX). That is instead of expected max of ~100ms.
So if you’re after the consistent performance for your user and you know particular locations they mostly originate from, it’s time to setup your own CDN. The DYI CDN solutions can be found online:
BIND can be configured for GeoIP just fine without any patching. All the articles out there are outdated. If you search well you will find a good reference to configuring GeoDNS in BIND: example1, example2. However, BIND does not support newer .mmdb GeoIP format (yet) and the free .dat format is no longer available for free autoupdates.
PowerDNS software has got GeoIP backend, with support for newer .mmdb file format. However, this its documentation is a bit buggy – .mmdb support start at version 4.2 which is yet to be released and distributed.
ClouDNS provides free GeoDNS trial for their service, so this can be used as alternative to setting up BIND with GeoIP.
You can still use Full page cache Redis cache even with Varnish configured. This is an additional caching layer, but it never hurts to have more.
What’s great is that the new Magento supports full page cache in the Community edition for free! There is no longer a need for a third party full page cache plugin.
Get additional performance gain by storing sessions in RAM. Due to some incompatibility in configuring session store to Redis, we recommend to use Memcached for storing Magento 2 sessions.
By default Magento 2 search is slow. No need to worry, as you can leverage the open-source Sphinx search engine which will not only index your products and provide very accurate results. It will also increase the speed of searches a lot! We recommend Advanced Sphinx Search Pro for Magento 2 for implementing Sphinx support in your Magento 2.
The default M2 installation uses very slow AJAX-ified image loading. For performance reasons, we recommend using Magic Zoom Plus plugin. Just check their demo to see how faster it is! And it works without Javascript.
On a large store, you can fix URL issues as well as “warm-up” the URL cache after importing a large set of products, by using this plugin.
So if you have a problem with messed up URLs (products displaying wrong URLs), run:
DELETE FROM catalog_product_entity_varchar where attribute_id =(SELECT attribute_id FROM eav_attribute WHERE attribute_code LIKE 'url_key' and entity_type_id=4 );
Then run URL regeneration.
11. Fix sessions
Magento 2 with Varnish has a bug resulting in dozens, hundreds, or thousands of extraneous session files.
Until this is resolved, you may want to set up a cron to clean those unneeded session files. Otherwise, they will contribute to a slower session start.
Magento default cron tasks implementation is errr.. bad. You can use an alternative cron module here
14. Enable DNS cache
Magento 2 makes use of some external APIs and sure enough, this results in many DNS lookups produced by it. You can speed up your checkout experience, and squeeze out more performance from it, by enabling DNS cache. Here is how to enable DNS cache on your server.
15. Stay away from bad themes and modules
Be sure to stay away from the Argento theme or at least disable SEO modules that it comes with. There is a serious issue.
Using Flat Catalog instead of managing catalog data in many tables by the EAV will help you to speed up your product collection.
In order to enable Flat Catalog, navigate to Stores ➡ Settings ➡ Configuration in the Admin panel. Then select Catalog on the left panel under the Catalog tab. Expand the Storefront section and scroll down to Use Flat Catalog Category and Use Flat Catalog Product. Select “Yes” in both sections.
Session locking is a problem, especially with AJAX requests, which do not even need locks.
Resolve session bottleneck in Magento AJAX Requests by using a special module.
Don’t do upgrades, fixes, and other things at a random time. If you are not experiencing serious issues, or simply want to do a maintenance upgrade, do it on a rare schedule. E.g. updating once a month, on the 7th day, is a fine schedule.
The purpose of reducing maintenance windows serves well both the customers and performance. A maintenance results in having to clear cache completely. It takes many days, and for some store weeks to fully warm up / refill the cache. An efficient cache is the one that is not cleared often!
20. Clean up unused media
Over time, the media file directory can grow huge. To clean it up, use EAVCleaner.
Furthermore, the image cache (thumbnails), grows over time with unused image’s thumbnails. Do image cache clearing around once in 1 year:
n98-magerun media:cache:image:clear
# flush cache especially FPC, so that Magento can dynamically recreate thumbnails upon access
n98-magerun cache:flush
# prewarm cache for your visitor's convenience and faster pages
php bin/magento catalog:image:resize
21. One Step Checkout module
Traditional checkout processes often require the customer to navigate through multiple steps (billing, shipping, payment method, order review, etc.). A one-step checkout simplifies this into a single page, reducing the time it takes for a customer to complete a purchase. This can lead to a faster overall checkout experience, which is critical for reducing cart abandonment rates.