The most recent release of Magento Commerce, 2.4 included many new features and updates. The upgrade introduces support for PHP 7.4, Elasticsearch 7, and MySQL 8 versions. Not only that, but there is a significant security enhancement with two-factor authentication enabled by default for the Magento administrator. The release also introduces a default search engine catalog for Elasticsearch.
In this post, I will go over a few things you can do to get the best results from Magento 2.4.
Setup Magento 2.4 with a Docker Container
Upgrade or install Magento 2.4 in a Docker-isolated container for development. Doing this provides better elasticity and the ability to create separate instances for various purposes on a single underlying host machine. Please note that the Docker container can run on different PHP versions at the same time. Also, it provides an additional security layer for accessing the website code.
Why Elasticsearch is Must for Magento 2.4
Instead of using MySQL for search, Magento 2.4 now uses Elasticsearch as the default search engine. An Elasticsearch active node is required to complete the Magento 2.4 version. Similarly, a RabbitMQ can also be used for publishing messages in a queue and can define consumers that receive messages asynchronously. The Elasticsearch service can be created in a Docker container as well. For development purposes, we observed the recommended and supported version of Elasticsearch-7.6.x has some performance optimization when compared to the earlier version.
It’s important to follow the necessary system configuration process as recommended in the Elasticsearch documentation with respect to the file descriptor and virtual memory. If the underlying host is using a SWAP memory, then it’s recommended to limit the memory usage to avoid service killed due to high memory usage.
Use Varnish with Magento 2.4
Varnish, also referred to as ‘HTTP accelerated’, is designed for use with the HTTP protocol. Magento 2.4 supports the Varnish-6.x version. With the introduction of the latest Varnish support, the platform is optimized to serve the HTTP assets quickly back to the user.
As shown below, any asset requests can be fulfilled by Varnish. For requests that don’t reach the Web servers, Varnish can also return the cached content and reduce the number of subsequent requests that must be filled by Magento.
In the latest release of Varnish for Magento 2.4, it’s recommended to use the VCL for the same Varnish-6.x version. This is due to the fact that some old variables have been deprecated and features such as restart the transaction after TTL or grace expiration. The Varnish service will fail to start if it isn’t supported by a VCL configuration.
Configure and Use Redis
For storing the PHP session and FPC, Magento provides a CLI option to configure Redis caching. The caching setting can be configured by making changes in the app/etc/env.php file for high-speed backend cache with full cache tag support. This is recommended in multi-server environments.
Magento 2.4 is tested with the MySQL-8, but it also supports older versions like MySQL5.7. However, this has not been tested and recommended. The MySQL-8 version has some changes that support query optimization and indexer to optimize the Magento performance.
The MySQL-8 has binary logging enabled by default for database replication, which can be turned off to reduce the transaction time and database workload. But, in a cluster environment database replication, it’s a good thing to have for quicker fall back to a healthy node and to bifurcate the READ and WRITE database operation.
Infrastructure Setup for Magento2.4
As shown in the diagram below, an Azure CDN has been set up for delivering high-bandwidth static contents, accelerated dynamic contents, and WAF for blocking any unauthorized access for sensitive files. Then, Varnish is placed for accepting HTTP requests and serves cached contents that are available on its memory.
The uncached assets will proxy to the Magento container running on a dedicated cloud server. For search functionality, an Elasticsearch service is shared between both the WEB servers and a MySQL-8 database server, as well as with the Redis cache services to store Magento sessions and backend cache.
Using the architecture above, we’ve released six different environments for Magento-2 developments.
Why It Helps
Docker technology is more controllable and granular as it is a microservices-based method focused on efficiency. Using containers, developers can create required environments that are isolated from other apps running on the same host if you are building code with repeatable infrastructure and configuration. When an application is containerized, it’s isolated from other apps running in the same system, thus direct access to the application code is not easy. Docker allows us to build, test, and release images that can be deployed across multiple servers and provides flexibility to scale out the instances in a short duration, and starts serving the production traffic. The use of accelerator, like Varnish & CDN, enhance the operability of the application and can reuse the cached contents for faster content delivery.