2.17.2.2. Server response time (TTFB)
Server response time (TTFB — Time To First Byte) — the time until the first byte (network packet) of a site page is received after a request is sent from the client side (e.g., from a browser).
This is a complex indicator, primarily depending on what operations are performed on the server during request processing. High response time can be due to dozens of factors: app logic, slow database operation, routing, software platform, libraries, lack of processor power or memory.
Analysis
To check that the cause of increased server response time are internal processes of the site (scripts, database queries) and not general problems with the server, the easiest and fastest way is to use the most primitive PHP script, for example, displaying the results of phpinfo.
If you first measure TTFB for a request that involves site scripts (usually the main page of the site if it is dynamic) and then TTFB for a request to a primitive script, the difference between the results will roughly show the net time of scripts.
Tips:
- Use site technical check to quickly check the server response time.
- Use PHP profiler to measure the execution time of different sections of the site code.
You can make sure that the database itself also works fast by running primitive queries against it, or at least by simply checking the time of opening any tables using phpMyAdmin.
Reducing server response time
Site-side
To reduce the server response time, the site developer (or a third-party specialist) should independently analyze the operations performed on the site when processing requests, and explore the possibility of optimizing them.
The most direct approach is logging of scripts. That is, in the scripts that are executed each time the site is loaded, set checkpoints in which to mark the time it took to process this or that code section. Examples of outputting script processing time can be found in open sources on the web (for example, here).
If there are delays associated with database queries, you should:
- Check for slow queries and if present, take steps to optimize them.
- Try to identify the heaviest queries and optimize them similar to instruction.
- Analyze the structure of the database itself and, if necessary, work on its optimization similar to instruction.
Hosting-side
On the hosting side, the following tools can help to reduce server response time to some extent:
- Changing PHP version to a newer version if the site code is compatible with it (especially the effect of upgrading from PHP 5 to PHP 7 can be noticeable)
- Connecting and configuring the extra service OPcache (caching of compiled bytecode of scripts in RAM, no special support from the site is required).
- Connecting and configuring extra service Memcache(d) or Redis (caching arbitrary data in RAM, requires support from the site).