PHP Performance – Optimize, Eliminate or Scale
Get several more uses from your toothpaste by placing the tube in boiling water for several minutes to loosen up the toothpaste inside, then pulling the tube down across the edge of the bathroom counter to scrape all of the toothpaste from the bottom of the tube to the top.
Once this technique no longer yields more toothpaste from the tube, use a pair of scissors to cut the tube open about an inch or two from the top. Open the top part and use your toothbrush to scrape the toothpaste out of the inside of the tube.
When it comes to improving performance in a PHP system, there are a few different approaches you can take. First, find out where your system performance can best be improved – see PHP Optimization – Where to Start. Once you know where your biggest bottlenecks are, you can try to optimize through them, but at some point, you’ll reach the limits of what optimization can do for you – run out of toothpaste, so to speak. There are two widely used performance techniques beyond optimization – eliminate it, or scale it out.
Generally speaking, optimizing is getting the system to do the same thing, only faster. Optimization techniques include things like tuning the server configuration for optimal performance, refactoring the code to get better performance from PHP, Opcode caching or adding indexes to the database tables for faster reads. Optimizing is usually one of the first tools you’ll be pulling out of your bag to deal with performance issues. But there are limits as to how far optimization will take you.
With optimization, there is the law of diminishing returns. Your first pass at optimization may yield a 600%-800% performance improvement. Maybe your next one will give you a 200% improvement. The next one may still give you a 100% improvement. With each pass, finding ways to get those big performance gains will become increasingly difficult. At some point, you’ll be struggling to get a 5% performance boost. Or maybe you’ll find a way to increase performance, but it will also mean the code will become unmaintainable (Even if your one supergenius coder on the team can work with it, if it’s so confusing no other programmer can, it’s unmaintainable. More on that later).
At some point, it’s not going to be worth trying to continue down this route as your performance needs increase. I don’t boil my toothpaste and split it open to get every last drop, I go buy a new tube of toothpaste. Similarly, at some point, even if I know that there are still probably some performance improvements somewhere still be in the system, I’ll start looking for something other than optimization to improve system performance. This is where elimination and scaling come into play.
Elimination is getting rid of your bottlenecks by finding a way to not have to execute them. There are several different ways to do this. Caching and using background process are commonly used ones. Caching is probably the most common elimination technique – if there is data that does not need to be updated with every run, saving and reusing the processed information will save you from having to process it each time.
Another common elimination technique is to take the heavy lifting off of the PHP loader and put it into separate running background process. If there is a “heavy” process that doesn’t need to be real time, just close to real time, maybe it would make sense to have a cron job do the processing of this information. The web user would then just look at this pre-processed information instead of processing every time. This works two ways too – If a user uploads data, instead of waiting for it to be processed in real-time, maybe you could just hold it in a queue and tell them that it will be processed later.
Even with using optimization and elimination, or if you need to deal with real-time processes where elimination isn’t an option, you’ll reach some limits and need to be able to scale your PHP system to get better performance. There’s “vertical scaling,” which means doing something like buying a 48-core server with a terrabyte of RAM; kind of an expensive route. And then there’s horizontal scaling, which is setting up your system so that it can be split across multiple pieces of hardware to distribute the load. This same technique can also be used to give you highly available redundant systems.
Designing a system to horizontally scale typically requires the most work and has the most complexity of any of these three routes. And for many PHP systems, it’s a necessary route to take somewhere in the system. Deciding what needs to be able to horizontally scale is extremely situationally dependent. And with the added complexity to the development and the extra development time and costs that comes with this, designing to scale in situations that don’t need it is a very poor use of resources.
Summing it up
When it comes to improving PHP performance, there are many approaches that you can take, with no blanket solutions that will solve your problems in every situation. With each option, there are pros and cons to consider that will be impacted by many factors, anything from the deployment to the makeup of team that’s working on it. Having a big bag of tools to approach performance issues is necessary, especially for those times you can’t rely on being able to boil out a little more toothpaste.