Extreme Magento Speed: How to Get Instant Page Load Times

by on January 23, 2012

Fact: Lightning fast eCommerce stores have higher conversion rates. Even with the size and complexity of Magento it’s possible to get near-instant page load times. How do you speed up Magento 1.6 and beyond? First of all, if you haven’t read the basics on Magento speed and performance you should do so now. Read my introduction to speeding up Magento here. After you’re done and you still haven’t broken a sweat, it’s time to turn it up a notch. It’s time for supersonic Magento performance.

Q: Is it going to cost me more money?
A: Not necessarily. I’ll recommend both paid and free solutions.

Choose the Right Host

First things first. If you’re not familiar with servers go with a host that specializes in Magento. If you can log in to SSH no problem and you know the Linux distro you’re running then you don’t need my hosting advice! For store owners that just want fast Magento hosting and no headaches I recommend Nexcess Magento Hosting.

I know many store owners relying on Nexcess. They’re 100% PCI compliant, optimize servers for Magento, and provide unique IP addresses and SSL certificates for separate stores. They also offer great support and can help you set up my next recommendation.

If you’re competent at managing servers, you may want to consider using Amazon EC2 or another cloud hosting environment. The official Magento wiki has a detailed guide to configuring a server for Magento on EC2 (with plenty of suggestions also found in this article).

One last thing about hosting. You may want to ask your hosting provider about NginX. It’s a lightweight, high-performance web server that can replace your Apache web server. It’s extremely fast. For a good comparison between the two click here.

[top]

Distributed Memory Caching Systems & PHP Accelerators

By default Magento allows you to cache data via files or the database. You’ll want a better cache system and a PHP accelerator. Keep in mind this technique and the ones that follow are very advanced. You may need assistance from your hosting provider or developer.

Before reviewing this list pay attention. Some websites and even Magento can confusingly group Memcached with PHP accelerators. Memcache/Memcached is not a PHP accelerator. It’s a distributed memory caching system which is only used for storing cached data across multiple, load-balanced servers. To maximize performance I recommend using both Memcached and a PHP accelerator. At the very least choose APC and worry about Memcached later.

To learn more about Memcached and memory caching systems, click here.

To learn more about PHP accelerators, click here and get a comprehensive list of PHP accelerators.

Memcached (Distributed Memory Caching System)

Magento supports several caching systems out of the box. From my experience one of the most used is Memcached. It’s even used by huge websites like YouTube, Facebook, and Twitter. Once you install a supported cache system all you need to do is update your /app/etc/local.xml file. Click here to learn how to enable Memcached in Magento. Here’s a quick overview:

1) If you’re on a Debian-based Linux distro run the following:

sudo apt-get install memcached php5-memcache

2) Update your /app/etc/local.xml file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<cache>
  <backend>memcached</backend><!-- apc / memcached / empty=file -->
  <memcached><!-- memcached cache backend related config -->
    <servers><!-- any number of server nodes can be included -->
      <server>
        <host><![CDATA[127.0.0.1]]></host>
        <port><![CDATA[11211]]></port>
        <persistent><![CDATA[1]]></persistent>
        </server>
    </servers>
    <compression><![CDATA[0]]></compression>
    <cache_dir><![CDATA[]]></cache_dir>
    <hashed_directory_level><![CDATA[]]></hashed_directory_level>
    <hashed_directory_umask><![CDATA[]]></hashed_directory_umask>
    <file_name_prefix><![CDATA[]]></file_name_prefix>
  </memcached>
</cache>

Keep in mind this local.xml configuration only applies if you’re exclusively using Memcached without a PHP accelerator. To learn how to combine Memcached with an accelerator such as APC, read on.

APC (PHP Accelerator)

APC (Alternative PHP Cache) is a very popular PHP accelerator. You’ll need to copy the APC PHP extension into your PHP extensions directory or if you’re on a Debian-based Linux distro you can run:

sudo apt-get install php5-apc

…and then update your APC configuration as recommended in this official Magento Forum thread:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
extension = apc.so    #name dependent on your APC cache install
 
[APC]
apc.enabled = 1    # Turn APC cache on
apc.optimization  = 0    # Experimental keep off
apc.shm_segments = 1    # Shared memory segments
apc.shm_size = 128  # Increase until Cache Full Count remains at 0
apc.ttl = 7200
apc.user_ttl  = 7200
apc.num_files_hint = 8096
apc.user_entries_hint = 8096
apc.mmap_file_mask = /tmp/apc.XXXXXX
apc.enable_cli = 1 # Allow command line php to function
apc.cache_by_default  = 1 # Enabled, 0 for filters
apc.max_file_size = 5M # Maximum cached file size
apc.stat = 1 # 1 for dev, 0 for production, whether the source file is checked for mod date
#apc.include_once_override = 1 # Use PHP5.3+ for include_once optimization

Next you’ll need to update your /app/etc/local.xml file. It’s a bit easier than Memcached if used as an exclusive caching system:

1
2
3
4
5
6
<global>
     <cache>
          <backend>apc</backend>
          <prefix>MAGENTO_</prefix>
     </cache>
</global>

Wait! Are you planning to combine Memcached and APC? This is what you need to do in your local.xml configuration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<global>
     <cache>
          <backend>Apc</backend>
          <slow_backend>Memcached</slow_backend>
          <fast_backend>Apc</fast_backend>
          <slow_backend_options>
               <servers>
                    <server>
                         <host><![CDATA[127.0.0.1]]></host>
                         <port><![CDATA[11211]]></port>
                         <persistent><![CDATA[1]]></persistent>
                    </server>
               </servers>
               <compression><![CDATA[]]></compression>
               <cache_dir><![CDATA[]]></cache_dir>
               <hashed_directory_level><![CDATA[]]></hashed_directory_level>
               <hashed_directory_umask><![CDATA[]]></hashed_directory_umask>
               <file_name_prefix><![CDATA[]]></file_name_prefix>
          </slow_backend_options>
          <memcached>
               <servers>
                    <server>
                         <host><![CDATA[127.0.0.1]]></host>
                         <port><![CDATA[11211]]></port>
                         <persistent><![CDATA[1]]></persistent>
                    </server>
               </servers>
               <compression><![CDATA[]]></compression>
               <cache_dir><![CDATA[]]></cache_dir>
               <hashed_directory_level><![CDATA[]]></hashed_directory_level>
               <hashed_directory_umask><![CDATA[]]></hashed_directory_umask>
               <file_name_prefix><![CDATA[]]></file_name_prefix>
          </memcached>
     </cache>
</global>

We use APC to improve the speed and performance of our PHP code via opcode caching. Memcached is used to distribute cache across several load-balanced web servers as the “slow backend”.

XCache (PHP Accelerator)

XCache is another cache system supported by Magento. It’s similar to APC. I highly recommend using APC since it’s officially supported as part of PHP. However, I felt that you should be aware of the supported alternatives. XCache works well with Apache.

For more information about XCache, click here. The installation process is similar to APC and the local.xml file only requires a few extra tweaks.

eAccelerator (PHP Accelerator)

The nice thing about eAccelerator is that it doesn’t require any additional configuration with Magento. It’s as simple as installing and giving it your PHP path. However, it does lag behind releases of PHP and development is slowing. eAccelerator is still a great PHP accelerator and I’ve seen it combined with another accelerator such as ionCube.

If you’re interested in tweaking eAccelerator for Magento, check out this in-depth article.

[top]

Block & Full Page Caching

Now that we’re optimizing our cache and session handling we can learn about the most essential element of maximum Magento speed optimization: Full page caching. If you want instant load times this is the best optimization you can perform. You have two choices if you’re running Magento CE. If you’re on Magento Enterprise then lucky you because it already comes with full page caching!

Varnish + PageCache (Free)

Varnish is the ultimate open source web application accelerator, also known as a caching HTTP reverse proxy. It typically speeds up delivery with a factor of 300-1000x. That’s wicked fast!

If you’re looking for a free full page caching solution then use Varnish along with a free Varnish extension such as PageCache or Cache Extension.

You’re going to get lightning fast load times with Varnish. I guarantee it.

Tinybrick Extensions (Paid)

Tinybrick is a popular Magento extension vendor that’s been around for awhile. They offer two speed optimization extensions for Magento: Speed Booster and LightSpeed. Speed Booster caches catalog and CMS blocks to dramatically speed up the homepage and catalog browsing. LightSpeed is a full page caching solution.

I personally recommend using both extensions. I’ve installed them before and it’s very straightforward thanks to the documentation. Out of the box both extensions do a great job of caching and you’ll notice a huge performance boost. However, it can get a little tricky if you run into extension conflicts. They don’t play nice with certain minification and HTML compression extensions.

If you want absolute max performance from Tinybrick LightSpeed you’ll need to use a technique called holepunching. By default LightSpeed doesn’t provide cached pages when you add an item to the cart or log in to an account. In order to cache pages all the time you’ll need to holepunch dynamic areas.

For example, let’s say you have 0 items in your cart. You decide to purchase an item. You’ll need to holepunch and refill the cart block so the cart will update with “1 Item in Cart”.

Holepunching can be time-consuming and complex. It involves updating layout XML files and block templates, as well as creating a custom holefilling extension to fill in the hole with dynamic data. If you need assistance holepunching your Magento theme I offer speed optimization services.

Tinybrick Speed Booster for catalog and CMS block caching (70% faster).
Tinybrick LightSpeed for full page caching (90% faster).

[top]

Search Indexing Performance

One thing I’m hoping Magento will improve in the future is the quality and speed of product search. Luckily there’s a few extensions out there that can help you knock out both slow searches and poor quality results. The key is Apache Lucene or Sphinx, two open source indexing and search libraries.

Check out these Magento extensions to get started:

[top]

Compilation & Flat Catalog

These are two speed tips that should have been included in the basics but for the sake of completion I’ll be adding them here.

Compilation is a Magento core extension that began appearing in Magento CE 1.4.2. Simply go to Systems > Tools > Compiler. Click “Run Compilation Process”. And just like that you’re done! Just make sure you test your store by compiling on a test server before using on production. You may run into issues. Based on early tests, the Magento team claimed a 25% to 50% performance improvement after using compiler mode. There’s also a command line script for interacting with the compiler.

Another unmentioned and quick configuration setting that will boost performance is enabling the flat catalog. Magento uses the EAV model to store data which allows for greater extensibility but also results in slower SQL queries. By enabling the flat catalog you merge product data into one table. This is great for stores with over 1,000 products.

To enable flat catalog, go to System > Configuration > Catalog. Under “Frontend”, switch “Use Flat Catalog Category” and “Use Flat Catalog Product” to Yes.

Make sure you clear your cache afterwards. Test your store before using on production! Flat catalog can cause issues for certain extensions and code that makes use of EAV.

[top]

MySQL Configuration

As recommended by Yoav Kutner at Magento, you can modify the configuration of your MySQL server to take better advantage of your server’s RAM. An example my.cnf was provided from his “Performance is Key!” notes.

1
2
3
4
5
6
7
8
9
10
11
12
13
key_buffer = 512M
max_allowed_packet = 64M
table_cache = 512
sort_buffer_size = 4m
read_buffer_size = 4m
read_rnd_buffer_size = 2m
myisam_sort_buffer_size = 64m
tmp_table_size = 128m
query_cache_size = 96m
query_cache_type = 1
thread_cache_size = 8
max_connections = 400
wait_timeout = 300

I also came across this interesting post on Magento Connect that quotes a server administrator at Rackspace optimizing a server for Magento (includes some MySQL tips).

[top]

Well there you have it! To get blazing fast Magento speeds for Magento 1.6+ all you need to do is follow the recommendations above and you should be set. Talk to your hosting provider and see what they offer. At the very least, make sure you install a PHP accelerator such as APC and get a full page caching solution such as Varnish or Lightspeed.

If you have any questions leave a comment below. Did you enjoy the article? Follow me on Twitter or sign up for email updates to get the latest Magento blog posts from Fast Division.

  • http://nexcess.net/ Greg Swaney

    Thanks a lot for the shout! We’re very happy to hear your experience with our services have been a pleasant one!

    • http://fastdivision.com/ Jake Johnson

      No problem! See you guys at Magento Imagine!

  • Anonymous

    One thing here: my server can’t find the php5-apc package. I have updated the package listings, etc… is this redundant if I have already installed APC as per your previous post?

    • http://fastdivision.com/ Jake Johnson

      Hi Michael,

      Yes, it is redundant if you already followed my previous post on speed optimization. To verify that you have APC installed use <?php phpinfo();?> on a test page. APC should be listed. Good luck!

  • Rolf

    I´ve got APC installed with memcache, but in de the APC dashboard (apc.php) it keeps telling me the uptime is 0 minutes and the start time is the current time.

    Been trough Google for a couple of hours now, do you have any ideas?

  • Pingback: Mobile Magento Themes: Why You Need a Mobile Commerce Site — Fast Division

  • Julia

    Tyni Brick LightSpeed is too expensive. Advanced Cache Optimizer is four times cheaper and does the same.

Previous post:

Next post: