Speed up your .NET microservice with gRPC

For those who still don’t know, gRPC is a RPC (Remote Procedure Call) framework with high performance in mind. Born in Google and used for years, it has been recently released to the open source world and now is supported in about 10 specific languages: from C# to Java, C++ or Go.

In this post we’ll made a really basic overview focusing on the basic concepts of gRPC and its main characteristics.

On my way to gRPC

RPC is not a new concept. We’ve started speaking about RPC in the late sixties but the first real implementation comes in the early 80s. It become very popular only in the nineties with Java Remote Method Invocation. In the following years we try to manage RPC in many ways: CORBA, .NET Remoting and WCF are just some example of this attempt.

But, why gRPC? Well, in the last few years, new scenarios comes - think about the increased number of connection from mobile device - and we need to adapt software architecture to responde to this new requirements: interoperability, openness, scalability, performance. gRPC was created with exactly these characteristics in mind and is based on two different technology: HTTP/2 and Protobuf.

HTTP/2 stream

HTTP/2 represents the evolution of the most famous HTTP/1.1 protocol, providing the best way to transport data by supporting binary and an optimized connection usage (single connection vs multiple connection). You can read the HTTP/2 RFC 7540 or get a short description here. The transmitted binary data, when using gRPC, is represented by the use of Protobuf.

Protobuf is a “Google’s mature open source mechanism for serializing structured data”. So, what does this mean? One of the big problem with REST is that you don’t have something like a WSDL doc that describes the service and helps you build a client, but you need documentation or samples before you can do it. With Protobuf you can write the service and data structure in a metalanguage and, later, you can use specific-language generator to build your own client and server side code.

Take this example:

message RequestMessage
{
  string FirstName = 1;
  string LastName = 2;
}

message ResponseMessage
{
  string FullName = 1;
}

service NameService
{
  rpc GetFullName (RequestMessage) returns (ResponseMessage) {}
}

You can build any supported language-specific code starting from this .proto file, so you can simply share that file with any client that needs to interact with your service. Wonderful!

gRPC in .NET

You can write services and clients in C# with gRPC by using two different libraries:

The first one supports .NET Framework 4.5 and .NET Standard 1.5/2.0 while the second one supports .NET Standard 2.1 so you can use it only with .NET Core 3.0.

In the next posts we’ll explore the two implementations, checking the difference and how you can use it. In the meantime, on the official gRPC site, you can find guides and tutorials to start using the brand new microservice framework.

Enjoy!

Found a problem? Edit this post