{
    "slug": "opcode_caching",
    "term": "Opcode Caching",
    "category": "performance",
    "difficulty": "intermediate",
    "short": "Storing precompiled PHP bytecode in memory to skip the parse-and-compile phase on subsequent requests.",
    "long": "PHP normally parses and compiles source files on every request. Opcode caching (via OPcache, the standard built-in extension since PHP 5.5) stores the compiled bytecode in shared memory so subsequent requests for the same file skip compilation entirely. This can improve throughput by 50–90% for typical applications. The JIT compiler introduced in PHP 8.0 extends this further by compiling hot bytecode to native machine code. Proper OPcache configuration (adequate memory, file count, and deployment cache-clear strategy) is essential in production.",
    "aliases": [
        "opcache",
        "opcode cache",
        "PHP bytecode cache"
    ],
    "tags": [
        "php",
        "performance",
        "caching",
        "opcache"
    ],
    "misconception": "OPcache only benefits high-traffic sites. OPcache eliminates PHP file parsing and compilation on every request — a 2-5x speedup on most applications regardless of traffic volume. It should be enabled on every production PHP server as a baseline.",
    "why_it_matters": "PHP compiles scripts to opcodes on every request without OPcache — OPcache stores compiled bytecode in shared memory, eliminating repeated parsing and compilation that typically consumes 30-50% of PHP execution time.",
    "common_mistakes": [
        "Not enabling OPcache in production — it is often off by default in minimal PHP installs.",
        "Setting opcache.validate_timestamps=1 in production — checks file mtimes on every request, negating half the benefit.",
        "Not setting opcache.memory_consumption large enough — cache evictions cause repeated recompilation.",
        "Deploying new code without restarting PHP-FPM or touching opcache.php — stale bytecode serves old code."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "opcache",
        "php_fpm",
        "caching"
    ],
    "prerequisites": [
        "opcache",
        "php_compilation_pipeline",
        "bytecode_vm"
    ],
    "refs": [
        "https://www.php.net/manual/en/book.opcache.php"
    ],
    "bad_code": "; php.ini — OPcache disabled or misconfigured:\n;opcache.enable=1          ; Commented out — no caching\nopcache.memory_consumption=64  ; Too small for large frameworks — use 256+\nopcache.validate_timestamps=1  ; File stat on every request in production — set to 0",
    "good_code": "; php.ini — OPcache production settings\nopcache.enable=1\nopcache.memory_consumption=256     ; MB — size to your codebase\nopcache.interned_strings_buffer=16\nopcache.max_accelerated_files=20000\nopcache.validate_timestamps=0      ; disable in production — manual invalidation\nopcache.revalidate_freq=0\nopcache.save_comments=1            ; needed for PHPDoc-based tools\n\n; After deploy — invalidate stale cache\n; opcache_reset(); or restart PHP-FPM\nphp -r 'opcache_reset();'\n; Or per file: opcache_invalidate('/var/www/app/bootstrap.php', true);",
    "quick_fix": "Enable OPcache with validate_timestamps=0 in production (uses inotify or deploy hook to invalidate) — with it enabled your PHP app can serve 5-10x more requests per second",
    "severity": "high",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/opcode_caching",
        "html_url": "https://codeclaritylab.com/glossary/opcode_caching",
        "json_url": "https://codeclaritylab.com/glossary/opcode_caching.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Opcode Caching](https://codeclaritylab.com/glossary/opcode_caching) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/opcode_caching"
            }
        }
    }
}