Laravel and Golang, a match made in heaven

Laravel and Golang, a match made in heaven

I have been tinkering with an app idea for some time now and I think its coming along really well.

One of the main functionalities in the web application is checking some web services every 30 seconds to see if they are up. Obviously, this is CPU intensive.

I have absolutely loved Laravel ever since I started coding in it, the ease with which someone could build a full-fledged complex web application is just unparalleled. And to date it remains the best framework out there to build a moderately complex web application.

It has its limitations though. Not Laravel, but PHP in general.

I experienced the limitations first hand when the Laravel scheduler would consume close to 6% of CPU for checking those web services for uptime. One solution would be to not have the sub 1 minute checks but then that would mean knowing about a web service being down only after a minute. A huge compromise.

No matter how well you optimize your code, there is no way to solve the problem since PHP is by nature blocking. While one instruction is being executed, others have to wait. And if you have multiple cores, just start multiple processes. Most of the wait times generally happens because of I/O operations.

For example, Send a API request., wait for the response. Which is the main operation in my application.

I wanted to explore an alternative. This is where I decided to give Golang a try.

And boy its impressive! The concurrency model is just too good to pass up.

Just take a look at the below graphs. Included both my local system as well as observations from my production server (HETZNER).

I always dockerize my laravel applications for ease of building and deploying.

Here is a screenshot from my docker desktop application. With Laravel scheduler and Supervisord (MacBook Pro M4 with 12 cores)

Same functionality but completely rewritten in Golang (Also from my MacBook). I had stopped the laravel scheduler that does exactly what the Go service does in this screenshot.

Here are the screenshots from my HETZNER server (8GB RAM/2 CPUs – dedicated system). I have deployed my application in docker container.

Without using the service built in Golang, the CPU usage is constantly hitting 200% which is the capacity of the server.

Contrast that with exactly same functionality built in Golang. I stopped the Laravel scheduler here and am using the Golang service that does exactly the same.

I am not making a case to not use Laravel. I use it and I will continue to do so and I am actively building my application using Laravel.

However, for my application, it is clear that I would need to use Golang for specific purposes, simply because it is so much more efficient and when it comes to business this matters. Otherwise, you end up scaling servers which is expensive. And as a bootstrapped company you probably know what that means.

Laravel gives you the ecosystem to rapidly build your web application so you can get it as fast as possible in the hands of customers, while Golang lets you speed up the time-consuming and CPU/Memory intensive parts. The combination works really well.

Cheers!

Madhukar Prabhakara Avatar