As an e-commerce platform, Magento 2 is expected to handle a large number of requests and process them efficiently. However, if not optimized properly, a Magento 2 shop can experience performance bottlenecks that can lead to slow page loads and a poor user experience. In this article, we will discuss some common culprits of performance bottlenecks in Magento 2 and provide code examples on how to optimize them.

1. Unnecessary Database Queries

One of the main causes of performance bottlenecks in Magento 2 is the execution of unnecessary database queries. These queries can be caused by a variety of factors, such as inefficient use of the Entity-Attribute-Value (EAV) model or the loading of too much data in a single request.

To optimize database queries in Magento 2, you can use the following techniques:

  • Use lazy loading to only load the data that is needed at the moment. This can be done by using the addFieldToFilter and addAttributeToSelect methods to specify only the data that you need to load.
Example:

php
$products = $productCollection->addFieldToFilter('status', 1)
    ->addAttributeToSelect('*')
    ->load();


Use indexers to improve the performance of database queries. Indexers are used to pre-calculate data and store it in a separate table, so it can be retrieved more quickly. Example:

php
$productIndexer = $this->indexerRegistry->get('catalog_product_price');
$productIndexer->reindexList([1, 2, 3]);

Use caching to store the results of frequently used database queries. This can significantly improve the performance of your shop by reducing the number of database queries that need to be executed. Example:

php
$cacheKey = 'my_cache_key';
if ($this->cache->load($cacheKey)) {
    $data = $this->cache->load($cacheKey);
} else {
    $data = $this->getDataFromDb();
    $this->cache->save($data, $cacheKey);
}


2. Large Category Pages

Another common performance bottleneck in Magento 2 is the loading of large category pages. These pages can be slow to load due to the large number of products that need to be displayed and the amount of data that needs to be loaded for each product.

To optimize the performance of large category pages, you can use the following techniques:

  • Use lazy loading to only load the data that is needed for the current page. This can be done by using the setCurPage and setPageSize methods to specify the current page and the number of products to be displayed per page.
Example:

php
$productCollection = $this->productCollectionFactory->create();
$productCollection->addAttributeToSelect('*');
$productCollection->setCurPage($currentPage);
$productCollection->setPageSize($pageSize);
$productCollection->load();


Use the flat catalog to improve the performance of category pages. The flat catalog is a special database table that stores product data in a denormalized form, making it easier and faster to retrieve.

3. Slow Website Performance

A slow website can be caused by a variety of factors, such as inefficient code, large images, and unoptimized database queries. To improve the performance of your website, you can use the following techniques:

  • Use a content delivery network (CDN) to distribute static content, such as images, CSS, and JavaScript files, to servers closer to the user's location. This can significantly reduce the load time of your website by reducing the distance that the content needs to travel.
  • Optimize images by reducing their size and using appropriate image formats. Large images can significantly increase the load time of your website, so it's important to optimize them before uploading them to your shop.
  • Use caching to store frequently used data in memory, so it can be retrieved more quickly. This can significantly improve the performance of your shop by reducing the number of database queries and server requests that need to be made.
  • Varnish and Redis are both tools that can be used to improve the performance of a Magento 2 shop.

Varnish is a web accelerator that speeds up the delivery of content by caching static and dynamic content in memory. When a user requests a page from a Magento 2 shop, Varnish will check its cache to see if the requested content is already stored there. If it is, Varnish will serve the content directly from the cache, bypassing the need to access the Magento 2 application and database. This can significantly improve the performance of your shop by reducing the number of server requests and database queries that need to be made.

Redis is an in-memory data store that can be used to cache data in Magento 2. It can be used to store a variety of data, such as product and category information, customer data, and session data. By storing this data in Redis, Magento 2 can access it more quickly, improving the overall performance of the shop.

Both Varnish and Redis can be used together to improve the performance of a Magento 2 shop. Varnish can be used to cache static and dynamic content, while Redis can be used to store data that is frequently accessed by the Magento 2 application. By using these tools together, you can significantly improve the performance of your shop and provide a better user experience for your customers.

If you experience a slow Magento2 shop that you're having and want to improve it, you can always contact us to figure out a cost-saving solution to make it faster. We love to support businesses of any kind and are quite some experts on Performance Improvements.

4. Minify CSS and JS assets

Minifying CSS and JavaScript can improve the performance of a website by reducing the size of the files that are loaded by the browser. This is especially important for websites that have a large number of CSS and JavaScript files, as these files can significantly increase the load time of a page.

Minification involves removing unnecessary characters from the source code of a file, such as whitespace, comments, and new lines. This can reduce the size of the file by up to 50%, making it faster for the browser to download and parse.

In addition to reducing the size of the file, minification can also improve the performance of a website by reducing the number of HTTP requests that are needed to load the page. When a browser requests a page, it must make separate requests for each CSS and JavaScript file that is included on the page. By minifying these files and combining them into a single file, you can reduce the number of requests that are needed to load the page, improving the overall performance of the website.

There are a number of tools and techniques that you can use to minify CSS and JavaScript, including online minifiers, build tools such as Grunt and Gulp, and server-side optimization solutions such as mod_pagespeed for Apache.

In conclusion, minifying CSS and JavaScript can significantly improve the performance of a website by reducing the size of the files that are loaded by the browser and reducing the number of HTTP requests that are needed to load the page. This can lead to faster page loads and a better user experience for your visitors.

5. an optimized and lightweight Frontend

A modern, lightweight frontend framework/theme is crucial for good performance on a Magento 2 shop because it can significantly improve the loading speed of the shop's pages.

One of the main factors that affects the loading speed of a webpage is the size of the files that are loaded by the browser. A large, complex frontend framework/theme can increase the size of these files, making them slower to download and parse. This can lead to slower page loads and a poorer user experience for your customers.

On the other hand, a modern, lightweight frontend framework/theme can reduce the size of the files that are loaded by the browser, making the pages of your shop faster to load. This can be achieved by using a framework/theme that is optimized for performance, such as one that uses minimal CSS and JavaScript and is designed to be as lightweight as possible.

In addition to improving the loading speed of your shop's pages, a modern, lightweight frontend framework/theme can also improve the overall performance of your shop by reducing the workload on the server. This can be achieved by offloading tasks such as rendering and styling to the client-side, which can reduce the number of server requests that are needed to load a page.

In short, using a modern and lightweight frontend framework/theme is essential for ensuring good performance on a Magento 2 shop. This is because it can significantly speed up the loading of the shop's pages and make the overall experience better for customers. Additionally, a lightweight framework/theme can reduce the burden on the server, making the shop more efficient overall.


6. having the right infrastructure

Having the right infrastructure in place is crucial for the smooth and efficient operation of a Magento 2 online shop. The infrastructure of a shop refers to the hardware and software resources that are used to run the shop, including the servers, databases, and applications that are needed to support the shop's functionality.

One of the main benefits of having the right infrastructure in place is improved performance. A well-designed and optimized infrastructure can significantly improve the speed and reliability of a Magento 2 shop, leading to a better user experience for customers. This is especially important for shops that handle a high volume of traffic or transactions, as these shops can put a strain on the infrastructure if it is not properly configured.

Another benefit of having the right infrastructure in place is improved scalability. A scalable infrastructure can easily be expanded or modified to meet the changing needs of a business, such as the addition of new products or the implementation of new features. This can help to ensure that a shop is able to grow and evolve over time without being limited by the limitations of its infrastructure.

Finally, having the right infrastructure in place can also improve the security and reliability of a shop. A well-secured and redundant infrastructure can help to protect against cyber threats and minimize the risk of downtime, ensuring that a shop is always available and accessible to customers.

So, generally speaking, having the right infrastructure in place is crucial for the smooth and efficient operation of a Magento 2 online shop. It can improve the performance, scalability, and security of a shop, leading to a better user experience for customers and a more successful business overall.


Some additional note if you are considering Magento2 as your new online shop:

The development costs of a Magento 2 shop can vary widely depending on the specific needs and requirements of the business. Some factors that can affect the development costs of a Magento 2 shop include the complexity of the design and functionality, the number of integrations and extensions that are required, and the amount of custom development work that is needed.

In general, Magento 2 is a powerful and flexible e-commerce platform that is well-suited for businesses of all sizes and industries. It offers a wide range of features and functionality out of the box, and can be customized and extended to meet the specific needs of a business.

However, it is important to note that Magento 2 can be more complex and time-consuming to develop than other e-commerce platforms, such as Sylius for example. This is due to the large number of options and configurations available, as well as the need to develop and maintain custom integrations and extensions. As a result, the development costs of a Magento 2 shop can be higher than those of other platforms.

Overall, Magento 2 is best suited for businesses that require a high level of customization and integration with other systems, or that need to handle a large volume of transactions and products. It may not be the best fit for businesses that are just starting out and are looking for a more streamlined, cost-effective solution. For those companies we're usually suggesting not to get a Magento 2 shop, as it can become quite a financial burden to develop and maintain the onlineshop, especially when the vendor only wants to start selling their products online. In these cases we usually suggest to take on of our amazing Sylius shops, which are just as well performant and fast, however, bring less features by default. Honestly: in 99.9% of the time, a Sylius shop is perfect for a small business, where a Magento2 shop gets better results for larger business. That being said, Sylius is extremely powerful and can very easily compete with Magento 2. Its just a different architecture.

In conclusion, the development costs of a Magento 2 shop can vary widely depending on the specific needs and requirements of the business. Magento 2 is a powerful and flexible e-commerce platform that is well-suited for businesses that require a high level of customization and integration, but may not be the best fit for businesses that are just starting out and are looking for a more streamlined, cost-effective solution.