2.19.2. Server response time (TTFB)
Server response time (TTFB — Time To First Byte) — the time until the first byte (network packet) of the site page is received after the request is sent from the client (for example, from the browser).
This is a complex indicator, primarily depending on what operations are performed on the server while processing a request. A long response time can be associated with dozens of factors: application logic, slow database operation, routing, software platform, libraries, lack of processor power or memory.
To check that the reason for the increased server response time is the 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 results phpinfo.
If you first measure TTFB for a request that involves the participation of site scripts (usually the main page of the site, if it is dynamic), and then TTFB for a request to a primitive script, then the difference between the results obtained will approximately show the net running time of the scripts.
- Use website technical checkto quickly check the server response time.
- Use PHP profilerto measure the execution time of different sections of the site's code.
You can make sure that the database itself also works quickly by executing primitive queries to it, or at least just checking the opening time of any tables through phpMyAdmin.
Reduced server response time
From the site
To reduce the server response time, the site developer (or an involved third-party specialized specialist) should independently analyze the operations performed on the site when processing requests and explore the possibility of their optimization.
The most direct approach is to log scripts. That is, in the scripts that are executed every time the site is loaded, put checkpoints in which to mark the time it took to process a particular piece of code. Examples of outputting the processing time of the script can be found in open sources on the network (for example, here).
If there are delays associated with database queries, you should:
- Check availability slow queries and if any are present, take steps to optimization.
- Try to identify the most difficult queries and optimize them by analogy with instruction.
- Analyze the structure of the database itself and, if necessary, carry out work on its optimization by analogy with instruction.
From the hosting side
On the hosting side, the following tools can help reduce server response time to some extent:
- PHP version change to a newer one, if the site code is compatible with it (the effect of the transition from PHP 5 to PHP 7 can be especially noticeable).
- Connection and setup of additional services OPcache (caching the compiled byte-code of scripts in RAM, no special support from the site is required).
- Connection and setup of additional services Memcache(d) or Redis (caching arbitrary data in RAM, requires support from the site).