A Short Introduction to gRPC
gRPC, short for “gRPC Remote Procedure Call,” is Google’s lightweight, open source RPC framework. In this article I’ll basically be condensing information from the Google Developer site to serve as a primer/introduction for anyone thinking about using gRPC.
gRPC passes data through protocol buffers. They are defined by Google’s Developer Guide as:
… a flexible, efficient, automated mechanism for serializing structured data — think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages. You can even update your data structure without breaking deployed programs that are compiled against the “old” format.
So, protocol buffers are to gRPC what XML or JSON are to REST. They’re a fast, small, and flexible way of serializing data to be passed over the network. When compared to, XML protocol buffers are:
- Simpler (to use/understand)
- 3–10 times smaller
- 20–100 times faster
- Less ambiguous (through things like strict typing)
- Generate data access classes that are easier to use programmatically––which, knowing XML, is (1) not all that surprising and (2) very welcome
Short Example
Here’s a quick barebones example of gRPC in action to provide a little context
You define the procedures in “services,” and organize the requests/responses into “messages.”
To load in this protocol buffer on the server, all you need to do is to write code that looks something like this:
And implement the rpc like so:
And bada bing bada boom, the client can now make remote procedure calls conforming to the .proto’s “Example” service.
So there ya go.