Tuesday, February 5, 2013

PHP on Windows: The Evolution of Non Thread Safe Binaries and FastCGI

PHP non-thread safe binaries for windows

Since PHP version 5.2.1, Windows binaries have included two versions: Non Thread Safe (NTS) PHP and Non Thread Safe PECL binaries. This shift marked a significant departure from the previous norm, as earlier versions exclusively provided Thread Safe (TS) binaries.

Background: Why Thread Safe Binaries Were Initially Standard

When PHP 3.0.17 was ported to Windows in October 2000, only Thread Safe binaries were provided. This decision was influenced by the architecture differences between Linux and Windows:

  • Linux: Favors a multi-process architecture, where each process runs independently.
  • Windows: Utilizes a multi-threaded architecture, where threads share memory and resources.

PHP on IIS (Internet Information Services) often ran as a CGI module, which operated in a multi-process model. This mismatch resulted in slower performance, making CGI unsuitable for high-performance scenarios. To address this, a PHP ISAPI module was introduced, which was much faster than the CGI implementation.

However, the ISAPI module faced stability issues, frequently crashing with IIS. As a result, CGI became the most stable solution for running PHP with IIS, albeit at the cost of performance due to the overhead of reinitializing PHP environment variables for each request.


Solving Performance Issues: FastCGI and Opcode Caches

To mitigate PHP+IIS performance issues, two solutions emerged:

  1. Opcode Cache (e.g., eAccelerator):

    • Pre-compiles PHP scripts into bytecode to drastically reduce execution time.
    • Enhances performance but does not address the overhead of PHP process initialization for each request.
  2. FastCGI Mode:

    • Modifies IIS to handle PHP processes in FastCGI mode, where PHP processes are reused instead of being terminated after each request.
    • Avoids the repeated initialization of PHP environment variables and drastically improves speed.
    • Maintains compatibility with PHP extensions by using the standard CGI interface.

Introduction of Non Thread Safe (NTS) Binaries

Non Thread Safe binaries were specifically created to optimize PHP's performance in FastCGI mode. Key features of NTS binaries include:

  • No Thread Synchronization:

    • In a Thread Safe environment, PHP must synchronize threads to ensure safe concurrent execution.
    • In FastCGI mode, threads are not shared between processes, making synchronization unnecessary.
    • Removing thread synchronization improves performance by approximately 40%.
  • Better Compatibility with FastCGI:

    • NTS binaries are designed to work seamlessly with FastCGI, which leverages the standard CGI interface to improve stability and maintain compatibility with PHP extensions.

Impact of FastCGI with NTS Binaries

  • Performance Gains:

    • Reusing PHP processes significantly reduces initialization overhead, resulting in faster request handling.
    • Non Thread Safe PHP binaries eliminate the need for thread synchronization, further improving performance.
  • Improved Stability:

    • FastCGI resolves the instability issues of the old ISAPI module while maintaining the compatibility of CGI.
  • Adoption by Microsoft:

    • Microsoft introduced its own FastCGI handler for IIS, simplifying the configuration for using PHP in FastCGI mode.
    • This paved the way for broader adoption of PHP on Windows servers.

Key Takeaways

  1. Non Thread Safe Binaries:

    • Optimized for use with FastCGI.
    • Faster than Thread Safe binaries when thread synchronization is unnecessary.
  2. FastCGI:

    • Reuses PHP processes, significantly improving speed and efficiency.
    • Ensures compatibility with PHP extensions by adhering to the standard CGI interface.
  3. Modern IIS+PHP Setup:

    • Use Non Thread Safe PHP binaries with FastCGI for optimal performance and stability.
    • Avoids the legacy issues of the ISAPI module and improves upon traditional CGI limitations.