Optimising Magento Part II

It appears that there are quite a few complaints on the forums about Magento being slow and this is probably true with an out of the box installation. There are quite a few things that can be done to improve things and response time in general.

With a switch to nginx as well as tuning of various components, the response time subsequently dropped by over half to about 4 seconds which is in the sweet spot for ecommerce sites.

The following are some of the changes made:

  • Switch to using nginx as a front end.
  • Set Expires to far in the future
  • Utilize PHP FPM.
  • Implement APC
  • PHP tuning
  • MySQL tuning

And Finally Test your site response using the tool http://tools.pingdom.com/

Optimising Magento Part I

Linux

If you have access-time logging enabled on any of your mysql, web server or cache partitions, try turning it off for a performance boost. If you’re using ext3 or reiserfs there may be faster journal write methods you can use. For more information see http://www.linux.com/feature/116693.

Windows

BSD

OS X

Web Server Configuration

Apache

http://httpd.apache.org/

Apache is a hugely popular web server (the A in LAMP). It can serve PHP sites using mod_php, FastCGI or CGI.

* Enable KeepAlives (see http://httpd.apache.org/docs/2.2/mod/core.html#keepalive and http://www.magentocommerce.com/blog/comments/performance-is-key-notes-on-magentos-performance/)

NginX

NginX is becoming an increasingly popular alternative to Apache and Lighttpd due to its stability, light weight and speed. NginX can server PHP sites using FastCGI or CGI.

PHP Configuration

General

* Set memory limit to 64M

mod_php

mod_php for Apache is probably the most popular way to run PHP applications. There are a few down-sides to mod_php though: you’re forced to use Apache’s less efficient prefork MPM and your Apache processes will get large. Serving static files with 32MB+ Apache processes isn’t very efficient.

PHP CGI

When PHP is run as a CGI program the web server spawns a process each time a request is made. This can be less efficient unless your operating system is good at spawning processes quickly. Unlike mod_php, CGI application won’t bloat your Apache, so the processes can remain small for service static files – important for high traffic sites.

PHP FastCGI

When PHP is run as a FastCGI, a number of daemon processes are started and sit waiting for the web server to request them to run PHP code. This avoids the cost of spawning new processes each time in addition to not bloating web server processes. Due to the nature of long-running processes, FastCGI may require some periodic process maintenance. Lighttpd’s spawn-fgi script provides a method to limit the number of requests a single FCGI process is allowed to serve in an attempt to reduce the effect of memory leakage.

Opcode Caches

PHP Opcode caches reduce the processing overhead involved in reading and compiling PHP scripts. They utilise shared memory as a way of storing and re-using compile scripts.

APC

APC http://uk2.php.net/apc is a free PHP opcode cache written by the PHP team and packaged using PECL. APC can be used with mod_php, FastCGI and CGI.

xCache

eAccelerator

ionCube

Zend Cache

MMCache

MySQL/InnoDB Configuration

Most operating systems install a lightweight MySQL configuration by default, a good LAMP server should provide a decent amount of its resources to MySQL. Machines with multiple cores and over 2GB RAM need to be configured carefully to make sure available RAM and cores are being used efficiently.

MySQL

There are some excellent tips in this Magento blog post http://www.magentocommerce.com/blog/comments/performance-is-key-notes-on-magentos-performance/

After your server has been running for at least 48 hours the “MySQL Performance Tuning Primer Script” analyses your server statistics against your configuration and offers guidance: http://www.day32.com/MySQL/

InnoDB

InnoDB (http://www.innodb.com/) is an alternative storage engine which makes use of MySQL’s client/server architecture. Whilst it shares some configuration settings with MyISAM (MySQL’s default storage engine) it has many settings of its own and will compete for server resources with MyISAM. It’s possible to tune MySQL server purely for InnoDB if no MyISAM tables are being used. Magento does however use MyISAM for some small tables so MyISAM is still required.

Multiple MySQL Slaves/Clustering

On larger sites MySQL’s replication functionality can be used to spread SELECT queries across multiple machines. Writes operations will still be run on every server in order to maintain data synchronisation.

Cache Filesystem

Magento uses file caching to store often-used data in an easy to use format. The speed of file caching is directly dependent on speed of the filesystem on which the cache files are being read or written. Again, since hard drives are slow moving this to RAM can really help. Having said that, operating systems will attempt to buffer reads/writes in memory in order to increase speed.

tmpfs

tmpfs is an in-memory filesystem designed to deliver the best possible performance.

Reverse Proxy Configuration

Reverse proxies can be used to speed up the serving of cachable assets such as images, CSS, html and javascript by removing the need to ask the web server to serve them.

Apache mod_proxy/mod_cache

Apache with mod_proxy using the worker MPM is a popular reverse proxy. There are probably also much leighter-weight alternatives. Setting up effect reverse proxies requires a good understanding of the application being proxied and the way in which it is used.

Finally:

We implemented only MySql cache the page request come down to 1-2 sec. Its a huge difference without going for a higher end server