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
# 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 -10A 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
opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=32
opcache.max_accelerated_files=20000
opcache.validate_timestamps=1
opcache.revalidate_freq=2In production, validate timestamps infrequently or disable validation and clear OPcache on deploy. Verify the cache is actually used:
php -i | grep opcache<?php
$status = opcache_get_status();
echo $status['opcache_statistics']['opcache_hit_rate'];PHP-FPM sizing
pm = dynamic
pm.max_children = 24
pm.start_servers = 6
pm.min_spare_servers = 4
pm.max_spare_servers = 12
pm.max_requests = 500Size max_children from memory, not guesswork:
available_ram_mb / average_php_process_mb = max_childrenMeasure 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
| Technique | Effect |
|---|---|
| Indexes on filter and join columns | Largest single win |
EXPLAIN on slow queries | Exposes full scans |
Avoid SELECT * | Less I/O and memory |
| Persistent connections | Fewer handshakes, watch connection limits |
| Query cache at the application layer | Skips repeated work |
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
Browser cache → CDN / edge → page cache → object cache → OPcache → database| Layer | What to cache | Typical TTL |
|---|---|---|
| Browser | Images, fonts, CSS, JS | 1 year, fingerprinted |
| CDN | Static assets, public pages | 1 day |
| Page cache | Full HTML for anonymous users | 5–60 minutes |
| Object cache | Query results, sessions | 5–30 minutes |
| OPcache | Compiled PHP | Until deploy |
Invalidate deliberately. A cache with no invalidation strategy becomes a correctness bug.
Static assets and HTTP
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
| Pattern | Symptom | Action |
|---|---|---|
| Steady high CPU | PHP inefficient | Profile the hot path, add OPcache |
| Spiky CPU | Uncached pages | Add page cache |
| High memory | Too many workers | Reduce max_children |
| Slow queries | Database bound | Index, then optimize schema |
| High bandwidth | Large assets | Compress, 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