Skip to content

Lesson 7 of 9 · 26m

Profiling PHP Under Real Production Load

Use production-shaped traces, sampling profilers, flame graphs, and system tools to find the actual bottleneck instead of optimizing what looks expensive in development.

Development profiling lies politely. Fixtures are small, caches are warm in the wrong places, and the database is six milliseconds away on localhost. Real systems fail at p95 under uneven traffic. Profiling production does not mean attaching heavy instrumentation to everything forever; it means sampling enough real work to preserve its shape.

Start with the distribution, not the average

A 120 ms average can hide a healthy p50 and a catastrophic p99:

p50  42 ms   cache hits, simple pages
p95 310 ms   search, personalization, cold objects
p99 1.8 s    upstream calls and lock contention

Instrument by endpoint, cache outcome, user type, and dependency latency. Aggregating all requests into one number destroys the evidence.

Use the right profiler at the right resolution

  • Tideways or Blackfire: production-safe sampling and transaction traces.
  • XHProf forks: low-level call graphs, useful in controlled profiling environments.
  • perf: native-level CPU sampling across PHP, extensions, and the kernel.
  • strace: syscall-level confirmation when the process is stuck in I/O.

Example system-level inspection:

pidstat -p 2381 1
perf top -p 2381
strace -c -p 2381 -o /tmp/php.strace

Do not run uncontrolled strace against a busy production process for long; it can add substantial overhead. Prefer short samples and already-isolated reproduction nodes.

Read a profile like a hypothesis

A flame graph showing 60% inside ORM hydration is not proof that the ORM is bad. It is proof that you should inspect query count and row volume. Often the expensive frame is the victim, not the cause: SQL time blamed on the database was really an N+1 loop issuing 900 queries.

A useful production trace answers:

  1. Where is wall time going: CPU, database, cache, HTTP upstream, filesystem?
  2. Is the shape consistent across slow samples or highly bimodal?
  3. Which code path is shared by the slow cohort?
  4. What changed before the regression?

Optimize in dependency order

Remove unnecessary work first: fewer queries, smaller payloads, fewer objects, no repeated computation. Then reduce wait time: indexes, connection locality, batching. Only then consider runtime flags. Reordering those steps produces impressive microbenchmarks and disappointing production graphs.

Field notes

  • Always profile with realistic cache warmth and realistic data cardinality.
  • Correlation IDs across nginx, PHP, SQL, Redis, and external calls turn isolated metrics into an actual causal story.
  • Watch memory allocation as well as time; allocation churn increases GC and CPU pressure.
  • Keep a canary node available for deeper tracing instead of turning invasive instrumentation on for the whole fleet.

Was this lesson helpful?

Sign in to rate