The importance of PHP's OPcache
Tags: php, performance, cache, opcache
Published: 2024-02-21 08:30:00

_Wednesday, 20th of December 2023, 10.08 AM_
__Client__: "Our PHP + Symfony application has performance issues. The website is slow; it takes a lot of time to load some pages. Sometimes, it just freezes, making it unusable. We are receiving complaints from our users: they need it to work, but they can not."

_Wednesday, 20th of December 2023, 04.57 PM_
__Developer__: "Hello! That is something I can look into. We can solve it, no problem. However, the next two weeks I am on holidays, so I will work on it when I am back. Merry Christmas and Happy New Year!"

_Monday, 8th of January 2024, 07.43 AM_
__Client__: "How is the optimization going? My users could not have holidays: they had to make up for the time lost while working with our website. They are starting to get angry. I even got some threats in my physical mailbox."

_Monday, 8th of January 2024, 01.19 PM_
__Developer__: "First, Happy New Year! I understand your users: it is normal to become irritable when you do not have time off. They should take their job less seriously; they are not going to inherit the company. And for you, just do not look into your mailbox. Who sends letters today, anyway?"

"But let me take a look at your application. First, we need to have some metrics to know what is going on. We need to observe which endpoints are slow and which ones are called the most... Otherwise, we are just acting on sensations, not on data. To observe the system, I just installed New Relic. We should have enough data to draw conclusions in a week. We can act then."

_Monday, 15th of January 2024, 08.56 AM_
__Client__: "How about the metrics of our application? A week has gone by already. Can you see something that can be improved? The users are losing their patience, and I am starting to get afraid. Yesterday, I found the windows of my car broken, with a note inside: my users request response times of less than 1 second. Otherwise, things will become more serious, they wrote."

_Monday, 15th of January 2024, 10.25 AM_
__Developer__: "It is a reasonable request, I must say. Any website nowadays should have a response time of less than 1 second on average. Maybe you can consider it a turning point in your life and stop using a car? It is healthier to walk, you will feel better. But I see there are two endpoints that take up to 50% of the load of the system. Both are queries, i.e., they just get data to show it to the user. They heavily use Doctrine, an ORM, so we are facing the famous N+1 problem. But fear not! I will fight back and win: we should be able to cache the data in Redis, so we avoid querying the database every time to get the same data, and get the calculated data straight from memory. Just hang in there."

_Friday, 19th of January 2024, 02.05 AM_
__Client__: "Hey! How is that performance improvement going? My family and I are «spending away time» with some of the ~~angriest~~ ~~craziest~~ users of our application. The worst? They keep showing me how slow and unusable it is. I have come to the point where I understand them. Anyway, let me know, how the improvement is going."

_Friday, 19th of January 2024, 12.46 PM_
__Developer__: "I am glad to know that you are enjoying some time off. You deserve it, buddy! In the meantime, I optimized both endpoints that were the slowest: they are now using projections, and all the models they require are cached in Redis. However, I cannot see much improvement, the response times are still over 1 second. Well, it is Friday noon, so I will look into it first thing on Monday. Have a nice weekend!"

_Saturday, 20th of January 2024, 03.23 AM_
__Client__: "HEY MAN! THEY TOOK MY DAUGHTER AWAY. THEY MADE ME SAY GOOD BYE TO HER. I DO NOT KNOW WHAT IS HAPPENING ANYMORE."

_Monday, 22nd of January 2024, 12.28 PM_
__Developer__: "They grow up so fast! It is good to let them have space and make their own mistakes. Well, I found it: it seems we forgot to enable OPcache. I enabled it at 11.45 AM. Look how we reduced the response times:"

![Response times](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ftxd7y49xgh27n9kzbq4.png)

"Here is the chart with transactons time after enabling OPcache:"

![Transactions time](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5zeftv209ki9e6487umh.png)

"And here is the chart with the AppIndex score:"

![AppIndex score](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nddi059obzaqti8t4ans.png)

"Problem solved!"

_Monday, 22nd of January 2024, 12.29 PM_
__Client__: "WTF IS AN OPCACHE?"

_Monday, 22nd of January 2024, 3.07 PM_
__Developer__: "PHP is an interpreted language. That means every time a script is executed in a request, the code must be converted to bytecode (or machine code), i.e., the code the computer understands. However, in production, the code is always the same amongst each deploy, so that conversion can be cached for all requests. That is what OPcache does. By the way, you should look into your «Caps. lock» key. It seems stuck."

_Monday, 29th of January 2024, 11.50 AM_
__Developer__: "Here is the bill. You could have at least thanked me after all the work I put when optimizing your application."