5 Reasons to use gRPC-Web over REST in 2021

Lightning Fast Performance with a 46% reduction in payload

Hammad Abbasi
4 min readDec 21, 2020
Image by : Markus Spiske

With the release of .NET 5, Microsoft has made gRPC really fast and introduced various performance improvements.

As per Microsoft's official blog post.

.NET 5 Server performance is 60% faster and .NET 5 Client is 230% than .NET Core 3.1

These stats are really tempting —even if you aren't dealing with the high-performance applications where milliseconds matter.

We will be using a sample demo: https://grpc-web.azurewebsites.net/ which I created using gRPC-Web (that uses javascript client to invoke gRPC over the browser and also fetches the same data using REST/JSON).

Image by the Author ©

(Note: If you are new to gRPC, you can learn more about it here. You may also refer to this article — where I explained what’s gRPC-Web and how to set up a real-world gRPC-Web Services using .NET 5).

In this article, we will discuss 5 reasons to use gRPC-Web over REST.

So let’s get started.

In the demo app, we have three methods to fetch some randomized in-memory data from the server :

  1. Get Products via gRPC (Server Stream)
  2. Get Products via gRPC (Unary Call)
  3. Get Products via REST

You can open dev tools -> Network panel to see analyze these requests.

Now consider below snapshot of theses requests:

gRPC vs REST API Calls — Image by Author

We can draw following facts from this:

  • You can see the first three requests are JSON requests and we are getting around 71.3Kb of JSON payload( not gzipped) in an average of 621ms.
  • Now same data when fetched with the unary call is taking about 313ms and the payload is reduced to 44.3 KB

gRPC-Web also supports server streaming where the server sends multiple responses against one request — We can also use this technique to fetch multiple rows from the server; in our case, it will be 1000 which is our sample size.

  • The streaming call takes around 48.6KB and it gets completed in 303ms.

Based on the above stats — Here are my 5 reasons to use gRPC over REST:

1 — gRPC is Lightning Fast:

It’s really fast (thanks to the guys at Microsoft). We can see the performance gain by 65%. It automatically translates to more resource availability at the server.

2 — Smaller Payload

  • You can see the payload size is reduced by 46% and thus it will consume less bandwidth than a typical JSON call.
  • It’s also platform and language agnostic like JSON.

3 — Faster Serialization

  • Another amazing benefit is that — it provides faster serialization as it uses protocol buffer serialization which unlike JSON is binary.
  • Provides faster Interprocess communication using the Protobuf(protocol buffers) messaging format, as REST/JSON was never considered an ideal solution in IPC scenarios.
  • It removes a lot of the dependencies in serialization/deserialization that JSON has to manage.

4 — Better use of HTTP/2

  • gRPC uses http/2 to support highly performant APIs and uses binary data — making efficient and compact communication.
  • While REST can only use the request and response model; a gRPC service can fetch data in streams which is a really powerful way to receive multiple messages from the server.

5 — It’s Flexible and Error-Prone

  • Just like REST — A gRPC Service can be consumed cross-language which makes it very scalable.
  • Unlike REST we don’t need to hunt down API documentation — API contacts are well defined in .proto file so both client and the server know, what to expect.
  • No more hand-crafted JSON objects. This is another area where gRPC shines — the requests and response objects ( also called messages) are strongly typed and with the tools, they are generated automatically.
  • Low-level networking methods are abstracted away in client implementations (which are also code-generated) — Thanks to its in-built protoc compiler.

Summary

REST has been dominating the modern API landscape for quite a while (and is likely to remain the de facto choice) but gRPC/gRPC-Web could surely be a potential game-changer when it comes to building highly performant systems. I hope this article helped you in understanding and evaluating the performance of gRPC-Web using .NET 5.

Feel free to drop a comment if you have any questions and I’d be happy to help.

--

--

Hammad Abbasi
Hammad Abbasi

Written by Hammad Abbasi

Innovating Enterprise Applications with AI & LLM | Solution Architect | Tech Writer & Innovator | Bringing Ideas to Life using Next-Gen Tech Innovations

No responses yet