PHP thread safe vs non-thread safe (ZTS vs NTS)
Codemia Knowledge Hub ·
Thread Safe (TS) and Non-Thread Safe (NTS) are two build variants of PHP that differ in how the interpreter handles concurrent execution. TS PHP includes internal locking so it can run safely inside multi-threaded web server processes like Apache with mod_php. NTS PHP omits that overhead and is meant for setups where each request runs in its own process, such as PHP-FPM with Nginx or Apache with mod_fcgid.
Picking the wrong variant does not always produce an obvious error. In one direction you get subtle data corruption under load, in the other unnecessary performance overhead.
What ZTS actually does
PHP's thread safety mechanism is TSRM (Thread Safe Resource Manager), historically also called ZTS (Zend Thread Safety). When PHP is compiled with ZTS, every global variable and internal resource lives in thread-local storage instead of one shared location. That stops one thread from corrupting another's state when several threads run PHP code in the same process.
The cost is real. Every access to a global resource goes through an indirection layer that looks up the correct thread-local copy. In benchmarks, ZTS builds typically run 5-15% slower than NTS builds for the same workload, depending on the application.
You do not write different PHP code for TS and NTS builds. The difference is entirely in how the interpreter manages internal state.
Which variant your server needs
The choice depends on how your web server invokes PHP.
Non-Thread Safe (NTS)
Use NTS when PHP runs as a separate process per request. This is the most common production setup today:
- Nginx + PHP-FPM (each FPM worker is a separate process)
- Apache +
mod_fcgidormod_proxy_fcgi - CLI scripts (single process, no threading)
- Docker containers running PHP-FPM
Each process handles one request at a time, so there is no shared state between concurrent requests and thread safety is unnecessary. NTS builds are faster because they skip the TSRM overhead.
Thread Safe (TS)
Use TS when PHP runs inside a multi-threaded web server process where threads share the same process memory:
- Apache +
mod_phpon Windows (worker MPM or winnt MPM) - IIS + PHP via ISAPI or the FastCGI handler in some configurations
- Any custom embedding of PHP in a multi-threaded application
On Linux, Apache's prefork MPM spawns separate processes, not threads, so even mod_php with prefork is safe with NTS. The TS requirement mainly applies to Apache's worker or event MPMs with mod_php, and to Windows IIS deployments.
| Architecture | Process Model | PHP Variant | Why |
| Nginx + PHP-FPM | Multi-process (one request per worker) | NTS | No shared memory between workers |
| Apache prefork + mod_php | Multi-process | NTS | Prefork uses processes, not threads |
| Apache worker/event + mod_php | Multi-threaded | TS | Threads share process memory |
| Apache + mod_fcgid | Multi-process (external PHP) | NTS | PHP runs in separate processes |
| IIS + FastCGI (Windows) | Depends on config | TS (recommended) | IIS may use threads internally |
| CLI scripts | Single process | NTS | No concurrency |
Checking your current build
The output shows either NTS or ZTS (Thread Safe):
Or check from code:
Extensions must match the build
Every loaded extension must also be thread-safe for the interpreter to work correctly in a threaded environment. An extension compiled without ZTS support loaded into a ZTS build can cause segfaults or data corruption.
Most popular extensions (PDO, mbstring, json, openssl) support both variants. Some older or niche extensions have only been tested with NTS. If you depend on one of those, you are effectively locked into NTS and a process-based architecture.
Performance difference
The gap is measurable but not dramatic for most applications. It comes from TSRM's thread-local lookups on every internal global access.
Numbers vary by workload. CPU-heavy applications with many internal state accesses see a larger gap. I/O-bound applications (database queries, API calls) see a smaller one because PHP execution speed is not the bottleneck.
In practice, use NTS with PHP-FPM unless your deployment specifically requires a threaded architecture. PHP-FPM is the standard for modern deployments and what most hosting providers, container images and cloud platforms configure by default.
Installing the correct variant
Linux
Most Linux package managers install NTS by default, including the official PHP packages and Ondrej's PPA.
Windows
The PHP downloads page (windows.php.net) offers separate ZIP files for TS and NTS:
- IIS or Apache worker MPM: download the Thread Safe (TS) version.
- Apache with mod_fcgid or standalone CLI: download the Non-Thread Safe (NTS) version.
Docker
Official PHP Docker image tags indicate the variant:
Run php -v inside the container to confirm.
Mistakes that cause crashes or wasted overhead
- TS build with PHP-FPM. FPM uses process isolation, so thread safety adds overhead with no benefit. Use NTS with FPM.
- NTS build with Apache worker/event MPM and mod_php. Threads share memory without protection, which can cause random segfaults and data corruption under concurrent load.
- Assuming TS makes your application code thread-safe. TSRM protects PHP's internal state only. Concurrent access to files, databases or caches still needs proper locking or transactions.
- Mixing TS and NTS extensions. Loading an NTS extension into a ZTS build (or the reverse) typically causes a startup error or a segfault.
- Not rechecking after an upgrade. Some upgrade paths or package manager changes can switch the variant silently. Verify with
php -vafter every major upgrade. - Confusing interpreter thread safety with application concurrency. PHP's request model is share-nothing at the application level. Even with TS PHP, each request gets its own variable scope.
Related reading
- What kind of problems if any would there be combining asyncio with multiprocessing?
- What language level support if any does Swift have for asynchronous programming?
- What Limitation of stdasync is Stroustrup Referring To?
- What operations are atomic in C?
- What operations in Java are considered atomic?
- What q.defer really does?
- What resources are shared between threads?
- What server-side architectures could provide high availability and avoid race conditions?
System design interview problems on this topic
- Design a Distributed Locking System: solution and walkthrough
- Design a Distributed Counter: solution and walkthrough
- Design Ticketmaster: solution and walkthrough
- Design a Flash Sale System: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.