Loading..
Processing... Please wait...

Product was successfully added to your shopping cart.



Magento speed and performance boost in 20 proven steps

20 proven steps to increase magento speed and performance 20 proven steps to increase magento speed and performance

Do you want to speed up Magento performance? Of course you do! And you are not alone.

The most popular (according to BuiltWith) eCommerce platform might be slow. And it is no surprise - it was built with scalability and flexibility in mind not speed.

Here are 20 proven steps that help you increase magento speed & performance:

 

Magento Speed Roadmap

 

  1. Optimize TTFB
  2. Minimize latency
  3. Optimize Server configuration
  4. Choose fast hosting
  5. Optimize VPS for Magento
  6. Audit Magento code
  7. Optimize Start Render time
  8. GZIP compression
  9. Domain sharding
  10. Enable browser caching
  11. Prioritize above-the-fold content
  12. Remove render-blocking CSS and Javascript
  13. Reduce the size of visible content
  14. Put above-the-fold content first
  15. Optimize document complete time
  16. Serve optimized images
  17. Sign up for CDN
  18. Optimize fully loaded time
  19. Don't abuse jquery.ready
  20. The less third-party JS the better

 

 

There are ways to make your magento site fly and I will show you how to do that in a sec. But first let us define what we will use to measure site speed.

 

 

How to measure magento speed

 

When a browser loads a web page it does certain actions:

  • it receives html data from a server
  • it analyzes data and loads external files you might have on page (CSS/JS/images/fonts etc)
  • it then builds what's called DOM (Document Object Map)
  • it renders the page (puts elements on the screen)

The detailed overview of a webpage gets loaded and displayed can be seen on varvy blog.

We can define 3 time periods that measure how quickly a webpage loads:

  • Time to first byte (TTFB) or server response time: the time from when a user clicked on a link or typed in a site URL until the first bit of information from the server arrived.
  • Start render time: the first point in time that something was displayed to the screen. Before this a user was looking at a blank page.
  • DOM complete or document complete time: the point in time when browser considered the page loaded (onLoad javascript event fired). All the content has been loaded except content that is triggered by javascript execution.
  • Fully loaded time: a webpage is loaded and no network activity is performed by a web browser.

Definitions are taken from webpagetest.org.

TTFB < Start render < DOM complete < Fully loaded

The waterfall chart The waterfall chart

The most important time in my opinion is a start render time. The quicker you show a user that something is happening after he requested a webpage the faster he thinks the site is.

There are 3 main time limits that describe user perception of how quick a web application is [study] :

  • 0.1 second response time - a user feels that a system is reacting instantaneously.
  • 1.0 second response time - a user notice the delay but it doesn't bother him
  • 10 second response time - the limit for keeping the user's attention focused. For longer delays, a user will want to perform other tasks while waiting for the computer to finish.

We want to keep start render time around 1s to make a user feel a web site is fast.

Optimize Time to First Byte

 

Server response time or time to first byte (TTFB) consists of the time a browser's request goes to the server and server response goes back to a browser (latency) and the time a server generates HTML.

Google recommends having TTFB around 200ms. 1s TTFB is considered bad.

To lower TTFB you need to work on latency, server configuration and magento codebase.

1. Minimize Latency

 

Host you website as closer to your main audience as possible. If you sell shoes in London do not sign up for hosting in Japan. Common sense.

Do not think that SSL will impact your TTFB much. SSL negotiation time is negligible but SEO benefit is obvious.

2. Optimize Server Configuration

 

Optimizing server includes choosing the right hosting and setting up server software.

2.1 Choose Fast Hosting

 

Magento site can't run on $5/mo shared hosting. If you want good TTFB and start render times - sign up for VPS or magento optimized hosting Nexcess, Magemojo and others. Do a google search and see what others recommend.

2.2 Optimizing VPS for Magento

 

If you plan on running VPS or dedicated server you need to optimize server software and MySQL database.

Options to choose your server setup are endless and depend on your expertise. You can configure NGINX, Apache, Varnish, HHVM, php-fpm, Redis, memcache.

I will list possible solutions for high magento performance:

  1. Varnish + NGINX + PHP-FPM + Redis
  2. Varnish + NGINX + HHVM + Redis
  3. Use PHP7 with Magento 2 or PHP7 + Inchoo patch with Magento 1.x
  4. Host webserver and database on different servers
  5. Use MySQL master-slave replication for separate reads and writes

2.3 Server performance hogs

 

I will list common server misconfigurations that slow magento site:

  • disabled mysql query cache. Modern MySQL servers come with query cache disabled by default. Enable it - it will speed up magento significantly
  • slow disk I/O. make sure your host provides you with SSD storage.
  • RAM shortage. when system runs out of RAM it puts data on disk (swap) making things slow.

3. Optimize Magento code

 

Your Magento site php code and template files are the main reason for poor server response time.

First you need to profile the code and identify parts that slow site down. Magento comes with built in profiler you can use. You can use third party extension Aoe Profiler or sign up for NewRelic application performance management tool.

3.1 Magento code performance hogs

 

  • observers that run on every page request
  • poorly written template files
  • a slow mysql query(s) somewhere

Here is a detailed tutorial I wrote on how to optimize TTFB with magento profiler.

3.2 Poorly written magento code

 

An examples of bad magento code that I encounter too often:

Loading magento models inside a loop. That happens in product list templates, people iterate over product collection and load product model every time:

foreach($collection as $product){
  $product = Mage::getModel('catalog/product')->load($product->getId());
....

Don't do that. Better construct your collection to have all the data you need. Magento model loads are expensive.

Optimize Start Render Time

 

Start render time (SRT) is the first point in time when a user sees the content after requesting the webpage. It could be background image or some text blocks but from that time on he knows something is happening. Start render time in my opinion is one of the main factors by which a user judges how fast the site is.

According to 3 main time limits [study] i mentioned earlier we need to keep start render time around 1s.

SRT consists of:

  • time to first byte (TTFB)
  • time it takes to download a page HTML
  • <head> section parsing and downloading resources (JS/CSS external files) a browser finds there
  • initial layout calculation

After we understand what start render time is we have options to optimize it.

1. GZIP compression to shrink page size

 

Compress your html page so that browser has less kilobytes to download. Enable gzip compression on server side - see instructions for your web server. Confirm you have gzip enabled by this online testing tool.

1.1 NGINX gzip compression

 

NGINX example configuration to enable gzip:

http {
        gzip on; 
        gzip_disable "msie6";

        gzip_vary on; 
        gzip_proxied any;
        gzip_comp_level 6;
        gzip_buffers 16 8k; 
        gzip_http_version 1.1;
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;

You need to restart NGINX for the changes to take effect.

1.2 Apache gzip compression

 

When running apache you can enable gzip by changing .htaccess file inside your magento root folder:

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent
</IfModule>

Use online tool to test if gzip is on.

2. Domain sharding

 

Domain sharding is splitting external resource files (JS/CSS/images etc) across multiple domains. Why bother? Because a browser can download files in parallel but has a limit per domain. For example Chrome can do 6 concurrent connections.

If we can split images, js, css files across multiple subdomains we can make a browser download resources a lot faster.

2.1 Domain sharding in Magento

 

Magento supports domain sharding by default and it is easy to setup:

1. First create subdomains:

  • skin.domain.com
  • media.domain.com
  • js.domain.com

and point them all to the same IP and file folder your main domain is pointing to.

2. Go to magento backend main menu System > Configuration > Web and set set Base Skin URL, Base Media URL and Base Javascript URL for unsecure and secure (if you have one) links.

3. flush cache and confirm you resources are now served from subdomains.

3. Leverage browser caching

 

Browser can cache files for faster access. You can take advantage of it to lower start render time.

Configuration depends on your server:

3.1 Leverage browser caching for NGINX

 

Add this inside server directive:

  server{
       .....      
        location ~* \.(js|css|png|jpg|jpeg|ico|gif|pdf)$ {
              expires 300d;
              add_header Pragma public;
              add_header Cache-Control "public";
        }

3.2 Leverage browser caching for Apache

 

Add this into your .htaccess file:

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
## EXPIRES CACHING ##

as taken from Gtmetrix.

4. Optimize above-the-fold content

 

Above-the-fold content is a part of a page that you see before you scroll down. It is also referred to as visible content.

Prioritize visible content - load and display it as quick as possible.

How do I optimize above-the-fold content? Here are some tips:

4.1 Remove render-blocking CSS and Javascript

 

The less time a browser spends parsing <head> section of your webpage the better. Ideally only JS and CSS needed to display above-the-fold content should be put in between head tags.

Go through your css files and identify rules that are applicable to visible content. Inline those rules. Do the same for javascript.

Here is a detailed tutorial on how to optimize CSS delivery and how to defer Javascript parsing (remove render-block Javascript).

4.2 Reduce the size of visible content

 

The less visible content a browser has to do download the better. Make sure images are optimized. Remove any content that is not needed.

4.3 Put the above-the-fold content first

 

Analyze the page HTML and make sure visible content goes first.

Refer to this tutorial on how to prioritize visible content.

Optimize Document Complete time

 

Besides start render time and first byte time improvements there are a few things you can do to decrease document complete time:

1. Optimize images

 

The less time browser needs to download images the better.

There are various online tools that help you optimize images. Image Optimizer, Compressor.io, TinyPNG are just a few.

If you are familiar with linux command line then you can bulk optimize jpegs, pngs with jpegtran and optipng. Jpegtran is part of libjpeg package.

Useful oneliners to bulk optimize images for magento site:

find ./skin ./media ./js -type f -iname '*jpg' -exec sh -c 'jpegtran -outfile "{}.out" -optimize "{}"; mv "{}.out" "{}"' \;
find ./media ./skin -iname '*.png' -exec optipng -o5 -keep -preserve '{}' \;

2. Use CDN

 

Content Delivery Network (CDN) is a web service to host your magento site external resources: CSS/JS files, images, fonts etc. It aims to serve those files at lot faster than you can with your own server.

Magento supports CDN by default no extensions needed. Go to System > Configuration > Web and set secure/unsecure URLs for media, skin, js resources.

There are many CDN providers to choose from: MaxCDN, KeyCDN, Amazon CloudFront and others.

Optimize Fully Loaded time

 

If you have done start render, first byte and document complete times optimization there is the final step - fully loaded time improvements.

1. Don't abuse jquery(document).ready

 

Choose onLoad events carefully. Don't put CPU intensive tasks in jquery.ready - it slows down page loading.

2. The less third-party javascript the better

 

Examine what third-party javascript files you add to your magento store. Do you really need them?

Well known JS code like google analytics, facebook is all optimized and its performance impact is negligible. Others - use with caution and always test.

 

Implement magento performance tests

 

Include performance benchmarks into your workflow. Every time you add an extension from magento connect or roll out new feature - see what impact it has on site speed. Does it slow magento? Debug or go for another module.

A few tools that help you speed up magento and evaluate its performance (free to use):

1. webpagetest.org - provide you with page loading waterfall charts and tons of other useful information. Supports mobile browsers.

2. Google Chrome Timeline - helps you analyze frontend performance. Takes a little time to get used to but once you are familiar with it gives you valuable insights into why your magento is slow.

3. Google PageSpeed Insights - online tool that tells you what to do to boost site performance. Work on suggestions by google and improve magento speed.

 

Run a Magento 2 store? Check out 3 simple fixes to improve M2 performance.

 

Do you know that faster sites rank better in google? Let speed experts help you get more traffic!

 

Other articles you might be interested in:

  1. How to increase speed of magento website
  2. Magento 1.9.3.1 on HHVM beats PHP7
  3. [Infographic] Why site loading time is important
  4. Magento 2 upgrade to the latest version - 3 simple ways
  5. How to lower TTFB from 8s down to 0.8s

  

Our Magento extensions you might be interested in:

  

If you find this post interesting do not hesitate to sign up for our newsletter and join the 853 people who receive Magento news, tips and tricks regularly.

Thank You!

 

 

5 thoughts on “Magento speed and performance boost in 20 proven steps”
  • Alexander

    Magento is an ecommerce heavy open source platform. It is a big problem for you when your magento store runs slowly and at that time you have to setup it properly.

  • Tej singh

    Tottaly agreed to all the above points foe increased magento page speed but one point we are forgetting is what happens if we clear cache in middle of the day when customers are actually hitting the site, best solution is to have cache warmer that quickly crawls and puts the page back under cache.

  • olivedev

    Some great tips for optimizing Magento's speed. Few more things that you can do to tune the performance are: install Turpentine – Varnish Cache plugin and configure Memcached with Magento 2. You will notice a significant increase in speed.

  • Konstantin Gerasimov

    @Steve - thank you for your comment

    what did you mean by " I’d take care with infrastructure suggestions that are outside Magento’s requirements ( so no HHVM, > 5.4/5/6 on Magento 1.x and so on ). Don’t want to get sued!"?

    I run HHVM with magento 1.x with no problems

    master-slave setup for magento is a great way to speed things up - master for writes, slave for reads - magento supports separate reads/writes databases.

  • Steve Holdoway

    Great to see a well thought out list rather than the usual regurgitated posts from a decade ago! However, I do disagree with a few of these, and have a few suggestions ( well, what do you expect from a grumpy old bugger like me! )...

    - If you split database and web server then you've not only added a new point of failure ( and potential performance problems ), but you now have to build up / tear down TCP connectivity to extract data from the database, rather than ( with a properly configured server ) just pick it out from local memory.

    - it's very unlikely that your site will ever become busy enough to require a master / slave database setup. Also remember that this will slow things down if you're not very careful indeed.

    - there are many more file types that can take advantage of browser caching.

    - domain sharding is pointless with use of a CDN... just hand the heavy lifting over to them and let them sort it out.


    Also...

    - what about http2?

    - I'd take care with infrastructure suggestions that are outside Magento's requirements ( so no HHVM, > 5.4/5/6 on Magento 1.x and so on ). Don't want to get sued!

    - tuning of the OS?

    - and finally what about caching!