TTFB stands for time to first byte. This metric reflects how fast a user starts to receive the content after they’ve opened a website.
Server response time is one of the crucial web performance indicators which contributes to reaching a better PageSpeed Insights score. Store owners know perfectly well why they should aspire to the highest grade: to enhance both Google rankings and the customer experience.
Working on reducing TTFB is a complex task. There could be numerous reasons affecting it linked with a web server, caching strategy, database configuration, and so forth. In this article, we’ll define major issues and ways to improve TTFB for Magento stores.
The Good, the Bad, and the Ugly TTFB
When a person opens a website, 200+ database queries, API calls, PHP operations, and other processes start immediately. Put simply, three major tasks together make time to first byte:
- a request is sent to the server;
- the request is processing; a response is generating;
- the response is fetched to the client.
As soon as the user gets the very first byte of content, the “stopwatch” stops.
So, what is good TTFB? According to Google, you should aim for 200 milliseconds (or 0.2 seconds) and less. If Magento server response time is under 600 ms, it’s still displayed in the “Passed audits” section of PageSpeed Insights.
But once time goes above that mark, it’s a red flag: you’ll see the metric in the “Opportunities” section. So, it’s time to start improving your site’s loading speed since it influences other crucial metrics. You also have to take care of the user experience: if customers wait even an extra second, this negatively affects conversions.
In the meantime, some settings can increase TTFB to an unbelievable size. In our experience, the worst seen time to first byte was 21 seconds! The reason behind such a horrendous performance was the server that made queries to another server to download images every time somebody opened that website.
Of course, the described case is an exception, but even a one-second delay does substantial harm. Google states the likelihood of bounce grows by 32% as page load time increases from 1 to 3 seconds. That’s why it’s essential to find and get rid of pitfalls hindering your store from faster loading.
TTFB and Core Web Vitals
What about TTFB’s effects on your store’s performance score? Let’s turn to Google PageSpeed Insights again. The report advises the following:
“Keep the server response time for the main document short because all other requests depend on it (FCP, LCP).”
You are probably aware of FCP, LCP, and a couple of other Web Vitals. These are indicators used by Google to measure web performance from the UX perspective. The approach considers several aspects that hugely influence the user experience: loading, interactivity, and visual stability.
- First Contentful Paint (FCP) is the amount of time between the start of page loading and the appearance of any piece of content on the screen: logo, text, image, or anything else.
- Largest Contentful Paint (LCP) is the time needed for the largest above-the-fold image, text, or video to render.
Both metrics reflect loading performance. Among other parameters, they depend on the server responsiveness: Magento’s slow server response time leads to worse CWV scores. When they’re calculated, TTFB is taken into account. For instance, if the content appears in 900 ms, but TTFB is one second, FCP will be 1.9 seconds and fail the Core Web Vitals Assessment.
In the screenshot below, we see a different picture. TTFB is 120 ms, then loading some “invisible” site resources (JS, fonts, and styles) blocks FCP presumably for 800-900 ms. Finally, it takes up to 200 ms to load the first visible pieces of content.
So TTFB optimization leads to improvements in other key indicators. You can find more on Magento Core Web Vitals optimization in our recent guide.
How to Measure TTFB
There are a bunch of ways to check your online store’s time to first byte:
- Lab options like Lighthouse, Gtmetrix, or Webpagetest (lab tools make assessments in ideal conditions: a particular server and good internet connection);
- Google PageSpeed Insights (provides lab as well as field data, which is collected from real users);
- Field options: the Chrome User Experience Report or Google Search Console;
- Browsers can also display TTFB;
- Developers evaluate TTFB with the help of profilers as the most precise tools. They measure the script processing speed and show how much time each operation took, giving insights into what to fix. We’ll talk about profilers further.
We recommend taking measurements using a few instruments and comparing reports to obtain a comprehensive idea of your server responsiveness. When it comes to eCommerce sites, it’s obligatory to test the homepage, category, product pages, cart, and checkout.
Besides this, it’s important to assess TTFB with server caching (full page and block caching) turned off to get objective information.
What Slows Down TTFB on Magento?
Slow Magento response time is more than a widespread issue for online stores. Let’s define the most common contributors:
- Unoptimized code: heavy PHP logic, slow and/or excessive MySQL queries, unnecessary code, absence of caching on the code level.
- Unoptimized web server: poor configuration, disabled compression (for instance, Gzip), non-optimal server location.
- Absence of backend caching: without it, a store can’t cache some requests, chunks of the code, code logs, and HTML files in advance to retrieve them faster.
- Unoptimized database: poor queries or caching configuration, absence of indexes, and so on.
There can be more reasons for the time to first byte increase. Let’s discuss how you can find such problems.
How to Find Bottlenecks Affecting TTFB
The first step to reduce Magento 2 server response time efficiently is conducting a profound audit. Here different profilers come in handy as a developer has to check endless code lines in terms of execution time.
There are two groups of profilers:
a) Magento modules: Magento in-built profiler, Mirasvit Profiler, and MGT Developer Toolbar. All these tools examine basic queries and can’t boast great detailing.
b) External tools: XHProf and Xdebug Profiler. Roughly, via Magento modules, we’ll see 100 calls, while these instruments showcase 1 000 000. There are entire stack traces with all calls. Most of them take fractions of milliseconds, and a developer skips them, concentrating and studying inadequately long ones.
We’ve covered the matter of profiling deeper in our Magento performance optimization guide.
How to Reduce Server Response Time on Magento: Top 4 Steps
TTFB optimization is a complex task consisting of multiple subtasks. Let’s outline the scope of work. The good news is, most of the measures mentioned below are part of the general Magento speed optimization.
1. PHP Optimization
- The very first step for Magento stores to improve server response time is to rethink slow PHP code chunks so that they execute faster as well as reduce the overall number of operations.
- Reduce the work with files. For instance, many modules have a debug mode that is accountable for logging. Turn it off to avoid the overproduction of log files.
- Be careful when using PHP modules like Xdebug Profiler. It’s able to slow down the whole website even if it isn’t in use but enabled (we’ve seen a 5x slowdown). It’s vital to keep it disabled when your website is live.
- Update PHP to the latest version and configure it in line with the platform’s requirements.
2. Web Server Optimization
- Implement HTTP/2. Many Magento stores still use HTTP/1. But unlike it, HTTP/2 is able to handle requests not one by one but in parallel. Moreover, developers can prioritize more important queries over others.
- Configure browser caching to keep static content (fonts, images) in users’ browsers for a longer period (for instance, a year). It helps to reduce the number of queries to the server and accelerates content loading.
- Enable Gzip compression to archive content prior to transferring it to users. It will speed up the process.
3. Leveraging Caching Solutions
What definitely makes Magento 2 server response time slow is overlooking caching which is an irreplaceable helper for boosting TTFB. Apply at least these strategies in your online store:
- Full-page caching (FPC). It’s used to cache all pages.
- Block caching: for example, cache the header and footer.
- Static content caching for images, fonts, JS, and CSS files.
Use the combination of several solutions to reach the needed result:
- Redis and Varnish. Redis is a universal tool for any type of caching, while Varnish can replace Redis for FPC. In practice, these two solutions are sometimes used together, complementing each other and ensuring fast speed even if something goes wrong with one of the tools.
- CDN (Content delivery network). This service caches static content and retrieves it from the server closest to the client. It significantly unloads the store’s server and accelerates all processes, including TTFB. As a result, people receive content quicker.
4. MySQL Optimization
- Enable caching for queries returning the same results every time.
- Configure indexes to make the database search work faster.
- Rethink large database queries to make them execute quicker.
Check more tactics of MySQL optimization in our recent guide.
Here is an example from the Magento store of one of Onilab’s clients. In the first screenshot from the Mirasvit profiler, you can see the “Total time” indicator: TTFB is about 4.5 seconds, including 123 milliseconds for database queries. It’s quite slow and needs optimization.
The second screenshot reflects a far better performance: 756.5 milliseconds in total and just 18 ms for database queries. What have we done? First, we rewrote database queries and got rid of heavy logic (optimized cumbersome calculations). Secondly, we turned on some caching. If we work with caching profoundly, TTFB will decrease even more.
7 More Recommendations on Improving TTFB
To reduce server response time (TTFB) on Magento 2 further, we recommend considering the following improvements:
1. Make sure your server is capable of handling your ongoing traffic
TTFB can lengthen due to the shortage of server resources (e.g., RAM). Consider upgrading the server and\or use load balancing between two servers.
2. Is the used hosting enough to support your store?
Some providers save data on CPU, hard drives, and RAM. We advise you to opt for a faster SSD instead of an outdated HDD. Plus, look at the number of cores in CPU (min 4, but the more, the better). And you can also buy more RAM (min 4 GB for a mid-size store and 16 or 32 GB for a large one).
3. Implement Lazy Loading for particular blocks
Basically, this practice is applied to Magento image optimization, but you can leverage it for other cases. For instance, you have one of the blocks placed outside the above-the-fold area that takes much server time to load. Then you can defer loading it until a user starts scrolling.
4. Revise the extensions
There could be various unused or rarely used and ineffective modules. It’s better to disable them since they complicate the logic.
5. Avoid excessive logging in production mode
Constant logging additionally slows down working with the file system, which is not fast itself. A good practice is to log only critical mistakes for further analysis.
6. Upgrade your Magento to the latest available version
As of December 2021, this is Magento 2.4.3. Magento 2 TTFB optimization is way easier than, e.g., Magento 1.9 TTFB optimization. Each subsequent version fixes some bugs of the previous one and requires newer module versions (PHP and others) that also perform faster than before. Switching to the latest software is a proven method to speed up Magento backend on the whole.
7. Monitor the execution speed at the coding (code review) stage
Last but not least, examine the speed of modules right after installation and optimize if needed.
These are primary tips on how to reduce server response time on Magento. For more details, read our guide on Magento mobile speed optimization. We’ve also covered how to enhance other core performance indicators in the Magento 2 PageSpeed Insights optimization guide.
To Sum Up
Magento TTFB optimization requires a lot of attention and diligent work. But don’t let that demotivate you. First and foremost, these optimization practices favorably affect not only server responsiveness but also other performance indicators. And secondly, Onilab’s development team and our Magento optimization services are at your disposal.