Ana Sayfa / Blog / TTFB optimize etme: server-side ve database katmanı

TTFB optimize etme: server-side ve database katmanı

Time to First Byte (TTFB) SEO metric'i. 800ms altı hedef. Server-side optimization'un concrete adımları.

TTFB (Time to First Byte) user browser’ın request gönderdiği andan server’ın ilk byte response’unu aldığı ana kadar geçen süre. User experience’in temel metric’i, Google Core Web Vitals’in content load metric’lerinin prefix’i.

İyi TTFB: 800ms altı. Poor: 1800ms üstü. 19 yıllık WordPress optimization deneyimim, TTFB’yi tipik olarak 2-3s’den 400ms’ye indirdiğim projelerden notlar.

TTFB components

TTFB aslında 3 alt-metric’in toplamı:

  1. DNS lookup: Domain IP’ye çözülüyor (genelde cached, <50ms)
  2. TCP + TLS handshake: Connection kuruluyor (150-300ms, initial)
  3. Server processing time: Request’e cevap üretiyor (çoğu TTFB bu)

Optimization’un %80’i server processing time’a odaklanıyor.

Server-side measurement

Backend’de TTFB parçalarını ölç:

// WordPress örnek
add_action('shutdown', function() {
    $ttfb = (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) * 1000;
    error_log("TTFB: {$ttfb}ms");
});

Daha detaylı profiling: New Relic, Datadog APM. Request’in her phase’ini (DB, cache, external API) breakdown.

Database query optimization

TTFB’nin %50-70’i genelde DB.

Common issues:

1. N+1 queries. List gösterirken her item için ek query.

// BAD
$posts = get_posts(['posts_per_page' => 20]);
foreach ($posts as $post) {
    $author = get_user_by('id', $post->post_author);  // Her post için 1 query
}
// 21 query toplam

// GOOD
$posts = get_posts(['posts_per_page' => 20]);
$author_ids = array_unique(array_column($posts, 'post_author'));
$authors = get_users(['include' => $author_ids]);
// 2 query toplam

2. Missing indexes. Query slow çünkü full table scan.

EXPLAIN SELECT * FROM wp_postmeta WHERE meta_key = 'popular' AND meta_value = 'yes';
-- type: ALL (full scan) = bad
-- type: ref (index used) = good

CREATE INDEX idx_meta_key_value ON wp_postmeta (meta_key, meta_value(20));

3. Heavy JOIN’ler. 5-table JOIN slow.

Denormalize veya materialized view.

4. autoload option bloat. WordPress’te wp_options tablosunun autoload=yes satırları her request’te load.

SELECT option_name, LENGTH(option_value) FROM wp_options WHERE autoload = 'yes' ORDER BY LENGTH(option_value) DESC LIMIT 20;

1MB+ autoload option’lar TTFB’yi pahalıya mal ediyor. Gereksizi autoload=no yap.

PHP-level optimization

OPcache enable:

PHP OPcache every request’te parse etmemeyi sağlıyor. php.ini:

opcache.enable=1
opcache.memory_consumption=256
opcache.max_accelerated_files=10000
opcache.validate_timestamps=0  // production'da

Production’da validate_timestamps=0 ile file change check’i atlanıyor. Deploy sonrası opcache_reset() manuel.

OPcache preloading (PHP 7.4+):

// preload.php
opcache_compile_file(__DIR__ . '/wp-settings.php');
opcache_compile_file(__DIR__ . '/wp-includes/class-wp.php');
// ...

Bu file php.ini‘de opcache.preload=/path/preload.php. Boot time’da hot files preload.

JIT compilation (PHP 8+):

opcache.jit_buffer_size=100M
opcache.jit=tracing

%5-15 performance gain CPU-heavy workload’larda.

Caching layers

1. Object cache.

PHP object’leri request’ler arasında cache. Redis veya Memcached.

WordPress için: wp-content/object-cache.php drop-in. Redis Object Cache plugin.

Hemen %30-50 DB load reduction.

2. Page cache.

Rendered HTML cache. Static file veya Redis.

LiteSpeed Cache, WP Rocket, W3 Total Cache. Anonymous user için cached HTML, logged-in için bypass.

TTFB’de 1-2s’den 50ms’ye düşüş tipik.

3. Database query cache.

Repeated identical query’leri cache. MySQL query cache deprecated (5.7+), app-level caching.

$cache_key = 'top_posts_' . date('Y-m-d-H');
$top = wp_cache_get($cache_key);
if ($top === false) {
    $top = expensive_query();
    wp_cache_set($cache_key, $top, '', 300);
}

4. Full-page cache at CDN.

Cloudflare, Fastly edge caching. Content origin’e ulaşmıyor. CDN POP’undan serve.

Static content için TTFB <100ms globally.

Server infrastructure

Hardware matters:

  • SSD vs HDD: SSD 10x faster I/O. Günümüzde SSD standart.
  • RAM: DB’in working set memory’de kalsın. Page cache + query cache + app memory.
  • CPU cores: PHP-FPM worker’lar parallel. Cores × 2 worker tipik formül.

LiteSpeed vs Nginx vs Apache:

LiteSpeed performance’ta en iyisi, lisans ücreti.
Nginx ikinci, ücretsiz, good community.
Apache legacy, .htaccess convenience.

Benim tercihim: LiteSpeed if budget, Nginx if not.

External service dependencies

Plugin’lerin external API call’ları TTFB’yi büyütüyor:

  • Google Fonts API
  • Gravatar API
  • Analytics endpoint’leri
  • Third-party license check

Fix:
– Cache remote response’ları aggressive
– Async/background fetch (WP Cron)
– Local fallback

Bir projede 7 farklı external API call frontend TTFB’ye ekleniyordu. Her biri 200-500ms. Caching ile total 1.5s kazanım.

WordPress-specific quick wins

1. Autoload cleanup.

SELECT SUM(LENGTH(option_value)) FROM wp_options WHERE autoload = 'yes';
-- Total autoload size. >1MB ise cleanup gerekli.

2. wp_options bloat detection.
Plugin’ler ekledikleri transient’leri temizlemiyor. Milyonlarca satır birikiyor.

SELECT COUNT(*) FROM wp_options WHERE option_name LIKE '_transient_%';

10K+ ise cleanup. WP-CLI: wp transient delete --all.

3. Query Monitor plugin.
Dev/staging’de aktif. Her request’in query’lerini, memory, TTFB breakdown gösteriyor.

4. Disable wp-cron, use real cron.
wp-config.php:

define('DISABLE_WP_CRON', true);

System cron:

*/5 * * * * wget -q -O /dev/null https://site.com/wp-cron.php

wp-cron her page load’da tetikleniyor, random scheduled task’lar TTFB’yi boğuyor. Real cron ile isolated.

5. Heartbeat API throttle.
Admin’de her 15 saniyede Heartbeat ping. Production’da saniyede dakikalara gevşet. Heartbeat Control plugin.

Monitoring

Production’da sürekli TTFB track:

  • Real User Monitoring (RUM): web-vitals.js library, actual users.
  • Synthetic monitoring: Pingdom, UptimeRobot periodic checks.
  • Backend APM: New Relic, Datadog breakdown by endpoint.

Alert: p95 TTFB > 1.5s ise anomaly.

Sonuç

TTFB server-side processing time’a odaklanıyor. DB query optimization, PHP OPcache, caching layers, infrastructure choice. Her biri cumulative effect.

WordPress-specific quick wins: autoload cleanup, wp-cron fix, heartbeat throttle, query monitor profiling. 1 günlük iş çoğu site’da 1-2s kazanım.

Hedef: 800ms altı TTFB production’da. Google Core Web Vitals’te yeşil, SEO etkisi positive.

Bu konuda bir projeniz mi var?

Kısa bir özet bırakın, 24 saat içinde size dönüş yapayım.

İletişime Geç