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?
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.
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 220.127.116.11 website that has poor TTFB - up to 8s!
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:
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:
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:
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:
Going deeper into code base we see basicproducts.list.default2 block initializing _config variable on every instance:
let us cache this variable in this block along with other similar blocks:
Now Homepage TTFB is down to 0.5s:
Maybe we can do better? lets inspect the waterfall:
I inspected related templates but unfortunately nothing could be removed/optimized - all looked ok.
Homepage Final waterfall shows TTFB at 0.7s:
2. TTFB Category Page
Let us see a category waterfall:
We see TTFB at 1.5s.
Lets inspect category/view.phtml and product/list.phtml templates:
Inspection shows that inside foreach loop over product collection developer loads product models (((:
That is a common mistake. Magento product collection already has all the data loaded - no need to load product model!!!
We change it to:
Inspecting view.phtml we see this ugly code:
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%!
Category Page final waterfall shows TTFB at 1s:
3. TTFB Product Page
After above optimizations we have product page TTFB at 0.8s. Not bad!
Could it be improved? Lets inspect product view.phtml template:
Again we see product model load:
$_product has all the necessary information!
TTFB dropped down to 851ms.
Product Page final waterfall shows TTFB at 1.1s:
4. TTFB Checkout Cart Page
Checkout Cart page TTFB is 0.8s.
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:
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!
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 650 people who receive Magento news, tips and tricks regularly.