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

Product was successfully added to your shopping cart.



How to lower TTFB from 8s down to 0.8s - Real Website Example

Time to first byte (TTFB) shows how quick a server responds to a browser query. TTFB optimization is important to speed up Magento website. Good TTFB should be under 1s.

Today I will show you how I managed to decrease TTFB of a real website from 8s down to 0.8s.

8s TTFB is a sure sign of poorly written php code.

I reduced TTFB 10 times - isn't it great?

Preparation

First I installed AOE_Profiler extension to see what is going on inside Magento website. You can use default Magento profiler but this AOE plugin makes it easy to track unoptimized php code and slow DB queries. Plus it keeps statistics so you can always see what has improved.

8s to 0.8s TTFB optimization | AOE_Profiler | Goivvy.com 8s to 0.8s TTFB optimization | AOE_Profiler | Goivvy.com

Real Website TTFB Optimization

I will hide website url for security reasons because I am going to reveal some code details here. You just have to believe me this is a real live website.

The website hosting server specifications: Godaddy VPS Linux Centros 6 4GB w cPanel. The site is a Magento CE 1.9.2.3 website that has poor TTFB - up to 8s!

8s to 0.8s TTFB optimization | Homepage Waterfall | Goivvy.com 8s to 0.8s TTFB optimization | Homepage Waterfall | Goivvy.com

We would be using http://www.webpagetest.org/ for TTFB check.

I would start with optimizing Homepage.

1. TTFB Homepage

Homepage Profiler waterfall looks like this:

8s to 0.8s TTFB optimization | Homepage Profiler Waterfall | Goivvy.com 8s to 0.8s TTFB optimization | Homepage Profiler Waterfall | Goivvy.com

I see frontend/sm-shoppystore/default/template/sm/listingtabs/default.phtml template is taking 6s to load - something is definitely going on!

Further research showed that listingtabs/default.phtml file is a part of a plugin that shows 'Top Selling Items by Category' block on the homepage:

8s to 0.8s TTFB optimization | Time consuming block | Goivvy.com 8s to 0.8s TTFB optimization | Time consuming block | Goivvy.com

Let's see if it could be optimized.

Getting and processing bestseller items could be time consuming so the only option would be to cache that block and refresh it every time new sale is made.

In homepage xml:

     <block type="listingtabs/list" name="listingtabs.list.default" template="sm/listingtabs/default.phtml">
            <action method="setCacheLifetime">
                <lifetime>36000</lifetime>
            </action>
     </block>

 

Refresh every new sales is made code....

We were able to cut homepage TTFB down to 2s:

8s to 0.8s TTFB optimization | Layout Generate Blocks | Goivvy.com 8s to 0.8s TTFB optimization | Layout Generate Blocks | Goivvy.com

Let's look at layout_generate_blocks call for now - it takes 1.4s!

We should inspect call $this->getLayout()->generateBlocks(); in app/code/core/Mage/Core/Controller/Varien/Action.php:

8s to 0.8s TTFB optimization | GenerateBlock call | Goivvy.com 8s to 0.8s TTFB optimization | GenerateBlock call | Goivvy.com

Going deeper into code base we see basicproducts.list.default2 block initializing _config variable on every instance:

8s to 0.8s TTFB optimization | _config variable | Goivvy.com 8s to 0.8s TTFB optimization | _config variable | Goivvy.com

let us cache this variable in this block along with other similar blocks:

8s to 0.8s TTFB optimization | cache _config variable | Goivvy.com 8s to 0.8s TTFB optimization | cache _config variable | Goivvy.com

Now Homepage TTFB is down to 0.5s:

8s to 0.8s TTFB optimization | 0.5s Homepage TTFB | Goivvy.com 8s to 0.8s TTFB optimization | 0.5s Homepage TTFB | Goivvy.com

Maybe we can do better? lets inspect the waterfall:

8s to 0.8s TTFB optimization | Homepage Waterfall | Goivvy.com 8s to 0.8s TTFB optimization | Homepage Waterfall | Goivvy.com

I inspected related templates but unfortunately nothing could be removed/optimized - all looked ok.

Homepage Final waterfall shows TTFB at 0.7s:

8s to 0.8s TTFB optimization | Homepage Final Waterfall | Goivvy.com 8s to 0.8s TTFB optimization | Homepage Final Waterfall | Goivvy.com

 

2. TTFB Category Page

Let us see a category waterfall:

8s to 0.8s TTFB optimization | Category Waterfall | Goivvy.com 8s to 0.8s TTFB optimization | Category Waterfall | Goivvy.com

We see TTFB at 1.5s.

Lets inspect category/view.phtml and product/list.phtml templates:

8s to 0.8s TTFB optimization | view.phtml and list.phtml templates | Goivvy.com 8s to 0.8s TTFB optimization | view.phtml and list.phtml templates | Goivvy.com

Inspection shows that inside foreach loop over product collection developer loads product models (((:

8s to 0.8s TTFB optimization | Inside loop developer loads product models | Goivvy.com 8s to 0.8s TTFB optimization | Inside loop developer loads product models | Goivvy.com

That is a common mistake. Magento product collection already has all the data loaded - no need to load product model!!!

We change it to:

8s to 0.8s TTFB optimization | unneeded product model loads removed | Goivvy.com 8s to 0.8s TTFB optimization | unneeded product model loads removed | Goivvy.com

Inspecting view.phtml we see this ugly code:

8s to 0.8s TTFB optimization | category view.phtml ugly code | Goivvy.com 8s to 0.8s TTFB optimization | category view.phtml ugly code | Goivvy.com

Even though html is commented out magento is still processing function calls. Let us delete this code!

After all category related optimization we now have TTFB at 0.79s! We managed to reduce TTFB by 50%!

8s to 0.8s TTFB optimization | optimized category TTFB | Goivvy.com 8s to 0.8s TTFB optimization | optimized category TTFB | Goivvy.com

Category Page final waterfall shows TTFB at 1s:

8s to 0.8s TTFB optimization | Category Page Final Waterfall | Goivvy.com 8s to 0.8s TTFB optimization | Category Page Final Waterfall | Goivvy.com

 

3. TTFB Product Page

After above optimizations we have product page TTFB at 0.8s. Not bad!

8s to 0.8s TTFB optimization | Product Page Initial TTFB | Goivvy.com 8s to 0.8s TTFB optimization | Product Page Initial TTFB | Goivvy.com

Could it be improved? Lets inspect product view.phtml template:

8s to 0.8s TTFB optimization | Product Page view.phtml | Goivvy.com 8s to 0.8s TTFB optimization | Product Page view.phtml | Goivvy.com

Again we see product model load:

8s to 0.8s TTFB optimization | Product Model Load | Goivvy.com 8s to 0.8s TTFB optimization | Product Model Load | Goivvy.com

$_product has all the necessary information!

8s to 0.8s TTFB optimization | product load commented out | Goivvy.com 8s to 0.8s TTFB optimization | product load commented out | Goivvy.com

TTFB dropped down to 851ms.

8s to 0.8s TTFB optimization | Product Page TTFB at 851ms | Goivvy.com 8s to 0.8s TTFB optimization | Product Page TTFB at 851ms | Goivvy.com

Product Page final waterfall shows TTFB at 1.1s:

8s to 0.8s TTFB optimization | Product Page Final Waterfall | Goivvy.com 8s to 0.8s TTFB optimization | Product Page Final Waterfall | Goivvy.com

 

4. TTFB Checkout Cart Page

Checkout Cart page TTFB is 0.8s.

8s to 0.8s TTFB optimization | Checkout Cart Page TTFB | Goivvy.com 8s to 0.8s TTFB optimization | Checkout Cart Page TTFB | Goivvy.com

I checked and it seems that nothing could be improved. Let's leave it as it is.

Checkout cart page final waterfall (with no products added) shows TTFB at 0.8s:

8s to 0.8s TTFB optimization | Checkout Cart Page Final Waterfall | Goivvy.com 8s to 0.8s TTFB optimization | Checkout Cart Page Final Waterfall | Goivvy.com

 

5. TTFB Cms page

I didn't need to optimize CMS page - with all our tweaks I had done before it already had TTFB at 0.8s. Nice!

8s to 0.8s TTFB optimization | CMS Page Final Waterfall | Goivvy.com 8s to 0.8s TTFB optimization | CMS Page Final Waterfall | Goivvy.com

 

TTFB Performance Optimization Summary

Time to first byte could be reduced by php code audit. Magento is a complex system and poor coding could have dramatic impact on a website performance.

Magento developers have some common mistakes that affect a site speed:

  • load models inside a loop
  • not storing reusable variables inside Magento cache
  • not utilizing block cache functionality

Code Magento with performance in mind and your or your client's website will be fast with low TTFB.

If you have any suggestions of your own on how to improve Magento time to first byte - please don't hesitate to share them in comment section below!

 

 

 

 

 

 

 

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

Thank You!

 

 

One thought on “How to lower TTFB from 8s down to 0.8s - Real Website Example”

  • Peter Gustafson
    Peter Gustafson May 30, 2016 at 1:17 pm

    Great post. Congratulations on the speed enhancements. I know you folks are the best in the business and it's great to see you sharing client case studies!