11 Easy Steps to Optimize Your Magento 2 Performance Quickly

Optimizing Magento 2 PerformanceOptimizing the speed of Magento2 application is very important for several reasons, most of all speed allows your customers to have the best user experience possible while shopping in your eCommerce store.

Magento2 application comes with its different architecture. Saying this, it has many activities which run in the background. These activities may impact the speed issue if we didn’t take the right step and right time.

1) Enable Flat Categories and Catalog:

Maybe, you know that Magento provides a feature to get data from the database in an optimized way. When we are dealing with large catalog and category data at that time it is required to know about this feature and enable that feature to get data faster. This feature is used to copy all the EAV table data into the Flat table means a single table and use those data for the frontend store. In simple terms if we get data from the single table compare to multiple tables, it fetches data faster.

Go to the backend, Stores > Configuration > Catalog > Catalog > Storefront, select Yes in Use Flat Catalog Category field and run the reindex command.

2) Merge CSS and JS Files

This feature used to make the web page as light as possible for the fast loading. To make this feature to be working, please put Magento 2 into production mode.

./bin/magento deploy:mode:set production

Javascript File

Go to backend & follow, Stores > Configuration > Advanced > Developer > JavaScript Settings

Set Merge JavaScript Files to Yes
Set Minify JavaScript Files to Yes
CSS File

Go to backend and follow, Stores > Configuration > Advanced > Developer > CSS Settings

Set Merge CSS Files to Yes
Set Minify CSS Files to Yes

3) Content Delivery Network

Content Delivery Network (CDN) is a special system that can connect all cache servers. In addition to supported geographical proximity, CDN will take over delivering web content and faster the page loading.

Please follow this guide to set up CDN for Magento 2 stores. Go to backend & follow, Stores > Configuration > General > Web > Base URLs (Secure)

4) Caching

Magento default provides two types of caching default FPC and Varnish. Cach is a very important feature of Magento to load the site faster. Your site’s response time is quite good with approximately 0.5s. But it is not an ideal time as Google expects. Magento 2 supports Full Page Cache via Varnish natively due to that. You can set up Varnish to get the first byte around 0.1-0.2s using the following configuration.

Go to the backend, Stores > Configuration > Advanced > System > Full Page Cache, transfer Caching Application to Varnish Cache.

Go to backend menu System > Cache Management & check the settings. It should be looking link green as enabled.

5) Image Optimization

Image optimization is a necessary factor to load product/store pages fast. You can use third-party tools or SSH command to optimize images. For SSH command, you need to follow the steps mentioned here https://www.tecmint.com/optimize-and-compress-jpeg-or-png-batch-images-linux-commandline/. They will help you to optimize images using commands.

Please review below third party tools to optimize the images:

1) https://www.mageplaza.com/review/magento-image-optimizer-apptrian.html
2) https://commercemarketplace.adobe.com/jajuma-module-webpimages.html

6) Enable Compression

You can also enable compression using .htaccess file.

mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*

ExpiresActive On
ExpiresByType image/jpg “access plus 1 year”
ExpiresByType image/jpeg “access plus 1 year”
ExpiresByType image/gif “access plus 1 year”
ExpiresByType image/png “access plus 1 year”
ExpiresByType text/css “access plus 1 month”
ExpiresByType application/pdf “access plus 1 month”
ExpiresByType text/x-javascript “access plus 1 month”
ExpiresByType application/x-shockwave-flash “access plus 1 month”
ExpiresByType image/x-icon “access plus 1 year”
ExpiresDefault “access plus 2 days”

#ExpiresDefault “access plus 1 year”
ExpiresByType text/html A0
ExpiresByType text/plain A0

# Compress HTML, CSS, JavaScript, Text, XML & fonts
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml

# Remove browser bugs (needed only for old browsers)
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent

FileETag none

7) Magento Updates

Remember to update your Magento 2 store.

8) Profiler and Audit TTFB

It is a default feature of Magento where you can track files which are taking more time to load the page. It is also used to improve the first-byte time.

You can enable it via htaccess file:
Open .htaccess file, and add the following line (usually at the beginning):
etEnv MAGE_PROFILER “html”

You can also enable it via index.php
$_SERVER[‘MAGE_PROFILER’]=1;

9) Enable production mode

Production mode is used for deployment on a production server. Exceptions are not showing to the user, exceptions are displaying in the logs only, and static view files are served from “pub/static” only. Production mode has better performance because static view files are generating in the pub/static folders and because of code compilation.

You can enable production mode using below command. You can execute this command on the ssh
php bin/Magento deploy:mode:set production

10) Enable Redis

Redis is an open-source tool and it is an important tool to improve site frontend and backend performance. It is used for cache/store data. It works like Memcached, which stores cache files in memory. So, it is extremely fast.

To enable Redis, you need to ask your server support team to install, configure and enable Redis on the server. Also, you need to connect Redis with Magento 2 store via adding Redis server configuration in the app/etc/env.php file.

Note: You need to ask the server support team to provide Redis server configuration detail to set in the env.php file.

We can use Redis cache storage in Magento 2:
‘cache’ =>
array (
‘frontend’ =>
array (
‘default’ =>
array (
‘backend’ => ‘Cm_Cache_Backend_Redis’,
‘backend_options’ =>
array (
‘server’ => ‘127.0.0.1’,
‘port’ => ‘6379’,
‘persistent’ => 0,
‘database’ => ‘0’,
‘force_standalone’ => ‘0’,
‘connect_retries’ => ’10’,
‘read_timeout’ => ’30’,
‘automatic_cleaning_factor’ => ‘0’,
‘compress_data’ => ‘1’,
‘compress_tags’ => ‘1’,
‘compress_threshold’ => ‘20480’,
‘compression_lib’ => ‘gzip’,
),
),
),
),
We can use Redis cache storage in Magento 2:

‘session’ =>
array (
‘save’ => ‘redis’,
‘redis’ =>
array (
‘host’ => ‘127.0.0.1’,
‘port’ => ‘6379’,
‘password’ => ”,
‘timeout’ => ‘5’,
‘persistent_identifier’ => ”,
‘database’ => ‘1’,
‘compression_threshold’ => ‘2048’,
‘compression_library’ => ‘gzip’,
‘log_level’ => ‘1’,
‘max_concurrency’ => ‘6’,
‘break_after_frontend’ => ‘5’,
‘break_after_adminhtml’ => ’30’,
‘first_lifetime’ => ‘600’,
‘bot_first_lifetime’ => ’60’,
‘bot_lifetime’ => ‘7200’,
‘disable_locking’ => ‘0’,
‘min_lifetime’ => ’60’,
‘max_lifetime’ => ‘2592000’
)
)

11) Remove unwanted code and modules:

It is very important to remove unwanted code and module from the Magento 2 because the unwanted module will be used somewhere in the background.

We hope the above suggestions help you to optimize the Magento 2 store. Still, if you are not happy with the performance, you can use below third party extensions which helps you to speed up your store.

https://amasty.com/google-page-speed-optimizer-for-magento-2.html
https://swissuplabs.com/page-speed-magento-2.html
https://amasty.com/full-page-cache-warmer-for-magento-2.html