Difference between PHP thread safe and non thread safe binaries

Categories:

Since the release of PHP 5.2.1 back in Feburary there has been two new binary packages available for Windows, the non thread safe PHP binaries and the non thread safe PECL binaries. Since then I've read many threads in Internet forums where there seems to be a bit of confusion on what these extra binaries are, how they are used, and what effect they have. In this article I'll discuss the pro's and con's of these new binaries.

Since PHP first came to Windows back on the 20th of October 2000 with the release of PHP 3.0.17, the Windows binaries have always been released as thread safe packages. The reason for this is that Windows uses a multi threaded architecture as opposed to the multi proccess architecture of Linux and Unix. The problem this has when using PHP on IIS in CGI mode is that it makes it very slow as CGI was built on a multi process model, not a multi threaded model. On the flip side the problem this has when using PHP on IIS with the much faster ISAPI module is that there are several popular PHP extensions that have been developed with only Unix/Linux in mind (multi proccess model), and actually cause the PHP ISAPI module to crash on IIS. This making CGI the most stable environment for PHP on IIS, with the major disadvantage that it is terribly slow due to it having to load and unload the entire PHP environment from memory everytime there is a request.

There have been a few options available for a while to get PHP performing well on IIS. Firstly is the use of an opcode cache such as eAccelerator which stores PHP scripts in a partically precompiled state on disk and/or memory which drastically decreases script execution time. Another option is to configure IIS to use PHP in FastCGI mode which allows PHP processes to be recycled rather than killed off after each PHP request and also allows you to run several PHP processes at once, making PHP much much faster with the added bonus that as it is using the CGI interface there is little or no incompatibility issues with PHP extensions. This is still the fastest way to serve PHP, and is the way the IIS Aid PHP Installer is configured to install PHP on your IIS Environment.

What the non thread safe binaries allow you to do is to configure IIS (and other Windows based webservers) to use PHP as a standard CGI interface with massively increased performance as the PHP process is not required to wait for thread syncronisation. The performance increase is not to be sneezed at either as I've seen figures of upto 40% mentioned (though I'm yet to confirm myself), but it is still not as fast as the opcode/FastCGI method mentioned above. One of the biggest catches I've seen people getting themselves hooked on is that non thread safe binaries cannot be reliably used with the thread safe ones, and vise versa. This means that (for the moment at least) you cannot use opcode cache systems such as eAccelerator to give your non thread safe PHP environment a boost in the arm as they are all currently compiled as thread safe.

If the non thread safe binaries are not as fast as what you can configure the thread safe binaries to be then what is the point you ask? Here we come back to FastCGI, and in particular the efforts Microsoft have been making over the last year or so with the development of their own FastCGI handler. This new FastCGI handler from Microsoft enables you to configure the non thread safe PHP binaries in FastCGI mode, which is one massive shot in the arm for performance. Easiest way to put it is using the non thread safe PHP binaries with Microsoft's new FastCGI handler is like putting twin turbos on your car (without the inherent risk of blowing up your engine), and there is little doubt in my mind that this will be the future of PHP on IIS.

Average rating
(94 votes)

Comments

Anonymous's picture

FastCGI GoLive

hi,

thanks for writing all these excellent articles!

on 24 sep 07 we announced the golive version of fastcgi for iis 6: FastCGI beta go live

similar bits are available on server 2008 rc0, also announced 24 sep 07

please let us know if you find any troubles on the fastcgi forums at iis.net.

cheerz,
rickj.
(microsoft fastcgi dev)

Brashquido's picture

Hey Rick, That is great news

Hey Rick,

That is great news about the FastCGI beta release, thanks for letting me know! I have had a few issues with TP2, and will have to revisit them to see how they are now. From memory I had a few issues when using it with Helicon's ISAPI Rewrite 2.x. Will have to check though...
----------------
Dominic Ryan
3 x Microsoft IIS MVP, MCSE, MCSA
IIS Aid owner/webmaster

Anonymous's picture

FastCGI released

The production version of FastCGI was released in 2008 (and it kicks ass). This article should be updated to reflect that FastCGi is now live - the following text in the article is no longer valid: "Only downside is that the FastCGI handler from Microsoft is not even at beta level yet, so it will still be quite some time until it is production stable".

Brashquido's picture

Noted, and updated

Thanks for the catch. Article is now updated.

----------------
Dominic Ryan
4 x Microsoft IIS MVP, MCSE, MCSA
IIS Aid owner/webmaster

Anonymous's picture

TYVM

This helped me understand the difference between the two flavors of PHP binaries. Your article was easier to understand although the subject was complicated.

Anonymous's picture

thread safe and non-thread safe

Hello..
this is Mithilesh. I m using windows vista and want to install php on my computer. can anyone help which one is better for me either thread safe or non thread safe.

Thanx n Regards
Mithilesh.

Brashquido's picture

Non-thread Safe

Unless you have a specific requirement to use PHP ISAPI, I would be using the non thread safe binaries.

----------------
Dominic Ryan
5 x Microsoft IIS MVP, MCSE, MCSA
IIS Aid owner/webmaster

Anonymous's picture

Cannot Use FastCGI due to lack of extensions

Adam here:

We are currently running IIS with PHP v5.2.12 at our place of business. I would like to upgrade to PHP 5.3.2 FastCGI, however, there are a couple of extensions that are not there when I install the VC9 x86 non-thread-safe version of PHP. Such as MSSQL Support and PDF Support.

With our current setup we are experiencing random crashes. Any suggestions on what to do?

Brashquido's picture

Check event log

If your primary reason for upgrading is these crashes, then I'd be looking at your Windows and/or php log first to see what is causing the crashes.

----------------
Dominic Ryan
5 x Microsoft IIS MVP, MCSE, MCSA
IIS Aid owner/webmaster

Anonymous's picture

re: Cannot Use FastCGI due to lack of extensions

Adam,

Hi. It looks like losts of things are happening at Microsoft with respect to getting PHP and MSSQL running nicely on Windows. E.g.:

http://technet.microsoft.com/en-us/library/ee229548(SQL.10).aspx
http://msdn.microsoft.com/en-us/library/cc793139(SQL.90).aspx

Once you've got it all set up PHP NTS does seem to run quite nicely on Windows Server with FastCGI and MSSQL.