6.16 How to improve TTFB

Banner

Web application speed has become a crucial factor in any site’s success. Users expect instant responses to their actions, and search engines consider performance when ranking sites. One of the key performance indicators is server response time (Time to First Byte, TTFB) – the period between a user sending a request and receiving the first byte of server response.

TTFB is an important indicator of server infrastructure efficiency and can significantly impact overall user experience. Although Google does not officially recognize TTFB as a direct ranking factor, studies show a consistent correlation between low response times and high search results positioning. This is explained by the fact that fast server response time is the foundation for optimal page loading speed overall.

It’s important to understand that TTFB is just one of many factors affecting web application performance. However, its optimization is a critical first step in creating high-performance web systems. In this article, we will examine a comprehensive approach to web application optimization.

Note: Depending on the service, different TTFB optimization methods are possible.

  • On shared hosting, basic caching and GZip compression are available.
  • Premium hosting provides Memcached usage.
  • VPS and dedicated servers allow full control over web server settings and additional software installation.

To ensure maximum site performance, choose hosting with modern high-speed SSD NVMe drives. All our hosting plans use this disk type, which provides significantly higher read and write speeds compared to conventional SSDs.

For dedicated server services, pay attention to the possibility of individual SSD NVMe subsystem selection.

Web Request Processing Chain

The web request processing consists of four key levels, each performing specific functions to ensure efficient web application operation:

1. Client Level: Browser forms an HTTP request based on user actions. Processing efficiency depends on internet connection quality, geographical distance from the server, and user device capabilities. Modern browsers optimize the process through pre-loading resources and local caching.

2. Web Server: Responsible for processing incoming HTTP requests, distributing static content, and managing dynamic requests. Implements basic caching and data transfer optimization. Ensures request routing and interacts with the PHP interpreter. Implements multi-level caching to optimize performance and reduce system load.

3. PHP Interpreter: Executes program code, processes business logic, and forms database queries. Implements PHP-level caching to optimize recurring operations and store intermediate results.

4. Database Server: Provides data storage and access. Optimizes SQL query execution through indexing, result caching, and execution plans. Manages transactions and ensures data integrity.

Scheme

Such a multi-level architecture ensures scalability, reliability, and high performance of web applications. Each level can be optimized separately, allowing flexible system configuration for specific needs.

Caching Levels

Caching is one of the most effective ways to optimize web application performance. It allows storing and quickly retrieving frequently used data, significantly reducing server load and system response time.

PHP Interpreter Level Caching

Two main caching mechanisms are used at the PHP interpreter level:

  1. Memcached/Redis (In-Memory Caching):

    • Storing data directly in RAM.
    • Ultra-fast data access (microseconds).
    • Distributed caching.
    • Ideal for frequently requested and dynamic data.
  2. File Cache:

    • Storing data on disk.
    • Slower access compared to Memcached/Redis.
    • Suitable for large-volume data.
    • Simpler to configure.

Effectiveness Comparison:

Memcached/Redis

Advantages:

  • Ultra-fast data access.
  • Distributed storage.
  • Support for complex data structures.
  • High scalability.

Disadvantages:

  • Fixed RAM size.
  • Risk of data loss during overload.
  • Requires additional infrastructure.
  • Significant RAM consumption.
File Cache

Advantages:

  • Easy to implement.
  • Unlimited memory volume.
  • Low system load.
  • No additional configuration required.

Disadvantages:

  • Much slower compared to in-memory caching.
  • Dependent on hard drive performance.
  • Additional time for file read/write operations.
  • Constant read/write operations.

CMS Configuration: Modern CMS provide flexible caching settings. You can choose a specific caching type or configure a hybrid type.

Functionality that plugins or built-in functionality can provide:

  • Choosing caching type (Memcached/Redis or file-based).
  • Setting cache storage periods.
  • Ability to partially or fully reset the cache.

Web Server Level Caching

Web server level uses static caching with mechanisms:

  • Static file caching.
  • Content compression (GZIP).

Features:

  • Caching images, CSS, JavaScript.
  • Setting long storage periods (1, 7, 14, 30, 90, 366 days).
  • Adding headers for cache control.
  • Reducing server load.
  • Accelerating static content delivery.

TTFB Measurement Tools

Several tools exist for measuring TTFB, including:

  • Chrome DevTools
  • Google PageSpeed Insights
  • GTmetrix
  • WebPageTest

The simplest way to check Your site’s TTFB is using Chrome’s built-in developer tools:

  1. Open Chrome DevTools by pressing F12 or using Ctrl + Shift + I.
  2. Go to the Network tab. Select the Doc filter at the top.
  3. Check Disable cache to prevent using cached data.
  4. Open the site in the browser address bar or reload the page.
  5. Find your site’s document in the request list.
  6. Go to the Timing tab. Waiting for server response (TTFB) shows server response time.
  7. For more accurate analysis, reload the page several times and calculate the average TTFB.

Google dev tools

Note: Developer tools (DevTools) are available in all popular browsers, including Google Chrome, Safari, Mozilla Firefox, Opera, and Microsoft Edge. Different keyboard shortcuts may be used to open DevTools in different browsers.

Content and Media File Optimization

Image Optimization

Images

Graphic content as the art of technical efficiency. Images are not just illustrations, but a powerful communication tool that requires a professional technical approach.

Technical mechanisms for image optimization include several transformation levels:
  1. Formatting

    • WebP: reducing file size up to 30% compared to PNG/JPEG.
    • Quality loss less than 5% during compression.
    • Transparency and animation support.
  2. Intelligent Compression

    • Adaptive compression algorithms (e.g., mozjpeg).
    • Preserving image meta-information.
    • Removing redundant service data.
  3. Adaptive Delivery

    • Generating multiple image versions.
    • Using srcset for automatic selection.
    • Supporting different pixel densities (1x, 2x, 3x).

Modern optimization methods transform bulky files into light and fast web page elements.

Lazy Loading Implementation

Loading

Intelligent content loading technology. The concept of deferred loading is not just a technical technique, but a true philosophy of efficient internet resource use. Instead of an instant “explosion” of content, the page breathes, gradually revealing itself.

Implementation Mechanisms:
  1. Native Browser Support

    • loading="lazy" attribute.
    • Support in over 90% of modern browsers.
    • Minimal settings.
  2. JavaScript Libraries

    • IntersectionObserver API.
    • Libraries like Lozad.js, lazysizes.
    • Additional customization options.
  3. Efficiency Metrics

    • Reducing Initial Load time up to 40%.
    • Reducing traffic usage.
    • Improving Core Web Vitals metrics.
Key Advantages:
  • Instant primary loading
  • Reducing channel load
  • Saving device energy resources

Video Optimization

Video

Video is the most complex media format. It’s not just a sequence of frames, but a complex ecosystem of technical solutions designed to provide the best quality with minimal resource expenditure.

Strategic Approaches:
  1. Encoding

    • Using H.264/H.265.
    • Configuring variable bitrate transmission.
    • Supporting multiple quality profiles.
  2. Adaptive Streaming

    • HLS (HTTP Live Streaming).
    • MPEG-DASH.
    • Automatic quality selection.
  3. Preprocessing

    • Generating preview images.
    • Precaching metadata.
    • Minimal preview size.

Video Optimization

Video

Video is the most complex media format. It’s not just a sequence of frames, but a complex ecosystem of technical solutions designed to provide the best quality with minimal resource expenditure.

Strategic Approaches:
  1. Encoding

    • Using H.264/H.265.
    • Configuring variable bitrate transmission.
    • Supporting multiple quality profiles.
  2. Adaptive Streaming

    • HLS (HTTP Live Streaming).
    • MPEG-DASH.
    • Automatic quality selection.
  3. Preprocessing

    • Generating preview images.
    • Precaching metadata.
    • Minimal preview size.

Code and Resource Optimization

Technical excellence is not just beautiful code, but its maximum functionality and performance.

JavaScript and CSS Optimization

Code

Code is not just instructions, but a complex interaction system where every byte matters. Modern practices transform bulky scripts into elegantly tuned mechanisms.

Optimization Methodologies:
  1. Minimization

    • Removing comments and spaces.
    • Combining files.
    • Using source maps.
  2. Code Splitting

    • Webpack, Rollup, Parcel.
    • Dynamic module loading.
    • Reducing initial bundle.
  3. Critical Rendering

    • Embedding critical CSS.
    • Asynchronous script loading.
    • Content prioritization.

Database Optimization

Optimization

A database is the heart of any information system. Its optimization is not a technical procedure, but an art of balancing speed, reliability, and scalability.

Optimization Directions:
  1. Data Structure

    • Normalization and denormalization.
    • Selecting optimal types.
    • Redundancy control.
  2. Indexing

    • B-Tree, Hash indexes.
    • Partial indexes.
    • Composite indexes.
  3. Query Caching

    • Query result cache.
    • Analyzing frequently called queries.
    • Preprocessing data.

Advanced SQL Query Optimization

Advanced

SQL query optimization is a key performance factor for any web application working with databases. Properly constructed queries can significantly speed up the program and reduce server load.

It’s important to adhere to basic optimization principles, such as creating effective indexes and minimizing the number of table joins. Avoiding common mistakes, like using functions in WHERE conditions, helps ensure stable and fast application performance.

Optimization Includes:

Basic Optimization Principles:

  • Creating correct indexes.
  • Selecting only necessary data.
  • Avoiding excessive table joins.
  • Optimizing WHERE conditions.

Avoiding Common Mistakes

  • Refusing to use functions on the left side of operators.
  • Minimizing correlation subqueries.
  • Proper use of JOIN operations.
  • Optimizing complex filtering conditions.

Each section demonstrates that optimization is not just a technical procedure, but an art of balancing performance, reliability, and usability.