← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · updated 7d
← Back to glossary

PHP Preloading (opcache.preload)

PHP PHP 7.4+ Advanced
debt(d5/e3/b3/t7)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches' (d5), opcache-gui and blackfire (per detection_hints) can show opcache miss rates and whether preload is configured, but issues like stale preloaded code after deploy are silent until observed.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3), quick_fix is creating a preload.php and setting two ini directives — small config change in one component, not a one-liner but contained.

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

Closest to 'localised tax' (b3), applies only to web context and FPM deployment configuration; impacts deploy process (must restart FPM) but doesn't reshape application code.

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

Closest to 'serious trap' (t7), misconception explicitly states devs assume preloading always helps and that changes are picked up — but stale code runs until FPM restart, contradicting normal PHP deploy expectations where file changes are picked up immediately.

About DEBT scoring →

Also Known As

PHP preload opcache.preload PHP 7.4 preloading

TL;DR

Loading PHP files into shared memory at server start so every worker process gets pre-compiled bytecode with zero per-request filesystem overhead.

Explanation

PHP 7.4 introduced preloading via opcache.preload — a php.ini directive pointing to a bootstrap script that calls opcache_compile_file() on framework and application files. At PHP-FPM startup these files are compiled to bytecode and mapped into shared memory, available to all worker processes with no per-request file stat or compilation cost. Frameworks like Symfony and Laravel ship preload scripts. Gains are most noticeable for large frameworks (5–10% throughput). Caveat: preloaded files cannot be updated without restarting PHP-FPM; always disable preloading in development environments.

Diagram

flowchart TD
    subgraph Without_Preload
        REQ_A[Request 1] --> OPCACHE2[OPcache loads class files<br/>on first use per worker]
        REQ_B[Request 2] --> OPCACHE3[Same class loaded again<br/>in each worker process]
    end
    subgraph With_Preload
        BOOT[PHP-FPM boots] --> PRELOAD2[opcache.preload runs<br/>loads framework classes once]
        PRELOAD2 --> SHARED[Shared memory<br/>available to ALL workers instantly]
        SHARED --> FAST2[Zero class loading overhead<br/>on any request]
    end
style PRELOAD2 fill:#1f6feb,color:#fff
style SHARED fill:#6e40c9,color:#fff
style FAST2 fill:#238636,color:#fff

Common Misconception

PHP preloading always improves application performance. Preloading loads specified files into shared memory at server startup — if the preloaded files change (during deployment), the server must be restarted to pick up the new code, otherwise the old preloaded version continues to run.

Why It Matters

Preloading compiles PHP files into shared memory at FPM startup — those files skip parsing and compilation on every request, reducing per-request CPU overhead for large framework codebases.

Common Mistakes

  • Preloading files that change frequently — FPM must restart for changes to take effect.
  • Not setting opcache.preload_user — FPM won't preload as root by default.
  • Preloading the entire vendor directory — classes that are rarely used waste shared memory.
  • Not measuring the impact — preloading adds startup time and memory; benchmark before committing.

Code Examples

✗ Vulnerable
; php.ini — preloading not configured:
; opcache.preload =              ; Not set — no preloading
; opcache.preload_user =         ; Required — must be non-root

; Correct:
opcache.preload = /var/www/app/preload.php
opcache.preload_user = www-data
✓ Fixed
; php.ini — preload at FPM startup (PHP 7.4+)
opcache.preload      = /var/www/app/preload.php
opcache.preload_user = www-data

// preload.php — compile key files on startup:
<?php
foreach (glob('/var/www/app/src/**/*.php') as $file) {
    opcache_compile_file($file);
}

; Laravel: php artisan optimize
; Symfony: php bin/console cache:warmup

; Verify:
$ php -r "print_r(opcache_get_status()['scripts']);"

; Biggest win: eliminates per-request parsing of framework files

Added 15 Mar 2026
Edited 22 Mar 2026
Views 113
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
3 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 1 ping S 1 ping S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 3 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 2 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 2 pings S 0 pings M
No pings yet today
Amazonbot 1 SEMrush 1
Amazonbot 13 PetalBot 12 ChatGPT 8 SEMrush 8 Ahrefs 7 Perplexity 6 Scrapy 5 Google 4 Bing 4 Claude 3 Twitter/X 2 Applebot 2 Meta AI 1 Brave Search 1
crawler 70 crawler_json 6
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
PHP php A server-side scripting language that generates web pages and APIs — the code runs on the server, and only its output (usually HTML or JSON) reaches the browser.

PHP is often the first server-side language people meet, and understanding its execution model — script starts fresh on every request, no memory between requests — explains most of how the web backend works: sessions, databases, and caching all exist to bridge that per-request amnesia.

💡 Start with PHP 8.x, declare(strict_types=1), and PDO — skip any tutorial that mentions mysql_query().

Ask Codex about PHP →
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Medium
⚡ Quick Fix
Create a preload.php that includes your most-used framework files, set opcache.preload=/path/to/preload.php and opcache.preload_user=www-data in php.ini
📦 Applies To
PHP 7.4+ web laravel symfony
🔗 Prerequisites
🔍 Detection Hints
No opcache.preload configured for framework app; high opcache miss rate on framework core files
Auto-detectable: ✓ Yes opcache-gui blackfire
🤖 AI Agent
Confidence: Medium False Positives: Low ✓ Auto-fixable Fix: Low Context: File


✓ schema.org compliant