PHP or Go

MTR Design
4 min readDec 26, 2018

--

Answering this question depends on your demands, but in case you need high performance, the choice is clear.

First, let’s shed some light on our own experience. We had to create a RESTful API for an ad platform (which we are actively developing in the last 3 years) which is currently handling around 50 millions ad events (impressions and clicks) per day. The dev team included two seasoned PHP backend developers, the platform itself is also developed with PHP, so it made sense to decide to stick to that and write the API in PHP.

As you are aware PHP is an easy language, relatively fast and provides plenty of useful tools. So we built our API in PHP and were pleased with it — it worked, looked nice but still we decided to give Go a try. You may wonder what was the reason for our decision. The answer is we had some concerns about the performance of the API and were looking for alternatives. Although we had no previous experience with Go we decided to use this language and to rewrite the API in it.

My personal opinion about Go is that it is an interesting language, similar to C but without too many integrated tools. On the other hand it has plenty of packages and libraries. It’s strictly typed language and if your previous experience has nothing to do with such kind of programming languages than you may have a hard time. But strict as it is Go teaches you some discipline.

Our first job was to find some useful packages for the REST service. The requirements the package had to meet were speed and to have some rate limiters.

After doing some research and initial tests we decided to try 3 packages — Chi (https://github.com/go-chi/chi), Mux (https://github.com/gorilla/mux) and Gin (https://github.com/gin-gonic/gin). They are quite similar, but Gin has a lots of features. Since we looked for performance we decided to make a simple test with response static JSON using 100000 request and 100 concurrent connections.

The results were as follows:

Looking at the test results our choice was to go with Mux — it was a bit faster than Chi and nearly two times faster than Gin.

Now the trickiest part of the work was ahead us — we had to write the API.

The Go’s package system is a little bit strange to me, and we were as well not certain how to structure the project. Looking at it now I assume we could have used different approach but even the way it turned out the design looks nice.

When we were ready with our Go RESTful API, we were eager to perform tests to compare it with PHP’s performance. In the beginning the results were almost the same. That was quite a surprise, because the test on the local machine showed different values. We started looking for the reason behind these results and in the end we found it. We use Nginx as reverse proxy and it has some limits in configuration. Hence our first results were not what we expected them to be. So we resolved that issue and did the test one more time.

For the test we used 1000 requests and 100 concurrency:

Although there was a striking difference, we decided to try with more queries — 10000 requests and 100 concurrency:

At the higher load the PHP started to return failed requests, so we did another test without Nginx using only Go. This time we used 10000 requests and 1000 concurrency for the test and the results were interesting:

It was obvious that Go without Nginx worked faster and steadier. This made it clear how to proceed further.

We were clear to go with our next task. Since we wanted to use different domains on one port we had to find small and fast proxy for our Go API. Which we did.

So in case you need to build a high loaded application operating tons of requests we strongly recommend Go and our approach if you find it useful.

Originally published on the MTR Design company website.

MTR Design is a small Bulgarian web development company with expertise in a wide range of technologies (PHP, Python, Golang, Lua, Salesforce, Node, React and React Native, Angular, VueJS, iOS and Android development). We are currently open for new projects and cooperations, so if there are any projects we can help with, please react out (use the contact form on our website, or email us at office@mtr-design.com) and we will be happy to discuss them.

--

--

MTR Design

MTR Design, mtr-design.com, is a Bulgarian software development consultancy with proven track record in delivering complex, scalable web solutions.