Choices while designing!
Choosing a web framework is an extremely tough decision. We must really understand the requirements, use cases, and a few nonfunctional considerations before deciding on a framework. This article highlights a few design choices for High-Performance web APIs projects and contrasts both Golang and Rust across several factors.
Over the last couple of years Golang has matured to be a particularly good language for web application development. There are numerous frameworks that help developers quickly write REST APIs, GRPC APIs and a whole lot of performant backend systems. One of the main reasons for choosing Golang as a web framework is its performance, memory consumption, build times and developer productivity — and not to mention cloud native support. We’ll look at some of these factors in the coming sections and compare Golang and Rust. Something that often comes up within Rust vs Golang is memory management. To start with, even though Golang is a very efficient language, memory management is done via Garbage Collector.
Golang has a very efficient Garbage Collector. It’s very performant for most applications.
We’ll look at memory management in detail. Before that, let’s take a look at some of the most popular web frameworks for Go. ( Echo is starting to become my favorite. )
And many more. Please refer to this link for a comprehensive list. https://github.com/mingrammer/go-web-framework-stars
Now let’s talk about Rust. Traditionally Rust has been compared with C++ as a systems programming language. It’s been the focus of a lot of communities over the last couple of years. As a language, it is top tier with a lot of constructs that enable developers architect and write beautiful code. Personally, with Rust’s evolution, I think it is both a systems and general-purpose programming language —of course with the caveat of Manual memory management. This is exactly why Rust seems to have a steep learning curve.
Regardless of its learning curve and memory management, a lot of frameworks have emerged over the past couple of years that cater to Web and API development within Rust. Not to mention the fact they have one of the highest performing stats amongst a wide variety of benchmarks. Below are a few web frameworks in RUST world.
It is extremely simple to write an API in rust with the help of these frameworks in Rust.
But the challenge when writing production quality applications is not starting it. It’s maintaining it and really making sure that it can grow and satisfy business requirements over a period reasonably.
Let’s look at some of the factors that we must consider while choosing between these languages.
When it comes to performance, there is no doubt Rust is the leader. Most of the benchmarks between Rust and go will show that Rust beats go by at least 15–30%. That’s no surprise because Rust doesn’t have a garbage collector. All memory management is explicit. And developers manage memory by following a certain set of rules. Go, on the other hand has a Garbage collector that allows developers to not worry about managing memory. There are other reasons due to which Rust does perform faster as well. But an important thing to note is that there is a cost, we as developers must pay to write Performant Rust code. At this point , it is important to consider whether the functional requirements of your app justify writing code and investing in it.
It is simple to write a few
Hello World programs, and some simple CRUDs, but as your application grows, it’s going to be a pain if your team doesn’t have Rust developers at hand. As important it is to start development, it is even more important to support and maintain it over time.
We often think of microservices as some functionality exposed by REST API. That is true, but more often than not, REST APIs also handle a lot of complexity and abstracting complex actions within applications. For instance, multiple service interactions, transaction management, security, logging, object storage, message passing are all examples of the things APIs typically handle. While thinking and planning for your project, think about the available integrations, help, support, and time to develop these features. Think of a few future use cases / requirements that immediately follow.
There’s no simple answer for this to choose between Rust and Go. It depends a lot on the features and services you’re building.
In general, Golang has a huge community that has developed libraries / middleware for almost everything at the time of writing of this article. Cloud tools have great support for it as well.
Obviously, Rust doesn’t lag, because it’s turning out to be one of the most loved Languages in the world. But the community is still growing. And things may be a little difficult to work with when compared to go for certain aspects ( of course, generally speaking ) .
If you’re ready to really invest some time because your application is mission critical, and requires absolute performance, security and stability, you can spend time writing these yourself.
I hear folks talk about concurrency in Go a lot. And it’s just awesome. Rust has what is called ‘Fearless Concurrency’. This is where the compiler helps you avoid common concurrency issues like deadlocks, race conditions and memory issues. When comparing go and rust , i wouldn't take a side here. Its mostly about the ease of use that comes in here.
Both approaches have their own pros and cons. I would side with Rust on this in some cases due to its asynchronous programming primitives. But i love Go when i want to have some concurrency embedded within my program. It’s so straightforward out of the box.
In most cases the framework that you chose handles the concurrency aspect for requests and you don't have to worry about some basic ones.
When working in an enterprise, this is something that is very cucial. Like every other software architecture and design, your application requirements govern the framework and implementation. I would suggest choosing very carefully between Go and Rust. Think about the business as one of the important priorities. Ask yourself a question repeatedly whether choosing Rust over golang makes sense considering it would also be an effort hiring developers and getting them ramped up. Is the nature of the application such that it justifies the investment in Rust and Maintaining it.
Although, Rust’s compiler is great at what it does, there is a significant cost to training people over time and hiring as well. This is not a technical design choice, but an important one if you’re developing Apps within your organization. Golang on the other hand, has a small learning curve, easy semantics, and amazingly fast application development lead time.
I love both Rust and Go for their own use cases. And they’re one of my top choices for most of my developments. But i have learnt to decide very wisely between the two. It is also ok to prototype and build in Go Initially, and if it justifies, migrate to rust. After all, that’s the beauty of writing services.
For instance, if you’re in the business of building the next API for serving images from s3 to the entire world. There is no doubt, i would choose rust over go. But if you’re writing a crud admin API / an internal tool where garbage collection within Golang won’t have performance spikes or security issues pertaining to memory management, I would choose Golang.
Rust is excellent at avoiding memory issues, but at the same time we have great tooling to detect such issues in every other language. The choices you make may impact a project financially, productivity wise, and even deliverable in the long run.
EDIT 3rd Oct 2022: Our of curiosity, I did run a very simple performance test for a REST API implemented in both Rust and Golang. Although I am of the opinion that these don’t serve as a factor for moving to Rust — but it allowed me to get some numbers for future design considerations. Please refer to the articles below :