What is gRPC?
4 min readFeb 4, 2021
อ่านเวอร์ชันภาษาไทยได้ที่นี่
gRPC is an HTTP transport protocol. Before getting to know it in detail, let’s see why it was invented? What wrong with the existing protocols for instance REST?
Why gRPC was Invented?
You may see from the following timeline that we have been using HTTP/1.1 for more than 15 years and there were many protocols/mechanisms released over the years, for instance, REST and GraphQL. Finally, in 2015 the HTTP/2 was released 🎉.
HTTP/2 is a game-changer ❗️
The HTTP/2 protocol has several prime differences from the HTTP/1.1
- It is a binary protocol rather than text. It can no longer be read and created manually. Despite this hurdle, improved optimization techniques can now be implemented.
- It is a multiplexed protocol. Parallel requests can be handled over the same connection, removing the order and blocking constraints of the HTTP/1.x protocol.
- It compresses headers. As these are often similar among a set of requests, this removes duplication and overhead of data transmitted.
- It allows a server to populate data in a client cache, in advance of it being required, through a mechanism called the server push.
- It allows streaming from client -> server, server -> client, and bidirectional.
Can we just use REST on HTTP/2 ❓
Yes ❗️
What are REST problems, then?
- REST cannot optimize using of some HTTP/2 features.
- Textual representation(JSON).
- Implement streaming is complicated.
- Bi-directional streaming is impossible at all. - It needs to implement authentication, load balancing, tracing, health check, etc.
- No formal machine-readable API Contract.
- So, it needs humans or third-party library to create it
- Imagine providing and maintenance 5+ client libraries.
What is gRPC?
gRPC is a opensource, modern and high-performance RPC framework
- RPC stands for Remote Procedure Call framework. It is a set of tools that enable calling a piece of code in a remote process as if it were a local procedure call, for example, SOAP, WCF, Thrift etc.
- gRPC was initialized by Google and used internally before opening it to the public.
- Right now it is a part of the Cloud Native Computing Foundation (CNCF).
- There are many great companies adopted and using gRPC in production.
How does it work?
From the above diagram, you may see that
- On the server: it needs gRPC Service middleware to handle gRPC requests/responses. The middleware uses Proto Buffers for data serializing/deserializing.
- On the client: the middleware is called gRPC Stub.
- It communicates over HTTP/2.
gRPC vs REST
Summary
- We may say that HTTP/2 is the next generation of HTTP/1 and gRPC is the next generation of REST.
- You may consider using gRPC on your projects
- For internal APIs: try it on a new service then gradually migrate existing services after you and your clients getting proficient.
- For public APIs: may wait until gRPC gets widely used or run gRPC API alongside REST API until your clients can keep up with it.
- GraphQL still being a good choice for some cases like queryable and aggregatable APIs.
Reference
- High-performance Services with gRPC in .NET Conf 2020
- https://grpc.io/: consist of gRPC history, who is using it and why, supported languages and tutorials, etc.
- gRPC Conf 2020 videos: consist of many interesting topics from great companies sharing their experience with gRPC.
- https://github.com/grpc/grpc-web: an open-source repository for gRPC for web browsers.