DEV Community

Cover image for The importance of PHP's OPcache
Rubén Rubio
Rubén Rubio

Posted on

The importance of PHP's OPcache

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

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

Transactions time

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

AppIndex score

"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."

Top comments (0)