Reducing PHP's CPU usage by nearly 40% by upgrading from Ubuntu 20.04 to 22.04
Over these past few weeks, we've updated our Ubuntu 20.04 LTS uptime servers to Ubuntu 22.04 LTS. The reason was mostly just staying up-to-date, but we had a surprising side-effect that was unexpected but very welcome.
Measuring reduced CPU usage
An easy riddle: can you guess when we were running on Ubuntu 20.04 and when we were on 22.04? The spikes in the middle were during the upgrades (when this server was taken out of rotation).
For the exact same workload, here's how the numbers work out:
- Avg CPU (user) on Ubuntu 20.04 LTS: 22.9%
- Avg CPU (user) on Ubuntu 22.04 LTS: 13.2%
That's a whopping 42% reduction in CPU usage just by upgrading the OS. 🥳
And this wasn't an isolated server, this was measured across multiple servers, across multiple (independent) cloud providers.
Purposefully simple workload
Our satellite servers are spread worldwide and act as mere proxies for HTTP(s) requests. They are, on purpose, simple by design.
Each server has Nginx & PHP-FPM running, nothing more. It just takes in requests from our dispatcher, processes them by calling out to an external service (aka: our client website to monitor) and then returns the response.
After the upgrade;, we observered a significant increase in context switches for each server.
Because the workload has remained constant, I can only assume this dramatic speed up of context switches/interrupts is the direct cause of the performance improvements we're seeing. Happy to hear thoughts/theories on this!
Measured across dozens of servers
Just for dramatic effect, here's the CPU usage of a few other servers, but without the fixed y-axis scale. Sorry data scientists, I couldn't resist skewing the scale to our advantage here.
Exhibit 1: a drop from ~16% CPU usage to ~10.3%.
Exhibit 2: a noticeable reduction in the CPU load as well:
On average, the CPU load went from 0.7 to 0.4.
Your mileage may vary
Obviously, our setup isn't yours. We run pure PHP processes here, no databases/caches/file storage/... It may be a rather exceptional setup, as most environments would at least have some kind of persistence in the form of a database in the mix here.
But, the performance improvements are noticeable!
If you're still on Ubuntu 20.04 LTS - which gets support until 2025 - you might want to consider upgrading to the next LTS to get some noticeable speed improvements.