ZTS is the future of PHP
Uncategorized
For a few years I’ve had a vision for PHP: it can only be compiled with ZTS mode. Selfishly, my reason is that this would boost ext/parallel adoption.
With ext/parallel, PHP would gain a form of Go-like async programming, adding a second async approach next to the Polling API and Fibers, which are already available.
In my (yes, highly subjective) opinion, ext/parallel covers the 80% use-case that most PHP applications need for multi-threading and parallelism. I’ll write about ext/parallel and how it works in another post soon.
The other 20% are also being worked on now with the new Polling API in PHP 8.6 and some recent RFCs on top for 8.7: I/O Hooks and IO Ring.
This blog post on ZTS has been ready for a while, but this weekend a discussion about the Docker PHP image ZTS switch to 8.6 put this topic on the top of everyone’s mind.
Sadly the discussion revolves around a lot of misconceptions and FUD being spread, when essentially running PHP-FPM with ZTS has hardly any effect at all, and 99% of users will not even notice the switch.
What is ZTS?
PHP can be compiled either be multi-threading aware (ZTS = Zend Thread Safety) or unaware (NTS = No Thread Safety).
For a runtime that scales primarily through multiple processes and forking, thread safety is not an interesting quality by default. Apache with mod_php and PHP-FPM scale through processes, and this is why PHP on Linux is exclusively compiled with NTS mode by distributions.
With FrankenPHP as a new SAPI with rising adoption, PHP with ZTS became much more important. FrankenPHP scales through threads, and not through processes. Each request is handled by a thread, and all threads are running in parallel in a single FrankenPHP process.
But you don’t have to use threads to isolate requests from each other; it allows you to run blocks of code in parallel in the same request. Then you can scale PHP through processes first on the request level, and then through threads to parallelize different code.
Why is ZTS not default already?
Thread Safety sounds good; why is it not the default already?
There have been some pretty big drawbacks to ZTS in the past, but they have been addressed over time:
- Executing code with PHP ZTS was historically always slower than with NTS. This gap was only recently closed with PHP 8.6 with several patches from Marc Henderkes.
- In PHP 5 writing a ZTS-aware extension required using a set of macros correctly over and over in the code base: at function declaration and at function call sites. It was pretty tedious. A handful of extensions did not have ZTS support. These ZTS-aware macros are not needed anymore since PHP 7.
- Due to build complexity, distributions usually did not build ZTS. As such, not a lot of code ran on ZTS to increase the collective trust in its reliability.
These and a dose of “never change a running system” have kept ZTS away from the spotlight until FrankenPHP came along.
Anyone running a PHP application should not see any difference in the runtime being NTS or ZTS. The main difference that might be noticeable is the different handling of signals, meaning that max_execution_time uses a different way of counting time.
Build complexity problem
Distributions haven’t previously built ZTS versions of PHP: in Debian, Ubuntu, CentOS, and all the others, PHP is always built in NTS mode.
This makes it extremely challenging to get a version of PHP with ZTS to use with ext/parallel for example.
With FrankenPHP this probably really surfaced for the first time, so much so that they came up with the Static PHP CLI project to help them with the build process.
If PHP could only be built in ZTS, then we would get rid of this problem. Every distribution automatically builds in ZTS mode, and then ext/parallel can be used out of the box, and the shared objects could be linked into FrankenPHP and simplify their build step.
This would also save a lot of CI resources that extensions and php-src currently have to do running against both NTS and ZTS builds.
Road to ZTS as default
Since PHP 7 and 8, extension authors did not really have to think about ZTS anymore. Usually extensions would just work with ZTS unless they are using non-thread-safe APIs of third-party libraries. So for extension authors, it’s not a hurdle anymore.
With increasing FrankenPHP adoption, there is now a lot more code that is run on a ZTS version of PHP, increasing the confidence in its production readiness.
Now changes in PHP 8.6 make ZTS finally almost as fast as NTS thanks to the work of Marc Henderkes, and more overhead reductions are being worked on.
If we can truly reduce the overhead of a ZTS runtime to the point where it’s exactly as fast as NTS, then maintaining two ways of doing things in PHP is a waste of time and resources.
On an issue in the Docker repository, Marc lists the technical steps to complete this work.
I believe ZTS is the future, and that is why we at Tideways are putting our time and expertise behind making it a reality for PHP 8.7.
