Magento server performance configuration

magento-server-perofrmance-setupThere are best practices in Magento 2 performance configuration of operating system, MySQL, PHP and webserver. This article covers important configurations of all components that will speed up the Magento site.

Operating system

Operating system configurations and optimizations are similar for Magento as other high-load web applications. As the number of concurrent connections handled by the server increases, the number of available sockets can become fully allocated. The Linux kernel supports a mechanism to “reuse” and “recycle” TCP connections. Be aware that more aggressive recycling than re-use may cause issues on the load balancers. To enable these kernel settings, set the following values in /etc/sysctl.conf:

The kernel parameter net.core.somaxconn controls the maximum number of open sockets waiting for connections. This value can be safely increased to 1024, but it should be correlated with the ability of the server to handle this amount. To enable this kernel parameter, set the following value in /etc/sysctl.conf:

PHP Settings

To guarantee successful execution of all Magento instances without dumping data or code to disk, set the memory limit as follows:

 

ByteCode

To get maximum speed out of Magento 2 on PHP7, you must activate the OpCache module and properly configure it. These settings are recommended for the module:

When you fine-tune the memory allocation for opcache, take into account the size of Magento’s code base and all your extensions. Magento’s performance team uses the values in the preceding example for testing because it provides enough space in opcache for the average number of installed extensions.

If you have a low-memory machine and you do not have many extensions or customizations installed, use the following settings to get a similar result:

 

APCU

We recommend enabling the PHP APCu extension and configuring composer to support it to optimize for maximum performance. This extension caches file locations for opened files, which increases performance for Magento server calls including pages, Ajax calls, and endpoints.

Edit your apcu.ini file to include the following:

Web server

Magento fully supports the Nginx and Apache web servers. Magento provides sample recommended configuration files in the <magento_home>/nginx.conf.sample (Nginx) and <magento_home>.htaccess.sample (Apache) files. The Nginx sample contains settings for better performance and is designed so that little reconfiguration is required. Some of the main configuration best practices defined in the sample file include:

  • Settings for caching static content in a browser
  • Memory and execution time settings for PHP
  • Content compression settings

You should also configure the number of threads for input request processing, as listed below:

Web server Attribute name Location Related information
Nginx worker_connections /etc/nginx/nginx.conf (Debian) Tuning NGINX for Performance
Apache 2.2 MaxClients /etc/httpd/conf/httpd.conf (CentOS) Apache Performance Tuning
Apache 2.4 MaxRequestWorkers /etc/httpd/conf/httpd.conf (CentOS) Apache MPM Common Directives

MySQL

This document does not provide in-depth MySQL tuning instructions because each store and environment is different, but we can make some general recommendations.

There have been many improvements to MySQL 5.6 and 5.7. We are confident that MySQL is distributed with good default settings. The most critical settings are:

Parameter Default Description
innodb_buffer_pool_instances 8 The default value is set to 8 to avoid issues with multiple threads attempting to access the same instance.
innodb_buffer_pool_size 128MB Combined with the multiple pool instances described above, this means a default memory allocation of 1024MB. The total size is divided among all the buffer pools. For best efficiency, specify a combination of innodb_buffer_pool_instances and innodb_buffer_pool_size so that each buffer pool instance is at least 1 GB.
max_connections 150 The value of the max_connections parameter should correlate with the total number of PHP threads configured in the application server. A general recommendation would be 300 for a small and 1,000 for a medium environment.
innodb-thread-concurrency 0 The best value for this configuration should be calculated by the formula: innodb-thread-concurrency = 2 * (NumCPUs + NumDisks)

Varnish

Magento highly recommends using Varnish as the full page cache server for your store. The PageCache module is still present in the codebase, but it should be used for development purposes only. It should not be used together with, or instead of, Varnish.

Install Varnish on a separate server in front of the web tier. It should accept all incoming requests and provide cached page copies. To allow Varnish to work effectively with secured pages, an SSL termination proxy can be placed in front of Varnish. Nginx can be used for this purpose.

Magento distributes a sample configuration file for Varnish (versions 4 and 5) that contains all recommended settings for performance. Among them the most critical in terms of performance are:

  • Backend health check polls the Magento server to determine whether it is responding in a timely manner.
  • Grace mode allows you to instruct Varnish to keep an object in cache beyond its Time to Live (TTL) period and serve this stale content if Magento is not healthy or if fresh content hasn’t been fetched yet.
  • Saint mode blacklists unhealthy Magento servers for a configurable amount of time. As a result, unhealthy backends cannot serve traffic when using Varnish as a load balancer.

See Advanced Varnish configuration for more information about implementing these features.

Optimize asset performance

In general, we recommend storing your assets (images, JS, CSS, etc) on a CDN for optimal performance.

If your site does not require deploying a large number of locales and your servers are in the same region as the majority of your customers, you may find significant performance gains at a lower cost by storing your assets in Varnish instead of using a CDN.

To store your assets in Varnish, add the following VCL entries in your default.vcl file generated by Magento for Varnish 5.

At the end of the if statement for PURGE requests in the vcl_recv subroutine, add:

 

In the vcl_backend_response subroutine, look for the if statement that unsets the cookie for GET or HEAD requests. The updated if block should look like the following:


 

Restart the Varnish server to flush cached assets whenever you upgrade your site or deploy/update assets.

Caching and session servers

Magento provides a number of options to store your cache and session data, including Redis, Memcache, filesystem, and database. Some of these options are discussed below.

Single web node setup

If you plan to serve all your traffic with just one web node, it does not make sense to put your cache on a remote Redis server. Instead, either use the filesystem or a local Redis server. If you want to use the filesystem, put your cache folders on a volume mounted on a RAM filesystem. If you want to use a local Redis server, we highly recommend configuring Redis so it uses sockets for direct connections, rather than exchange data through HTTP.

Multiple web nodes setup

For a multiple web nodes setup, Redis is the best option. Because Magento actively caches lots of data for better performance, pay attention to your network channel between the web nodes and the Redis server. You do not want the channel to become a bottleneck for request processing.

 

Want me to do this for you? Drop me a line: itgalaxyzzz {at} gmail [dot] com