---
title: 'OPcache Preloading - A miracle cure to improve PHP performance?'
source: 'https://youtube.com/watch?v=wkhpNd7aYV8'
video_id: 'wkhpNd7aYV8'
date: 2026-08-03
duration_sec: 726
---

# OPcache Preloading - A miracle cure to improve PHP performance?

> Source: [OPcache Preloading - A miracle cure to improve PHP performance?](https://youtube.com/watch?v=wkhpNd7aYV8)

## Summary

This video explores OPcache preloading, a PHP feature since 7.4, and evaluates its real-world performance benefits. The presenter explains what preloading is, how to set it up, and shares benchmark results from Tideways, concluding that preloading offers significant gains mainly for very fast applications.

### Key Points

- **Introduction to Preloading** [00:02] — Preloading is a core PHP feature since 7.4, contributed by Dmitry Stogov in 2018. It allows loading scripts into memory at PHP process startup, making functions and classes available to all requests as if they were internal.
- **Preloading vs. OPcache** [01:00] — OPcache caches compiled bytecode, while preloading eliminates autoloading time by having classes already loaded. Preloading complements OPcache but has a downside: longer PHP-FPM or Apache startup times.
- **Requirements for Preloading** [01:42] — Requirements: the application must be the only one on the server, PHP must use PHP-FPM or Apache with mod_php (shared nothing architecture), and you need a custom preloading script that requires all files to preload.
- **Writing the Preloading Script** [02:38] — The script must require all files in the correct order, handling dependencies. Symfony generates this automatically; Laravel has third-party packages. The script iterates over source files, excluding tests, and uses require_once.
- **Configuring PHP.ini** [04:25] — Set opcache.preload to the script path and opcache.preload_user to the Linux user running PHP. After changes, restart PHP-FPM or Apache and monitor error logs or tools like Tideways.
- **Verifying Preloading Works** [05:05] — Use opcache_get_status() to check the 'preload_statistics' key, which includes memory consumption. This key is only available when preloading is enabled.
- **Measuring Autoloading Impact** [06:00] — Tideways hooks into Zend compile and Zend autoload to measure time spent. In a Shopware demo, autoloading was 10-16ms per request, but relative to 1s response time, it's under 1% of total time.
- **Tideways Backend Test** [08:08] — Tideways backend (Symfony-based) had 19ms response time (95th percentile), with autoloading at 1ms. After enabling preloading, response time dropped to 18ms (10% improvement). API service improved from 34ms to 32ms (5%). Staging improved 25% but with low traffic.
- **When to Use Preloading** [11:30] — Preloading is beneficial for applications with response times below 100ms, increasing throughput and potentially saving CPU costs. For slower apps, other OPcache settings may be more impactful.

### Conclusion

OPcache preloading is not a miracle cure but a targeted optimization for high-throughput, low-latency applications. It can yield 5-10% improvements in fast services, but for most apps, the impact is negligible.

## Transcript

even more performance out of your PHP application. There are only a few special cases where preloading will have a measurable impact on your application performance. And in this video, we are going to cover what preloading is, how
you can set it up and configure it in your application and what performance gain you can expect. Mo, I'm Benjamin and living and breathing PHP performance for the last 10 years now, helping thousands of developers, companies, and
open source projects to speed up their PHP code. Preloading is a core PHP feature since version 7.4 and was contributed by version 7.4 and was contributed by Dimmitri of Zent back in 2018
and was accepted unanimously during voting. This feature allows you to preload scripts at the startup of the PHP process when no requests have run yet. Then each request run in the process has these preloaded functions
and classes already available immediately as if they were internal classes. This is different than opcache itself which improves and eliminates the time spent compiling PHP scripts to bite code
through caching. Preloading complements opcache by reducing time spent in the autoloader that may also take up a significant that may also take up a significant portion of your request time. So on
paper, preloading should eliminate the time spent autoloading classes and reduce the response times in that share. The downside of pre-loading is that every startup of PHP FPM or Apache 2 is taking a longer time because the
pre-loading script has to be compiled and run and put into memory. To use prerequisites. First, you have application has to be the only one running on the server. This is usually the case in modern PHP
deployments, but I want to mention it regardless. Second, you must be using regardless. Second, you must be using PHP with uh either PHP FPM or Apache 2 PHP with uh either PHP FPM or Apache 2 with mod PHP or any other SARP that is
shared nothing architecture-based preloading has no effect on application servers such as Franken Pip, Roadrunner or Laravel's Octane. And in runtimes such as breath on RVS Lambda, it may shift performance around only from the
request to the boot time, but in a way that users will still feel and um recognize. The third requirement is that you uh write your own preloading PHP
script. That script must essentially require all the files that you want to pre-ompile and preload into the PHP process. Uh the Symfony framework generates this by default. Um Laravel has for example a third party package
that allows to do that which I haven't tested myself. Uh in other cases and when you don't use those two you have to write this yourself or find uh packages write this yourself or find uh packages that um do this for your f framework.
The PHP documentation has information on how to do this uh in your application. uh in the pre-loading script any file reference by include include once and so on opache compile file will be passed into persistent memory in the following
example all PHP files in the source directory will be preloaded unless they are a test file so we can see here uh a script iterating the source directory script iterating the source directory excluding test files and then require
excluding test files and then require ones on the file and this does not take into account that um scripts might have dependencies on each other with um
dependencies on each other with um either inheritance or um types uh used either inheritance or um types uh used for arguments and return types. So this order has to be the correct one to include because we don't have
essentially have to do the mapping yourself which file comes first and which one comes after that and uh then you include them like in the right order. Then you have to configure the PHP ini with two settings opache uh
preload points to the file that we've created before or our framework created and opach preload user um is the Linux user um that uh the
process um switches into when running the scripts. After the change, just the scripts. After the change, just restart PHP FPM or Apache 2 and then restart PHP FPM or Apache 2 and then monitor your error lock files uh or your
exception tracking software such as Tideways to make sure that everything runs smoothly. That's it. To see if opcache preloading is working, you can call opcache get status the
you can call opcache get status the function in your uh code and look at the newly added preload statistics key that is only available when pre-loading is is only available when pre-loading is enabled. It includes a metric uh called
memory consumption to understand how much memory the preloading stage um much memory the preloading stage um used. And uh I haven't looked at the PHP source code, but I would expect this memory to be taken from the regular
memory to be taken from the regular upcache memory uh buffer and um counting towards this limit. In all honesty, I have not gotten around to test preloading on Tideways before, but it's hovering in my to-do list since it came
out. This week, it was finally time to evaluate. The reason for not looking into it for the last five years is that I didn't expect the performance benefit to be large enough that it mattered looking into it and getting familiar
with this feature. So let's look at a demo application and how much time it spends in autoloading and compiling. We can measure this with tightways which is hooking into two PHP engine level hooks available to PHP
extensions. The first one is called Zen compile which is related to compiling PHP scripts and the second hook is called Zent autoload which is related to you would guess autoloading by overwriting uh and adding timing to
both hooks in our typebased PHP extension. We can see how much time an application spends in both layers. At this time, there's no way to measure this in userland PHP code unless you directly patch the autoloader using
highresolution timer calls. Let's take a look at a Shopware demo application look at a Shopware demo application which is written in Symfony uh to see what impact we can expect from autoloading in PHP scripts.
So in the history module we can see the performance of layers over um large amount of time in this case 5 weeks and we can see autoloading here is around um
10 to 16 milliseconds on average in every request and this is across all these uh thousands of requests that uh were made um during that time.
were made um during that time. And if we set this in relation to the 95 percentile of performance uh of the complete scripts then we can see with them being around 1 second the autoloading performance is actually
below 1% share of the whole PHP request time and preloading only improves the autoloading time. So we don't expect this to be anything that improves
visibly for users or makes the performance better in any significant way. But what if your application is already fast and takes less than 100 milliseconds to load? Let's take a look at the impact that preloading has on the
Tideways back end. Because Tideways is based on Symfony and Symfony already generates a pre-loading script through the dependency injection container, my colleague Tobias and I could test the the impact of preloading with just 2
hours of time investment. First, we started with an assessment of the potential impact that this uh preloading change could have on the performance of change could have on the performance of our data inest service. The data ingest
our data inest service. The data ingest service um has a response time of around uh 19 milliseconds uh in the 95 percentile and the average is lower at around 12 uh milliseconds. We can see in the history of tideways
We can see in the history of tideways that uh in the last five weeks the performance um that autoloading had the impact that autoloading had on this performance was always just 1 millisecond. So if we set this in
relation we are expecting to see a 5 to 10% improvement by using uh op cache preloading. We rolled out this configuration change to our application servers and used
to our application servers and used tideways to create a marker that allows us to compare the performance before and after this configuration change. We can see uh on this marker for the service for the data ingestion service
service for the data ingestion service that the performance improved by 10% um going from 20 milliseconds to 18 milliseconds in the 95 percentile. So exactly what we um thought was possible um before. And um the requests here we
um before. And um the requests here we can see at 32,000 per minute. We can can see at 32,000 per minute. We can also um see this by hovering over um the layer performance graph here where we can see that before the change
autoloading is still responsible for 2 milliseconds of the performance and afterwards it's uh not responsible for the performance anymore.
marker that we've added to the API service. The API service only has 4,000 requests every minute, so it's used a little less. Um, and the performance dropped from 34 milliseconds to 32 milliseconds. Still an improvement of
milliseconds. Still an improvement of around 5% and that is uh very good for these kind of very fast requests. We have also tested this in our staging environment where you can see that before uh the configuration change
autoloading um took about 7 or 8 milliseconds uh on average and after the milliseconds uh on average and after the change um it's not visible anymore and the performance dropped from 40 milliseconds to 30 milliseconds for a
milliseconds to 30 milliseconds for a 25% improvement in performance. However, think that this is just our staging environment and it's uh limited in resources and also doesn't have a lot of requests. As you can see here, just six
requests every minute. Uh so this improvement is not really um showing real world potential. While preloading does not improve performance in a way that users feel, it allows to handle more requests and increases throughput
capacity for high traffic applications. Consequently, you should look into opcache preloading if your application is already very fast and has response times of below 100 milliseconds. This might allow you to save costs by
reducing aggregated CPU count. With higher response times, I don't think preloading is actually worth it, but maybe there are other OPC cache settings that you haven't optimized yet. See this next video for details and don't forget
to subscribe to the channel or our newsletter on PHP performance topics. newsletter on PHP performance topics. See you around.
