# Cutting PHP CPU usage 40% with an Ubuntu 22.04 upgrade

Over these past few weeks, we've updated [our Ubuntu 20.04 LTS uptime servers to Ubuntu 22.04 LTS](/news-and-updates/how-to-upgrade-from-ubuntu-2004-lts-to-ubuntu-2204-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).

![CPU utilization graph dropping after the upgrade window from Ubuntu 20.04 PHP 8.2 to Ubuntu 22.04 PHP 8.2](/media/blog/EAd6fAdGtiFZH7NliehPZUttW6lxgRkbeckl1Atp.png)

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.

![Graph of context switches and interrupts per second jumping higher after the Ubuntu upgrade](/media/blog/VEyGAFjHLeFvP3Eno5psi1Bjg1x4Vlih2IZM7914.png)

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

![Graph of CPU usage by the forge user falling from about 16% to 11% after the upgrade](/media/blog/hjOde7cE7g2fkuRuj6nGUjASTKhdplbzpLp6LuO7.png)
**Exhibit 2**: a noticeable reduction in the _CPU load_ as well:

![CPU load average graph dropping after the switch from Ubuntu 20.04 to Ubuntu 22.04](/media/blog/f4wsKVRzSrXmnVPAeLTIRdhrU9rOgUO06rHTSXls.png)
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.