©  Viktor Khliupko 2017

Viktor Khliupko, Magento 2 DIY , 10.1007/978-1-4842-2460-1_11

11. Performance

Viktor Khliupko

(1)Duesseldorf, Germany

A435230_1_En_11_Figa_HTML.jpg

Good performance is a key aspect of a successful e-commerce project. It is not only important as a vital ranking factor, but is highly appreciated by customers. Thus, a fast online store often has a higher conversion rate than a slower one. As a result, it turns more visitors into buyers and helps to increase sales. Now, when the importance of Magento 2 performance is obvious, it’s time to shed light on how to increase it in the case of the platform1.

We already have a detailed guide to Magento 1.X productivity, but the second version of the platform differs a lot and requires another approach to performance improvements. Magento 2 is faster, less resource-hungry, and better optimized for high loads than 1.X. Besides, it offers a lot of new performance features out-of-the-box. For instance, you get native support for Varnish, Redis, and Nginx as well as full page caching in Magento 2. As for the first version of the e-commerce platform, all these features were only available through customizations and extensions.

Although Magento 2 provides better opportunities related to performance, there is still a lot of work to be done before your e-commerce store will be as fast as the platform allows. You can check the appropriate article on Firebear, The Magento 2 Performance Guide2, but now we will start with Nginx.

Full Nginx Support

Nginx has been developed with three core principles in mind: high performance, high concurrency, and low usage of memory. Thus, it significantly increases the speed of every web site. To handle requests, Nginx utilizes an event-driven asynchronous approach, while the default Magento 1.X is based on a process-oriented approach with the Event MPM as the asynchronous processing model. Due to event-driven architecture, Nginx shows more predictable performance even under higher loads.

As mentioned above, Magento 2 offers a native support for this solution, so it can be easily installed on top of the platform. To find out how to perform this procedure, check this Magento 2 Nginx Configuration3 guide. Besides, Magento 2 has a recommended Nginx configuration in the root folder. You can also check it on GitHub here.

Magento 2 and Redis

Redis is an advanced key-value cache which provides top notch performance and offers such features as value incrementation in a hash; pushing an element to a list; appending to a string; getting a sorted set of members with the highest ranking; and set intersection, union and difference computing.

It incorporates several use cases for in-memory datasets, which are the reason for high performance results. Thus, you can either persist it by dumping the dataset to disk or append each command to a log. Moreover, if you need a feature-rich, in-memory cache, the persistence can be disabled.

Additionally, Redis incorporates non-blocking synchronization and auto-reconnection with resynchronization partial on net split. All these features make it a lightning-fast solution for every e-commerce web site, especially for a Magento store.

If you don’t know how to install and use Redis with the second version of the popular e-commerce platform, check this Magento 2 guide: Magento 2 Redis Configuration4.

Different Cache Types

Magento works with the following types of cache:

  • Configuration cache appears when Magento gathers configuration from modules. It also includes store-specific settings from database and the file system.

  • Layout cache includes compiled page layouts.

  • Block HTML output cache consists of HTML page fragments per each block.

  • Collections data cache gathers database queries.

  • DDL cache is designed to work with database schema.

  • Entity attribute value cache includes Metadata related to EAV attributes: store labels, attribute rendering, search settings, etc.

  • Page cache gathers data about generated HTML pages.

  • Translations cache is related to merged translations from modules.

  • Integration configuration cache is aimed at compiled integrations.

  • Integration API configuration cache consists of compiled integration APIs.

  • Web services configuration cache is the cache of a web API structure.

A435230_1_En_11_Figb_HTML.jpg

You can find appropriate configurations under System ➤ Cache Management. For further information, check our Magento 2 Cache Management5 guide.

Full Page Cache (FPC)

Full page caching is so powerful due to the ability to store the full page output in a cache. As a result, subsequent page loads do not require much server load. Thus, full page caching is mandatory for high- traffic web sites, as it keeps server load as low as possible and helps to avoid downtime even when traffic is really high. In addition, by using full page caching, you will increase the speed of your e-commerce web site, since all the server needs will be reduced to fetching and rendering pages from the cache.

If necessary, Magento can clean up this cache automatically, but you can still put any data in any segment of the cache manually. Please note that it is necessary to clean or flush FPC after modifying any code level that affects HTML output. Always keep this cache enabled, since it significantly improves the performance of Magento 2.

Varnish Cache

Varnish cache is an HTTP accelerator for heavily consumed APIs and content-heavy dynamic web sites. Magento 2 supports this cache out of the box, you can easily configure it for your e-commerce project to increase the default performance.

Every web server receives HTTP requests, but does not return HTTP responses immediately. Therefore, a long sequence of steps is required for each request. Multiplied by thousands of requests, these steps lead to server overloads, heavy resource consumption, and a significant performance decrease. Unfortunately, the server does not remember even repeated requests, but you can fix this problem with the aid of Varnish. It receives requests instead of your web server, looks at what’s being requested, and sends requests to your web server, which sends a response to Varnish to pass it back to the client. It seems that such a procedure only slows down the server, but the performance increase occurs when Varnish begins to store responses from the back end in its cache. As a result, it can rapidly serve further repeated responses without any need to connect to the back-end server. Besides, Varnish uses in-memory cache storage which makes it even faster.

As for Magento 2, this cache can be easily configured according to the requirements of the platform under STORES ➤ Configuration ➤ ADVANCED ➤ System ➤ Full Page Cache. For further information, check our How to Configure Varnish for Magento 2 tutorial.

A435230_1_En_11_Figc_HTML.jpg

CSS Preprocessing

Although CSS has a very powerful syntax, it can easily grow to enormous sizes when working on non-trivial projects. Luckily, we can easily fix this problem with the help of CSS preprocessors: SASS and LESS. Both allow additional leverage over CSS by offering enhanced syntax, but for the Magento 2 development we will focus on LESS. It’s core features are:

  • nested syntax;

  • ability to define variables and mixins;

  • operational and mathematical functions;

  • multiple files joining.

If you are not familiar with this CSS preprocessor, pay attention to the following documentation:

  • Magento 2 CSS preprocessing

  • Create symlinks to LESS files

CSS and JS minification

In Magento 2, CSS and JS minification procedures are almost the same as in the case of 1.X. By combining, compressing, and caching Javascript6 and CSS files, you can improve the performance of your Magento 2 web site. Thus, it’s only a matter of time when appropriate tools will be available. Besides, you can perform everything manually:

A435230_1_En_11_Figd_HTML.jpg

Catalog images caching

A435230_1_En_11_Fige_HTML.jpg

Magento pre-generates cropped product images in different sizes to reduce processing on every page load. Thumbnails are generated during the first image load and can be easily removed.

Code Generation

A435230_1_En_11_Figf_HTML.jpg

Code generation provides you with the ability to bring aspect-oriented programming, generic programming, and other complicated techniques to languages, which do not support them by default. As a result, you can improve Magento 2 performance and add unique features to the default setup. There are two ways to trigger code generation in Magento 2. First of all, you can perform it on the fly:

  • Use a meaningful name that follows a certain pattern to declare a class.

  • Now, the system should autoload the class. If it can not find it, the class will be generated.

Unfortunately, this approach slows down the system, so we recommend you utilize the command line. By using the following command, you will force the system to inspect the code and generate all necessary classes.

1   magento setup:di:compile

This approach speeds up the system, so it is useful for production.

With the aid of both approaches, you will get generated classes in the MAGENTO_ROOT/var/generation directory.

For further information check this article: Introduction to code generation in Magento 2.

RequireJS for Improved JS Performance

There is the official documentation related to the configuration of JavaScript resources, but we are going to focus our attention on RequireJS. Being a JavaScript modular script loader, this tool is optimized for both JS environments and in-browser use; therefore, it is best suited for Magento 2. RequireJS is used to improve the quality of code and the speed of apps. Since JavaScript does not provide any ways for specifying code required to execute a file, RequireJS plays a crucial role in the improvement of JS development: it specifies dependencies between JS files and loads them into a browser. Such an improvement leads to a significant jump in performance; therefore, we recommend you check this article: Advanced Development with RequireJS + Magento 2 Tutorial7.

Session Storage Management

Another useful performance improvement is related to session storage management. You can move it from files to the database or Redis. Use the -session-save option with any of the following:

  • db will move it to the database. It is necessary to choose database storage in case of a clustered database.

  • redis will move it the Redis back end.

  • files should be used to store session data in the file system.

We strongly recommend you store everything in Redis, since Magento 2 fully supports it and you can prevent your store from performance degradation. In case of a large number of users, both your database and file system suffer a dramatic decrease in productivity. As a result, your Magento 2 web site slows down, leading to a terrible user experience. Thus, Redis is the only reliable option.

Out-Of-The-Box CDN Support

A435230_1_En_11_Figg_HTML.jpg

Since Magento 2 also provides support for CDN support, media and static content can be loaded from separate subdomains located on different servers of CDN networks. CDN options can be configured under Stores ➤ Configuration ➤ Web.

A435230_1_En_11_Figh_HTML.jpg

PHP 78

Besides tons of new features and improvements introduced in PHP 7, we will get a language with completely new performance. It will be much faster than the latest available 5.X, and Magento 2 will fully support it. Thus, PHP 7 will change our understanding of how fast an e-commerce shop could be. For a deeper insight into PHP 7, check this post: PHP 7 - features, release date, rumors; and don’t forget to examine a current problem related to PHP 7 and Magento 2: String class name issue for php79.

Magento 2 vs Magento 1

You can compare the performance of both platforms via MageMeter, an amazing service from Inchoo. From the following picture, we can see that the latest Magento 2 beta with disabled caches already beats Magento 1 Enterprise Edition:

A435230_1_En_11_Figi_HTML.jpg

Magento 2 Extensions10

You can check our list of Magento 2 modules on the blog, but the platform is still too young to create a separate article related to performance extensions. We expect that all the most-wanted Magento 2 performance modules will replace their Magento 1.X ancestors. Improved Full Page cache , JS/CSS Minification , CDN services, and dozens of other tools will be available soon.

For instance, the second version of our Control for CloudFlare extension allows you to control the CloudFlare system directly from your Magento 2 admin. It offers better integration as well as more flexible connections between Magento 2 and CloudFlare than its first version. Thus, we anticipate that Magento 2 performance extensions will provide a better experience than their current analogs.

Besides, we recommend you check Magento 2 modules reviewed on Firebear. All tools are divided by companies: Magento 2 Extensions reviewed on Firebear11.

General Server Side Improvements

Although the new version of Magento offers tons of improvements compared to 1.X, there are still a lot of common performance optimizations. You can find all of them in our Magento Performance Ultimate Guide. Check the links below to find the appropriate tutorials”

  • OPcache is a code-caching module designed to improve that part of your Magento 2 performance which is related to PHP. It uses a shared memory for storing precompiled script bytecode. Thus, OPcache eliminates the PHP’s need for loading and parsing scripts on every request. Therefore, by optimising OPcache settings, you can dramatically improve the performance of your Magento 2 web site. Follow this link—OPcache12 —for further optimizations.

  • MySQL and alternatives. Properly configured MySQL is among the most important enhancements to the performance of Magento 2. First of all, it requires the understanding of your hardware; then, you should know what settings to use. But MySQL is not a necessity, since these are two reliable substitutes: MariaDB and Percona. Follow this link—MySQL13 —for further information.

  • New Relic. A slow page is always a good reason to leave a webstore and never return again. Luckily, there are a ton of different monitoring tools developed for identifying the problem in a timely fashion, and one of them is New Relic. Follow this link—New Relic14 —for further information.

This was our short guide to Magento 2 performance. We are going to update it as soon as new information is available. For more tips related to Magento 2 development, check our Cookbook15. For other important information, visit this page: Everything about Magento 2 on Firebear16.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.36.30