Forest header image

Symfony Finland
Random things on PHP, Symfony and web development

Running Symfony Applications with PHP-PM or PHPFastCGI

PHP is a shared-nothing platform. This means that each time a request comes to your server your PHP application starts anew. PHP-PM is a way of running PHP in an alternative way, by maintaining a pool of PHP applications waiting for a request to come in.

PHP's shared-nothing architecture allows easy scaling of PHP applications, simply by adding more servers. You'll need to handle session sharing, etc. yourself using Redis or other similar technologies.

As your application starts up and dies in a short while there has been little worries about memory leaks, but the down side of this that your server needs to bootstrap everything each time a request comes in. This means repetition of basic tasks like loading configurations, etc.

The above can be alleviated a significantly with smart caching strategies, but the overhead will always be there. That is how PHP was built to work:

The shared-nothing architecture of PHP where each request is completely distinct and separate from any other request leads to infinite horizontal scalability in the language itself. PHP encourages you to push scalability issues to the layers that require it.

PHP's claim to fame was the LAMP stack. It remains very easily approachable with tools such as WordPress. But at the same time PHP has evolved to be a tool for creating complex web applications in a structured way.

Async PHP?

PHP was originally used as an inlined script to generate some output easily. The aforementioned growth in complexity of applications built with PHP means that the overhead of bootstrapping each request has grown to be significant in some cases.

In the early 2010s the asynchronous programming paradigm became a staple among web developers, mainly to JavaScript and Node.js which forces everything to be async. As opposed to common belief that that PHP cannot be async, it can used in such manner: Getting started with Icicle

Early concept implementations of async in PHP were just POCs and responses to trolls (Benchmarking Codswallop: NodeJS v PHP), but some implementations have become quite credible, namely: AMPHP, Icicle and ReactPHP. The two latter are apparently applying for the PHP-FIG:

PHP-PM: An alternative way to run PHP apps

In the first part of this arcticle we discussed how PHP is run by default. But with the async tools at hand this could be done with a different way. Running PHP processes that are already bootstrapped and ready respond to your client requests without that overhead.

Marc J. Schmidt wrote about this approach way back in February 2014: Bring High Performance Into Your PHP App (with ReactPHP). Since that the PHP-PM project was created and ReactPHP remains in active development.

PHP-PM is a CLI process manager for modern Request-Response PHP Applications. It wraps around existing applications and allows them to be served in a more efficient manner. This is achieved by implementing PHP-PM HttpKernel Adapters.

Symfony and Laravel are the frameworks supported by default by PHP-PM. I modified the adapter, changing from app/AppKernel to ezpublish/EzPublishKernel, to run eZ Platform, a fully featured Content Management System built on the Symfony framework. And it booted up fine and at a quick glance things worked as expected.

I did not run any benchmarks to see whether or not there was any performance gain. My aim was just to validate that I could run a complex application in a very different way from the standard PHP execution methodology.

PHPFastCGI and future prospects

PHPFastCGI is another alternative to running PHP in the way that PHP-PM introduced. This project heard by Andrew Carter is a well built project with plenty of prospects. Learn more about Andrew's preliminary results with PHPFastCGI and a Symfony app and the excellent slides from PHPLondon describing the proposal of the technology with a real world example of a restaurant.

While all this is still early stages, it is good to see that even the age old LAMP platform continues to evolve. There are bound do be some new challenges if this way of running PHP applications would become popular, but as a proof of concept by Marc and the team it's once again validated.

The most exciting prospect is that it works with existing code, allowing reuse of existing functionality at a large scale.

Written by Jani Tarvainen on Saturday October 24, 2015
Permalink - Tags: php, symfony, php-pm, reactphp, icicle, amphp, phpfastcgi

« Free SSL Certificates from Let's Encrypt to Drive HTTP/2 Adoption - MySQL 5.7 brings sexy back with JSON »