PHP Performance: Additional CPU cores vs Faster CPU cores

Some time ago, I received an email from a client experiencing slow performance issues with a LEMP (Linux, Nginx, MySQL, and PHP web server). During a full audit, I found that the server’s load average was pretty low (see below screenshot). However, the website was indeed very slow. There were some misconfigurations, but one of the main PHP performance bottlenecks was poor CPU single-thread performance.

Upon further investigation, I found that web traffic was not enough to activate all 18 cores concurrently. Also, each CPU core took anywhere from 1 to 3 seconds on average to complete PHP requests. As mentioned, there were other code efficiency issues, but this article will focus on CPU single-thread performance (CPU core speeds).

PHP benefits greatly from CPU single-thread performance.
Each PHP-FPM process uses a single CPU core. PHP benefits greatly from CPU single-thread performance.

Before we get to that, let’s look at how PHP uses your web server’s CPU. As you may have already noticed, PHP is not designed for multithreading. Therefore, each page/request is served by one PHP process, and each process locks on to one CPU core. This is also the case when PHP waits for MySQL queries to complete. However, unlike PHP, MySQL is multithreaded, but that’s another topic.

This is the way PHP was designed to function. If your web server has concurrent page requests, you’ll also have several PHP processes – each using a CPU core – running concurrently. This makes your choice of CPU very important!

With 18 cores on this low-traffic server, the server’s load average remained below 18. In fact, during monitoring, no more than 6 – 10 cores were being used by PHP concurrently during peak traffic. As a result, in addition to other recommendations included in my PDF audit report, the following was also recommended:

You have a lot of CPU cores (18) but the core speed is only 2.0GHz. Since PHP processes are executed per-core, a VPS with 3+GHz cores would fit your workload better.

Faster CPU cores vs. Additional CPU cores

As mentioned, the server had 18 CPU cores, with a below-average core speed of 2.0GHz. It was clear from monitoring that although the PHP application and MySQL queries needed optimization, the issue was made even worse by the slow CPU single-thread performance. Let’s look at the importance of having the right blend of speed and capacity, or faster CPU cores vs. additional CPU cores.

php slow processes
Traffic was relatively low for this server, with approximately 6 to 10 cores being used during peak traffic.

If it takes a 2GHz processor core 3 seconds to process a request, then a 3GHz processor core would return the same request in around 2 seconds. This, in turn, frees up cores for additional requests at a faster rate. This means we can safely reduce the # of cores from 18 to 8. Although the concurrent capacity would drop slightly, the server’s max throughput would increase by over 30%. The result is improved scaling and a faster end-user experience! Of course, if this server were instead experiencing a high load average (18.00+), with, say, 3GHz+ cores, the recommendation would be different. In many cases, addressing PHP and other related performance misconfigurations will result in performance gains. With optimizations applied, this server took 100ms to 300ms on average, instead of 1 – 3 seconds.

PHP: More CPU cores, or faster CPU cores?
Client’s old server w/ slow 2.0GHz cores

The client was not to blame because when they informed their web host about slow web application performance, the host, instead of alerting them of the lack of CPU single-thread performance, suggested upgrading the VPS package, which featured additional CPU cores. However, during each of these upgrades, the 2.0GHz clock speed of the CPU cores remained unchanged, which provided precisely zero percent improvement to server throughput!

Checking CPU core speed and number of cores – Linux command line

Suppose you are unsure of your server’s CPU specs. You can quickly check with the following command:

lscpu

This will list your CPU specs:

root@vps01 [~]# lscpu 
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 8
On-line CPU(s) list: 0-7
Thread(s) per core: 1
Core(s) per socket: 8
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 62
Model name: Intel(R) Xeon(R) CPU E5-1650 v2 @ 3.50GHz
Stepping: 4
CPU MHz: 3499.998
BogoMIPS: 6999.99
Hypervisor vendor: KVM
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 256Khere
L3 cache: 12288K
NUMA node0 CPU(s): 0-7

Use nproc to display only the # of cores, or cat /proc/cpuinfo for all cores listed with specs.

For CLI-only PHP scripts, read here. For the next blog post, I hope to write about hardware selecting/sizing, application bottlenecks, or MySQL optimization. Please subscribe to updates using the email form below.

Published: May 6th, 2017 | Last Updated: June 17th, 2024

Tags: , , , ,

Discussion

  1. Thanks for this great post!

    I’ve got a related question that I hope you know something about.

    Vps servers give you a vcpu, which is a single cpu thread - at best (this seems to be aws and Google’s policy 2 vcpus per physical core, but many other providers oversubscribe the physical cores since they’re not all active at the same time). Lscpu shows this in your article - 8 cores, with 1 thread per core.

    But if we have a bare metal server, we have the actual physical cores and Lscpu would show the threads per core.

    My question is - how would we get php-fpm or lsphp on a litespeed server to use threads rather than physical cores - as is done on a vps server? The idea being that the php processes aren’t computing at all times, since they wait for db and other file I/O. So, in essence, php processes that are running “multithreaded” on a vps server should be able to outperform truly singled threaded bare metal php processes.

    Or am I missing something? I can’t find any info on this anywhere, and your site is one of the better resources on this sort of info!

    Thanks!

  2. First of all, welcome to the forums.

    Multithreading is the ability of a central processing unit (CPU) (or a single core in a multi-core processor) to provide multiple threads of execution concurrently, supported by the operating system.

    PHP does not come with built-in multithreading support. However, you can try Pthreading. Pthreads is an object-orientated API that provides all of the tools needed for multithreading in PHP. but it is usually not worth it. :face_with_diagonal_mouth:

    Beyond this, hardware is king :crown: for PHP. What is the clock speed of your cores? How fast is your storage.

    If this is a production environment with high traffic, I can offer you a free trial of our INSANE by invite only cloud platform (stackLinux.com), which we upgrade complimentary every 1 to 2 years before they burn out due to overclocking and forcing currently 3.9 GHz CPUs and everything else to always run at MAX.



Top ↑