10x faster requests thanks to Go

I like to try new things, new technologies and languages. And ideally in something bigger than just “hello world”. When we started solving the problem with a slow section of the application on Crossuite, we discussed two solutions. Either “stretch” this section to multiple servers or rewrite it. So I seized the opportunity to try a new language and after analysis, I rewrote the code into GoLang. When I deployed it, my jaw dropped. Literally.

I’m interested in every new language, especially when it’s based in the C. It’s what I grew up with and it’s still close to my heart. And even though Go was inspired by C/C++, it’s minimalist compared to them and it doesn’t require as much code writing. According to Internet sources, it should be equally fast, but with less “effort”. This information was my main motivation to try it. Another argument was, for example, that Google Search, Docker, Kubernetes or Prometheus applications, which we use to monitor data in the Cloud, are written in GoLang. 

In addition, I also read that Go has the so-called Goroutines, that is, running code in parallel. With a large number of requests, it’s important that one doesn’t wait for another one, and Go has resolved it already at the language level, not the operating system. Just starting a “parallel process” is therefore quicker. That was something I hadn’t encountered before, and it was prompting me to try out Go again.

Analysis by comparison

As part of the analysis, I wrote the same function in three languages that we currently use in bart – PHP, NodeJS and Python – and I also added Go. I let the code run a hundred times and watched how fast it went and how many resources (RAM, etc.) it “devoured”. I was pleasantly surprised by the result. Go has proven to be the fastest and most efficient language. Why?

PHP or Python are interpreted languages – instructions are converted from what you type into the machine code each time you run a program. Go is a so-called compiled language – after saving, it’s necessary to convert the code through the compiler to the program version, which can subsequently be started. Thus, the compilation is skipped during the start-up itself, it’s only performed again during changes.

The result of this process is a much faster application. In our case, on the Crossuite project, with the same functionality compared to Python, we had approximately 10 times faster requests and instead of 500 MB, RAM only took 15 MB. 

You need to choose with reason

Then why isn’t Go used everywhere? Because it has its specifics:

  • Strictness – Go is designed in such a way that if you include, for example, a library or define a variable in the code, but don’t use it in the end, Go refuses to compile the code. Nor does it contain many “modern” elements available in other languages (generics, inheritance, exceptions, etc.). So it makes you write cleaner, more structured code, and do more planning.
  • Little-known – Schools mostly teach “traditional” languages and so it’s difficult to find programmers who master Go. It’s therefore easier to write projects in PHP or NodeJS, in which new reinforcements in the company can orientate themselves faster.
  • Compilation duration – In robust projects, the compilation can take a long time. If it takes you 3 minutes to fix a bug, but the change takes half an hour to take effect, it isn’t very efficient. Fortunately, there’s a Go compiler, which, based on available sources, significantly reduces the compiling time. But I haven’t tested it on a big project yet.

Go, like other compiled languages, therefore isn’t suitable everywhere and its use needs to be well thought out. However, it has definitely paid off for us. Since this is a small part of the project, the compilation takes only about 15 seconds, and this loss is fully compensated by the speed of requests and saving resources.

It was basically a small change, the initiative of one team member who did an analysis and said, “Let’s give it a try!”. I got the green light and it worked. Now everyone on the team knows that GoLang is great and it motivates them to learn it too so that we can develop more in it.