Interesting history on scaling PHP and the birth of HHVM and Hack

Published on Friday 16, August 2019

Keith Adams was on Software Engineering Daily to discuss his time at Facebook and their usage PHP. Keith Adams spent six years at Facebook and is now at Slack. What makes Facebook interesting is that they opted to make PHP scale rather than deconstructing Facebook into many different microservices (at that time.) What I did not also realize is that Slack is a PHP application as well - as Keith Adams wrote in his Taking PHP Seriously article in 2016.

When HipHop, HHVM, and advances in PHP7 were coming around I was not following all of the buzz very closely. By the time I had entered the realm of web development it was already 2012 and much of this was already underway. I ended up learning about HipHop, HHVM, and Hack as after the fact events as I attemped to track the development of PHP7.

One of the biggest problems within Facebook with PHP was CPU burn. PHP runs on a single thread for each web server request. This required a lot of CPU and made the application feel sluggish. Back in 2009, Facebook still was figuring out how it would monetize its platform. This is back when they unveiled the autocomplete feature for search when autocomplete suggestions used to "wow" and amaze us all. Remember when Facebook experimented with virtual gifts? And when games on Facebook were huge (we all had family playing Farmville.) Facebook was still in need of having to find out where money was coming from and it was aware of where it was going - to hardware.

The team at Facebook made it a focus to make PHP faster. This would reduce hardware cost and improve the speed of Facebook. One of the first iterations was HipHop. HipHop was a transpiler that took an entire PHP codebase and created a compiled C++ binary. This binary was then deployed and served web requests. Keith Adams reported it increased performance about two fold. Which is pretty great, considering what PHP was years ago. 

Due to lengthy compile times and development and product environment differences, the next iteration was the HipHop Virtual Machine - HHVM. The HHVM is a just-in-time (JIT) compilation execution engine. What's interesting is this was based off of a hackathon project to compile PHP into Javacript so that it could run on the V8 JavaScript runtime engine. While it sounds boggling, PHP and JavaScript were more inline than C++ in the way they are handled in ahead of time compilation. PHP and JavaScript are interpreted languages that have levels of dynamism which the V8 runtime is highly optimized for.

However, there were problems. PHP uses counters for garbage collection and V8 uses tracing. In the end the team decided to write their own JIT compiler, birthing HHVM.  This also ended up in the creation of Hack, a derivative of PHP that supports static typing, generics, and more. Before PHP7 these were big differentiators, but we are seeing PHP adopt many of these features.

Today HHVM no longer runs PHP and only supports Hack applications. 

The podcast made for a great history lesson on PHP, Hack, and HHVM. It's great to remember how much PHP has grown with 7.x and where it will go with 8.x. It's also rad to know Slack is a scaled PHP/Hack application.