Best performance for Magento 2 development

Often I hear that Magento 2 is slow and loading its pages in dozens in seconds. Strange I would say, because my Magento 2 development environment is fast, faster than a Magento 1 shop could be. Here are some ground rules to make your Magento 2 in development fast too.

Native or virtual machine?

To run Magento 2 fast in your environment, you have to be knowledgable of Linux to some extent. If you are not using Linux (or MacOS), use a Virtual Machine instead. Don't attempt to run Magento 2 within XAMPP or something, it will lead to disaster. So, if you are a Windows user, there is no negotiation here, no work-around: Use a Linux-based Virtual Machine either through Vagrant or Docker (the latter is used in the official DevBox).

If you are using Linux (or MacOS) you have a choice. Personally I prefer to develop in throw-away environments, so I'm opting for Vagrant and Docker. However, you can also run the needed stack on your station locally. I always setup a simple LEMP (Linux Nginx MySQL PHP) stack on my Ubuntu laptop and desktop, just in case I need a virtual host quickly. Don't run the standalone PHP server - it can't be properly tuned.

Sync your files properly

When you are working with Vagrant or Docker, you probably need some file synchronisation between the VM files and your own local files. Don't use the Vagrant shared folder solution. Use NFS, Unison or dockersync. I've also used SSHFS, but it is slower than these 3.

Webserver and PHP 7

Use Nginx. Don't use Apache. Don't use anything lower than PHP 7. Also install Zend OPCache on top of PHP 7. In most cases it is already installed by default, but it might be disabled. Make sure Zend OPCache is enabled. Don't turn off timestamp validation, because you are going to change files. Increase the memory usage of Zend OPCache a bit.

Tuning MySQL

So, remember, these tricks are for a development environment. In a production environment I always spend time on tuning the InnoDB settings. In a development environment I mosty don't care about these settings. In production, the query_cache_size is an awesome thing to tune carefully. If you crank it up too much, it might cause a CPU lockup when the cache is flushed. So in production, it is best to keep this value between 32Mb and 256Mb. On my own desktop, I set it to be 1Gb. If there is going to be a CPU freeze, the only one who's going to be annoyed is me (and I'm generally a really easygoing guy) and it is only annoying for a few seconds. The rest of the day I enjoy a fast responsive shop.

Caching enabled with Redis

I keep all my caching enabled ... while developing. There are a lot of tutorials out there that mention to disable caching when you modify things. True, if you are new to Magento. If you are experienced, simply keep all caches enabled and keep track (in the back of your mind) that some changes will require refreshing the cache. If this becomes your way of life, it will help you troubleshoot caching issues in live sites better as well. When you start with Magento 2, you will need to find your way in XML. However, after that first learning curve, most of the time will be spent in PHP, where caching does not bite you.

Keep the Full Page Cache disabled though. And you might want to disable the Block Cache also, if you are not sure.

Use Redis. Earlier, I mentioned that Linux (or MacOS) is your best choice, so installing Redis should not be hard. Make sure to have Redis running at all times. I never found a compulsive need to tune it, it simply works. And configure Magento its app/etc/env.php file to make use of Redis.

Obviously, memory and SSD

Above all, make sure to have plenty of memory. Most of the tricks above rely on your machine having plenty of memory. A laptop or desktop of 4Gb will be just enough. I recommend to have at least 8Gb to experience the true powerrrr.

SSD is the way to go - a SSD drive is much much faster than a regular HDD. Forget about playing with RAM-disks or in-memory filesystems: SSD gets you at the same level just as well, without having to worry about a loss of data. I always try to place data that I rarely use on a slow disk, together with backups and data that changes a lot but where I care less about speed. This does not make the SSD faster, but might extend its lifetime. You could also tune your filesystem a little bit by removing logging of access times (noatime) to extend the life of the SSD a bit more (like as if it is 2007).

Keep Magento 2 up-to-date

There are a few things that are still really slow: Grunt or gulp compilation of LESS files, static view file processing, DI compilation. SSD and caching makes these processes faster, but only slightly. However Magento is working on improving this, so keeping your Magento 2 up-to-date via composer is definitely recommended. We've seen some real changes between 2.0 and 2.1, and I expect more changes performance-wise to come with the upcoming 2.2 release.

Conclusion

If you review this blog and you have come across a lot of new technologies that you did not know about yet, it might be a bit overwhelming: This is true for Magento 2 as a whole, of course, so make sure to spend time on tuning your environment to the max: The faster your own development environment is, the faster your learning curve will unfold, the more productive you will get.

Magento 2 is not slow. It simply requires you to do proper tuning of your environment before it is working as it was designed to work.