← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

OPcache Tuning

php PHP 5.5+ Intermediate
debt(d5/e1/b3/t7)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches' (d5), since opcache-gui, phpinfo, and blackfire from detection_hints can reveal hit rate and config issues, but only if you go look — it's silent otherwise.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch' (e1), the quick_fix is just adjusting a handful of php.ini directives (validate_timestamps=0, memory_consumption=256, etc.) — config-only change.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3), applies_to web context only and lives in php.ini; once tuned it doesn't shape application code, though deployment workflow must include cache reset.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7), the misconception that OPcache is enabled-and-done is widespread, and the validate_timestamps=0 setting contradicts dev-mode expectations — deploys silently serve stale bytecode unless explicitly invalidated.

About DEBT scoring →

Also Known As

OPcache opcache bytecode cache opcode cache

TL;DR

OPcache stores compiled PHP bytecode in shared memory, eliminating recompilation on every request — proper tuning is the single highest-impact PHP performance configuration.

Explanation

PHP compiles scripts to opcodes on every request by default. OPcache caches these in shared memory, making subsequent requests skip compilation entirely — typically 2-5× faster with zero code changes. Key settings: opcache.memory_consumption (size of shared memory, 128-256MB for large apps), opcache.max_accelerated_files (number of files to cache — must exceed your total PHP file count), opcache.validate_timestamps (set to 0 in production for maximum performance, invalidate manually on deploy), opcache.preload (PHP 7.4+ — load frequently used classes into memory at startup).

Diagram

flowchart TD
    subgraph Without OPcache
        REQ1[Request 1] --> PARSE1[Parse PHP files]
        PARSE1 --> COMPILE1[Compile to opcodes]
        COMPILE1 --> EXEC1[Execute]
        REQ2[Request 2] --> PARSE2[Parse again!]
        PARSE2 --> COMPILE2[Compile again!]
    end
    subgraph With OPcache
        REQ3[Request 1] --> PARSE3[Parse and compile]
        PARSE3 --> CACHE[(Shared memory cache)]
        REQ4[Request 2] --> CACHE
        CACHE --> EXEC2[Execute - skip parse+compile]
    end
    style PARSE2 fill:#f85149,color:#fff
    style COMPILE2 fill:#f85149,color:#fff
    style CACHE fill:#238636,color:#fff

Common Misconception

OPcache is enabled by default and needs no configuration — it ships enabled since PHP 5.5 but with conservative defaults; production deployments need explicit tuning for maximum benefit.

Why It Matters

Disabling or under-tuning OPcache is one of the most common PHP performance mistakes — a properly tuned OPcache can reduce CPU usage by 50-80% for typical web applications.

Common Mistakes

  • opcache.validate_timestamps=1 in production — checks file mtime on every request, adding syscall overhead.
  • opcache.max_accelerated_files too low — files not in cache fall back to recompilation.
  • opcache.memory_consumption too small — cache evictions cause thrashing.
  • Not invalidating the cache after deployment — stale bytecode serves old code when validate_timestamps=0.

Code Examples

✗ Vulnerable
; Default/untuned OPcache — suboptimal for production:
opcache.enable=1
opcache.memory_consumption=64        ; Too small for large apps
opcache.max_accelerated_files=2000   ; Too few for Composer apps
opcache.validate_timestamps=1        ; File stat on every request
; No preload configured
✓ Fixed
; Production-tuned OPcache:
opcache.enable=1
opcache.enable_cli=0
opcache.memory_consumption=256       ; 256MB — covers large apps
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=20000  ; More than your total PHP files
opcache.validate_timestamps=0        ; No stat check — invalidate on deploy
opcache.save_comments=1              ; Required for annotations/attributes
opcache.preload=/var/www/app/preload.php  ; PHP 7.4+

; Deploy script:
; php -r "opcache_reset();"  -- or reload PHP-FPM

Added 16 Mar 2026
Edited 22 Mar 2026
Views 31
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping F 1 ping S 1 ping S 0 pings M 0 pings T 0 pings W 2 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F
No pings yet today
No pings yesterday
Amazonbot 7 Perplexity 7 Unknown AI 2 Google 2 Ahrefs 2 ChatGPT 2
crawler 20 crawler_json 2
DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Low
⚡ Quick Fix
Set validate_timestamps=0 in production (reset cache after deploy), memory_consumption=256, max_accelerated_files=20000, interned_strings_buffer=16
📦 Applies To
PHP 5.5+ web
🔗 Prerequisites
🔍 Detection Hints
validate_timestamps=1 in production causing stat() per request; max_accelerated_files too low with many classes; hit rate <99%
Auto-detectable: ✓ Yes opcache-gui phpinfo blackfire
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✓ Auto-fixable Fix: Low Context: File

✓ schema.org compliant