The Path to PHP 7
The hurdles we overcame on our path towards PHP performance greatness!
At Kano we are always looking for ways to improve the performance of our code to ensure we are providing a quality experience to the players of our games. Throughout the past year we’ve taken on a few projects to improve game performance, some out of necessity, and others out of our desire to continually improve our framework. Highlights of these undertakings were:
- Upgrading memcached drivers to take advantage of igbinary for improved compression
- Caching refactor to ensure we are doing the optimal amount of cache operations
- SQL query refactor to ensure that we are accessing our databases as efficiently as possible with less time spent waiting for disk operations
While these changes did provide some modest improvements in latency and hardware utilization we felt there was still more to be done. We had read about Tumblr and Etsy who had migrated to PHP 7 and were reporting performance gains in the range of 50% in reduction of both request times and CPU usage. This is pretty amazing for a language version upgrade, and we were itching to try it on our own framework!
While we were keen to upgrade to PHP 7 there were a few hurdles to overcome. The first was the fact that AWS Elastic Beanstalk, a cloud service that our games infrastructure relies on, didn’t support PHP 7 with an official platform release until August of 2016. While there were ways around that, our infrastructure and build pipelines were complex enough that this was a blocker for us at the time, so the announcement of official support was met with excitement and was the catalyst for us to plan our upgrade.
Up next was the code itself. We knew there was deprecated code in our PHP 5 framework that would not be supported by PHP 7. For most part, the changes were minor — indeed, most of the syntactical and error-handling changes took only a couple of days. It was the changes in PHP extensions that would prove to be trouble.
We upgraded several of our major extensions (some had been deprecated or removed) but the single biggest problem we encountered was the old MongoDB drivers we were using were not supported under PHP 7 — there was a new set of drivers available that we had to switch to. Unfortunately, we discovered these drivers weren’t far along enough in development yet. Namely we needed BSON serialization support, which had been announced but was still unavailable at the time. Our upgrade took a backseat, as we would have had to re-tool a significant portion of our caching layers to work around this missing feature. When mongodb/mongo-php-driver version 1.20 was released in late November 2016, we cleared the roadmap for the new year, took a deep breath and dove in again.
With new drivers in hand we could begin the process of implementing them into our framework, however the differences between old and new proved to be non-trivial and required us to re-write most of our Mongo layer. After several week’s worth of dev work, we had a production environment without issues and we were finally ready to roll out PHP 7.
As soon as we rolled out PHP 7 servers and started redirecting web traffic to them, the improvements became dramatically apparent.
As noted by others who have upgraded to PHP 7, latency was drastically reduced. Average page load times were reduced from around 75ms down to 45ms, a reduction of 40%. Our outlying latencies, the p95 and p90 measurements (or the worst 5–10% of overall requests) were reduced from 145ms and 100ms down to 80ms and 56ms respectively, a reduction of about 45%.
The amount of processing power required by our web servers was also reduced by about 40%. You can see in the chart below that as traffic transitioned from PHP 5 servers to the PHP 7 servers, our average CPU load dropped from 25% usage down to about 15%.
We also saw a decrease in the amount of memory required by each request by about 55%!
The reduction in latency is the most important aspect of this upgrade as it is what will be noticed by the people who play our games. Improving the player’s experience in this manner means they can do more actions in less time, making for happier players, which helps drive retention and revenue. From an operations standpoint the gains from PHP 7 mean that it will take less hardware to support our products, a direct dollar savings on infrastructure costs. And finally from a developer perspective, upgrading to PHP 7 means that we can take advantage of some new features like the primitive and return type declarations which will help make our framework more robust.
Overall the upgrade to PHP 7 has been a great transition for Kano to help improve the performance of our games. Anyone who is currently running on PHP 5.x would do well to consider upgrading — the benefits are extremely positive.