Magento Speed up (Advanced): fine-tune Magento performance and scalability
In a previous post, I demonstrated some Magento Speed up – basic steps to improve Magento performance.
Now we are going dig deeper and work on more complicated methods. Many of these methods require addition installation or hands-on tuning, and their effects will vary a lot depending on the exact setup. So the key is to experiment and find out the combination best for your site.
First, an overview of the key subjects:
- Improve Magento cache: ramdisk and tempfs
- PHP Accelerator: Alternative PHP Cache and XCache
- Keeping connection alive?
- Combining JavaScript and CSS: Minify library
- Magento beyond single server
Improve Magento cache: ramdisk and tempfs
When the system loads the cache files, the limiting factor is the hard disk access speed. It’s so much slower than the speed of RAM. So we should take advantage of the ‘RAM disk’, which treats the memory as if it were a disk drive. Linux system comes with ramdisk, it may take you a few moments to setup. You can follow the details here: http://www.vanemery.com/Linux/Ramdisk/ramdisk.html)
A simpler implementation would be tmpfs, which is supported by the Linux kernel 2.4 and up. You can directly run the following in the terminal:
[codesyntax lang=”ini” lines=”fancy”]
mount -t tmpfs -o size=256M,mode=0744 tmpfs [cache_path] mount -t tmpfs -o size=64M,mode=0744 tmpfs [sessions_path]
[/codesyntax]
Note that you want to give everyone read access. And if you already saved sessions in the database (See previous post), you can skip the 2nd line.
Expected effect: In most cases, this is a significant improvement.
A very important point is: we are using RAM for storage. Key in mind they are volatile memory. So when you power off or reboot, the data would be lost. Make sure you copy the data to hard drive before you do so.
PHP Accelerator: Alternative PHP Cache and XCache
Due to the complexity of Magento class structure, a myriad of PHP files are required for each final view on the frontend or backend. As a result, a PHP accelerator is often desirable. It will cache the complied bytecode of the script. So upon each request the source code does not have to be parsed and compiled over and over again. This will reduce the server load significantly.
Note that many hosting companies may already have some PHP accelerator installed, you may want to check this before you start.
Expected effect: Very significant if no accelerator installed previously.
The popular choices would be Alternative PHP Cache (APC) http://pecl.php.net/package/APC or XCache http://xcache.lighttpd.net/
For APC, add the following to app/etc/local.xml:
[codesyntax lang=”xml” lines=”fancy”]
<global> <cache> <backend>apc</backend> <prefix>MAGE_</prefix> </cache> </global>
[/codesyntax]
Please be careful if you are hosting multiple stores on the same domain, you need to separate them with the <prefix> tag.
Keeping connection alive?
You can allow persistent connections to your server by enabling Apache KeepAlives. In essence, this allows multiple request to be sent by the client through a single TCP connection.
[codesyntax lang=”apache” lines=”fancy”]
KeepAlive On KeepAliveTimeout 15
[/codesyntax]
However, do not push this feature too far. It can increase the memory usage considerably and become a limiting factor.
Expected effect: KeepAlive is very useful when the requested page contains many images. However, as mentioned above, it may go both ways. Test first.
Combining JavaScript and CSS: Minify library
In the previous post, I also mentioned that a lot of JavaScript and CSS file would be loaded for each page in Magento. This gives room to optimization by ‘Minifying’ them, i.e. multiple JavaScript and CSS files will be combined and compressed into a single file, which will then be cached on the server side.
The project is hosted on Google, you can get it at: http://code.google.com/p/minify/
Expected effect: This is usually not a very big improvement, but in addition it’s also useful for search engine optimization.
Magento beyond single server
A final remark, as your business grows, a single-server solution will eventually become the bottle neck of the performance. We need to scale up. There are a couple of thing you can do without making your budget go rocket-high. Usually, the first thing to consider is to add a new web server, say, a media server. You may also do some image optimization at the same time.
The key is to maintain a structure with one centralized database server and then support it with several load-balanced web servers. This will help you to keep your budget low. In case you really need to move up to a multiple database server setup, Magento can also handle cluster environment well. Of course the cost will jump at this, but at that stage, the quality of the service should be a more important factor.