Magento 2 TTFB (Time To First Byte) Optimization Guide [Updated 2021]

1. Quickread

Time To First Byte (TTFB) is the time it takes for users browsers to receive the first byte of page content. Once a user requests a browser’s, the browser’s request travels to the server and the server responds back to it. As per Goole PSI standard TTFB should be between 100-200 ms, anything above 200 ms is not good TTFB and there will be some score loss.

2. How to check TTFB?

TTFB can be checked using GTMetrix.Go to https://gtmetrix.com/ enter the page URl you want to check. Make sure that Page Settings on GTMetrix are taken care of. Click on summary and you will find TTFB there as per screen shot
Latest-Performance-Report

3. How does it work?

Once a user requests for the URL in their web browser, a server request is generated to access the information. The request gets processed by the server and the content of the page delivered to the user’s web browser. During this process the server may need to do a lot of work execution to return a page content which users requested.
how-to-work-ttfb

3.1: Sending a request to the server

Following are the factors/parameters to be thought of :
  • 1. DBS lookup
  • 2. Users Network speed (as have no control as a website owner on this)
  • 3. user distance from the server
  • 4. TLS time
  • 5. Blocking time
  • 6. Connection time
  • 7. Time taken by server to execute PHP or deliver from cache
>>So Keep your server in near proximity to your users.
Example if your maximum users are in UK then you should not have your server in US but the server should be in the UK.
>> Use Varnish to reduce PHP execution
>> effectively use cache.

3.2: Processing and generating the response

Once a server receives a request, it has to generate a response.
This involves starting processes,reading cache, making database calls,  and other server level activities.
Note : If varnish is in place then for most of the pages if delivered from cache TTFB will be less then 0.2 seconds.
Just make sure that All blocks were cacheable and Varnish cache hit ratio is high.
Most of the catalog pages and CMS pages are cached in varnish and can be delivered from varnish cache.

3.3: Sending the response back to the client

Once a server generates a response, it needs to transmit it back to the user. This step is dependent on both the enterprise’s connection speed and the user’s connection speed. The TTFB is determined the moment the client begins receiving the response, literally when the client receives the first byte.
You Might Be Interested In: Magento 2 Speed Optimization Services

4. Magento and reasons for an average higher  TTFB

There are a number of things which are causing a higher TTFB. If you are noticing that your page is taking a long time to first byte, then it is important to investigate the issue and make any necessary changes in order to optimize your site’s speed. Below given some of the common reasons why you have higher than normal  Average TTFB:
We are intentionally stating average TTFB as this is ignored but in the real world we shall focus on this( check google analytics you will get average server response time which can  be considered as a parameter directly related to TTFB  )
  • Page not delivered from Varnish Cache ( Magento 2 / Adobe recommends Varnish as a must have )
  • Varnish configured but due to improper coding varnish is by passed( some block cacheable set to false or cache life is less or almost 0 or VLC code results in bypass )
  • Networking issues
  • Poorly configured web server
  • Database configuration/design
  • Slow DNS response time
  • Server to be in user Proximity
  • Load balancing
  • Crons consume too many resources so this would mean that the server does not have sufficient resources to efficiently handle requests.
  • Backend activities that are resource heavy are being done during peak load time.
  • Some modules are consuming resources due to improper coding.
  • Huge collections to be displayed on a page. This would mean in intensive sql queries when collection not in cache.
  • Call to very specific layered navigation that can’t be cached.
  • Too much log creation.
  • XHR and UI components not used effectively.
  • Unwanted blocks and modules were called on the page.
  • Use of securi or some third party modules that hook to the page.

5. Benefits of optimizing time to first byte

  • Users get a website response in less time, improved user experience.
  • Higher customer retention.
  • Increase conversion.
  • Speed improvement.
  • A higher speed also means google ads and face book ads can be less expensive as there ad algorithm charges less if page is good.
  • Better seo ranking

6. How to Reduce Time to First Byte (TTFB)

As we see if TTFB is more then it will impact the SEO ranking Below given techniques are helpful to reduce the TTFB of your store.

Disable unnecessary extensions

Use of unnecessary extensions in your Magento store can slow down the TTFB.
1.Some extensions may generate non cacheable blocks which in turn will result in varnish cache page not being delivered.
2. Extension are not coded all the time with cache and optimization in mind, so a due thought should be given when we install and extension.
3. Some extensions are not in use but as a thought was not given they are kept enabled which results in sql queries and PHP execution.
4. Some extension have lots of nested dependencies this can resulting in lots of memory getting consumed.

Use of Magento caching Solution

Magento 2 provides a built in cache functionality, where you can configure cache and use that. Let’s see how to use Varnish Cache and its benefits.
Varnish is a proxy server. It is useful to deliver content fast. When a visitor requests your site, then this request will not send to Magento, it’s delivered from cache. Varnish checks if visitors request for Home page, Catalog page, CMS page or some cached page.
Varnish  VCL file additionally checks for cookies and additional URL parameters and decides if a cached page can be delivered( taking into account cache expire time )
Magento strongly recommends using Varnish in production. The full-page caching (to either the file system or database) is much slower than Varnish.
To configure Magento to use Varnish log in to the Magento Admin:
  • Go to Stores > Configuration > Advanced > System > Full Page Cache
  • From the Caching Application list, click Varnish Caching
  • Enter a value in the TTL for public content field
  • Expand Varnish Configuration and enter the specific information about your Varnish configuration
Configuration-Settings
Both Redis and Varnish can be used for caching. varnish is a recommended caching option for Magento 2 for full page caching.
Varnish is a challenging tool to configure, but when done right it offers good TTFB.
Another advantage of Varnish ―Its VCL file allows adding page variations based on cookies.
A proper configuration will allow multi language pages delivered from Varnish.
Redis should be used for variable caching or session caching.

Upgrade Magento to the latest version ( 2.4.x)

Recommend to use the latest version of Magento. Magento released the new version with upgraded features, security patches, code standardization so always use the upgraded Magento version for better website speed.
generally we get quarterly Magento upgrade release.
While upgrading one should also check if the modules are compatible or not.
With Magento 2.4.x modules need so to communicate across using APIs this in the long run will mean that upgrades will be very easy provided all the modules installed provides APIs and developers use APIs rather then directly calling classes.
Due to fewer APIs in modules developers have no option but to directly call classes and its functions.
Going forward we see lots of module provider have started providing APIs.

Leverage Browser Caching:

Though this will not have a direct impact on TTFB.
But if a server is busy resolving other static file request then some of its resources will be consumed this in turn will mean in lesser resources for Magento PHP execution.
During peek hours this can result in requests getting queued.
So its a good idea to effectively use browser cache.
using expire header settings this can be achieved.

Use of Lazy Loading/ XHR/ Ui Components :

As is the case of browser caching this too will not have a direct impact on TTFB.
But if we can use some content to be delivered via XHR then the initial page will not need to have those modules executed.
This will mean in initial page being executed quickly which means a better TTFB.
Magento recommends use of UI components and almost all framework like Magento and Shopify are moving in this direction.

Check your logs

1. If something is being written to log that is a warning signal and in most cases when taken care will reduce execution time and resopurces.
2. File write is slower activity and when Magento writes to file its slowing down.

7. Conclusion:

We have detailed  various techniques to improve TTFB. TTFB refers to the first byte of content on your website. If the first byte loads faster it automatically improves the website speed. Speed optimization is must for better conversion, increase in sales, and customer retention. So by using these techniques you can improve the TTFB of your Magento 2 website.
we have worked and seen many practical scenarios and mistakes by developers which has resulted in slow Magento.
We have created multiple blogs to share our practical experience on Magento speed optimization approach and scenarios.
Too get more in-depth idea go through our blogs or contact us and we will provide our consultancy services.
Spending on Magento optimization services is a must do expense as the ROI is pretty high.