PHP
Thread Safety
Programming
Web Development
Non-Thread Safety

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.

php
1// Conceptually, what TSRM does under the hood:
2// Without ZTS:
3//   global $counter;  // one shared location
4//
5// With ZTS:
6//   global $counter;  // resolved to thread-local copy via TSRM lookup

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_fcgid or mod_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_php on 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.

ArchitectureProcess ModelPHP VariantWhy
Nginx + PHP-FPMMulti-process (one request per worker)NTSNo shared memory between workers
Apache prefork + mod_phpMulti-processNTSPrefork uses processes, not threads
Apache worker/event + mod_phpMulti-threadedTSThreads share process memory
Apache + mod_fcgidMulti-process (external PHP)NTSPHP runs in separate processes
IIS + FastCGI (Windows)Depends on configTS (recommended)IIS may use threads internally
CLI scriptsSingle processNTSNo concurrency

Checking your current build

bash
php -v

The output shows either NTS or ZTS (Thread Safe):

 
PHP 8.3.6 (cli) (built: Apr 11 2024 12:00:00) (NTS)

Or check from code:

php
1<?php
2if (php_sapi_name() === 'cli') {
3    echo "SAPI: CLI\n";
4}
5
6echo "Thread Safety: " . (defined('ZEND_THREAD_SAFE') && ZEND_THREAD_SAFE ? 'Enabled' : 'Disabled') . "\n";
7
8// Or use phpinfo() and look for "Thread Safety => enabled/disabled"
9phpinfo(INFO_GENERAL);

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.

bash
# Check whether this PHP build has ZTS enabled
php -i | grep "Thread Safety"

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.

 
1Benchmark: Simple request/response cycle (PHP 8.3, same hardware)
2
3NTS (PHP-FPM):   1,247 requests/sec
4TS  (mod_php):   1,089 requests/sec
5
6Difference: ~12.7% throughput reduction with TS

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.

bash
1# Ubuntu/Debian (NTS by default)
2sudo apt install php8.3-fpm
3
4# Verify
5php -v  # Should show (NTS)

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:

dockerfile
1# NTS (FPM) - most common
2FROM php:8.3-fpm
3
4# CLI (NTS)
5FROM php:8.3-cli
6
7# Apache with mod_php (TS on some builds, but typically prefork/NTS)
8FROM php:8.3-apache

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 -v after 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.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions