Say hello to a blazing fast gRPC Server 👋 (C++)

4 min read Original article ↗

Uditha Atukorala

It’s been a while since I wrote about the frustrations of using the official gRPC C++ implementation and started working on a better alternative. It took a few months but gRPCxx is finally production ready!

It’s fast, simple and flexible.

  • Fast: 3x faster compared to the official implementation(s).
  • Simple: Overall smaller codebase and 95% less generated code compared to the official implementation.
  • Flexible: The application code is given greater implementation choice by using C++ concepts instead of being restricted to one choice.

And it’s Open Source!

Prologue

In early 2023, I started working on an Open Source project with the intention of creating something that could do access control at scale. Let’s call it Project X.

Project X was designed to be an internal (backend) component but performance critical, so using gRPC for the communication protocol was a natural choice and I’ve had previous good experiences with gRPC in Go.

As for the programming language for Project X, I decided on C++ without giving it a lot of thought. But I soon realised the official C++ implementations of gRPC are very difficult to work with and made the codebase very cumbersome. Quite frankly Project X ended up being a pile of 💩.

In hindsight, using C++ was a bad choice. But hey, without that bad choice I wouldn’t have gone down the rabbit hole of gRPC and ended up creating gRPCxx 😬.

Search for something better

After getting frustrated with the official gRPC C++ implementation, begging it to be better and shouting at it with very strong profane words 🤬, I eventually realised I need to find something better.

After all, the official gRPC C++ implementations were giving C++ a bad reputation. As someone who loves C++, I felt obliged to do something about it.

So I did some soul searching, scoured the internet I convinced myself I could write something better. My goal was to create something simple and straight forward to use and showcase the beauty of modern C++.

Birth of gRPCxx

It took a few weeks of part-time effort to get a basic gRPC server implementation working and another few months of tweaking to make it fast and have enough features to be able to use to build gRPC server applications.

On 20th of December 2023, the first version of gRPCxx (v0.1.0) was released.

But there were issues and bugs 🙈. I identified some of the bugs and thankfully there were others who found bugs and reported them on GitHub. Eventually the first stable release, v0.1.4, was released on 12th January 2024.

Improving throughput (QPS)

gRPCxx started life being more than 2x faster than the official implementations. Not just the official C++ implementations, but the official Go implementation and unofficial (but popular) Rust implementation too.

More info on how the benchmarks results were achieved can be found here.

I used worker threads and TCP socket duplication to hand-over new connections to worthers in gRPCxx v0.1.x versions. Although I could achieve high throughput figures, TCP socket duplication meant this isn’t Windows compatible.

While I was experimenting with a request to make gRPCxx compatible with Asio/Boost.Asio I had a 💡 moment to make some changes to keep all I/O on the main thread and using C++20 coroutines to offload processing either to worker threads or do processing on the main thread.

This change made significant improvements to the throughput figures pushing QPS beyond 450k for the first time.

Now there’s a chasm between gRPCxx and the rest of the implementations in terms of throughput figures 😯.

Epilogue

This is (very likely) the last entry in the devlog about the journey of gRPCxx. But that doesn’t mean gRPCxx has become stagnant.

I’m actively working on improving gRPCxx and using it to build gRPC server applications (e.g. ruek). There are also others who have found gRPCxx useful as a better alternative to frustrating official implementations.

Use GitHub discussions to connect with the community and get involved with the project.

If you find this useful, please consider adding a ⭐️ on GitHub.