Skip to content
WasFor
Back to home
Documentation/Operations

Performance tuning

Performance work should start with measurement. Find the slowest layer, fix it, and re-measure. Most hosting bottlenecks are one of: PHP execution, database queries, uncached static assets or insufficient memory.

Measure first

bash
# Response time and status
curl -o /dev/null -s -w "dns:%{time_namelookup} connect:%{time_connect} tls:%{time_appconnect} ttfb:%{time_starttransfer} total:%{time_total}\n" https://app.example.com

# Server load and memory
uptime
free -m
vmstat 1 5

# Top processes
ps aux --sort=-%cpu | head -10
ps aux --sort=-%mem | head -10

A high TTFB points at PHP or the database. A high total with a low TTFB points at payload size or client-side assets.

PHP and OPcache

ini
opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=32
opcache.max_accelerated_files=20000
opcache.validate_timestamps=1
opcache.revalidate_freq=2

In production, validate timestamps infrequently or disable validation and clear OPcache on deploy. Verify the cache is actually used:

bash
php -i | grep opcache
php
<?php
$status = opcache_get_status();
echo $status['opcache_statistics']['opcache_hit_rate'];

PHP-FPM sizing

ini
pm = dynamic
pm.max_children = 24
pm.start_servers = 6
pm.min_spare_servers = 4
pm.max_spare_servers = 12
pm.max_requests = 500

Size max_children from memory, not guesswork:

text
available_ram_mb / average_php_process_mb = max_children

Measure the average process with ps -o rss= -C php-fpm8.3 | awk '{s+=$1} END {print s/NR/1024" MB"}'. Leave headroom for the OS, database and web server.

Database

TechniqueEffect
Indexes on filter and join columnsLargest single win
EXPLAIN on slow queriesExposes full scans
Avoid SELECT *Less I/O and memory
Persistent connectionsFewer handshakes, watch connection limits
Query cache at the application layerSkips repeated work
sql
EXPLAIN ANALYZE
SELECT id, total FROM orders
WHERE customer_id = 1042 AND status = 'paid'
ORDER BY created_at DESC
LIMIT 20;

If the plan shows a full scan, add a composite index on (customer_id, status, created_at).

Caching layers

text
Browser cache → CDN / edge → page cache → object cache → OPcache → database
LayerWhat to cacheTypical TTL
BrowserImages, fonts, CSS, JS1 year, fingerprinted
CDNStatic assets, public pages1 day
Page cacheFull HTML for anonymous users5–60 minutes
Object cacheQuery results, sessions5–30 minutes
OPcacheCompiled PHPUntil deploy

Invalidate deliberately. A cache with no invalidation strategy becomes a correctness bug.

Static assets and HTTP

nginx
gzip on;
gzip_types text/css application/javascript application/json image/svg+xml;
gzip_min_length 1024;

location ~* \.(css|js|woff2|png|jpg|svg)$ {
    expires 1y;
    add_header Cache-Control "public, immutable";
}

Enable HTTP/2 at the edge and HTTP/3 where supported. Fingerprint asset filenames so long cache lifetimes are safe.

Images and media

  • Serve modern formats (WebP/AVIF) with a fallback.
  • Resize at upload time, not on every request.
  • Lazy-load below-the-fold images.
  • Offload large media to object storage and a CDN.

Traffic patterns

PatternSymptomAction
Steady high CPUPHP inefficientProfile the hot path, add OPcache
Spiky CPUUncached pagesAdd page cache
High memoryToo many workersReduce max_children
Slow queriesDatabase boundIndex, then optimize schema
High bandwidthLarge assetsCompress, CDN, lazy-load

Monitoring

Track p50, p95 and p99 latency rather than averages. Alert on p95 regressions, error rate and saturation. Keep a baseline for normal traffic so a change is visible before users report it.

Verification checklist

  • [ ] OPcache enabled and hit rate high
  • [ ] PHP-FPM workers sized from measured memory
  • [ ] Slow query log reviewed weekly
  • [ ] Static assets fingerprinted and cached for a year
  • [ ] CDN or edge cache for public traffic
  • [ ] p95 latency and error rate alerted
  • [ ] Load test after major changes
Last updated: 2026-12-01