protobuf vs gRPC

lony picture lony · Jan 18, 2018 · Viewed 24.1k times · Source

I try to understand protobuf and gRPC and how I can use both. Could you help me understand the following:

  • Considering the OSI model what is where, for example is Protobuf at layer 4?
  • Thinking through a message transfer how is the "flow", what is gRPC doing what protobuf misses?
  • If the sender uses protobuf can the server use gRPC or does gRPC add something which only a gRPC client can deliver?
  • If gRPC can make synchronous and asynchronous communication possible, Protobuf is just for the marshalling and therefore does not have anything to do with state - true or false?
  • Can I use gRPC in a frontend application communicating instead of REST or GraphQL?

I already know - or assume I do - that:

Protobuf

  • Binary protocol for data interchange
  • Designed by Google
  • Uses generated "Struct" like description at client and server to un-/-marshall message

gRPC

  • Uses protobuf (v3)
  • Again from Google
  • Framework for RPC calls
  • Makes use of HTTP/2 as well
  • Synchronous and asynchronous communication possible

I again assume its an easy question for someone already using the technology. I still would thank you to be patient with me and help me out. I would also be really thankful for any network deep dive of the technologies.

Answer

Peter Wishart picture Peter Wishart · Feb 2, 2018

Protocol buffers is (are?) an Interface Definition Language and serialization library:

  • You define your data structures in its IDL i.e. describe the data objects you want to use
  • It provides routines to translate your data objects to and from binary, e.g. for writing/reading data from disk

gRPC uses the same IDL but adds syntax "rpc" which lets you define Remote Procedure Call method signatures using the Protobuf data structures as data types:

  • You define your data structures
  • You add your rpc method definitions
  • It provides code to serve up and call the method signatures over a network
  • You can still serialize the data objects manually with Protobuf if you need to

In answer to the questions:

  1. gRPC works at layers 5, 6 and 7. Protobuf works at layer 6.
  2. When you say "message transfer", Protobuf is not concerned with the transfer itself. It only works at either end of any data transfer, turning bytes into objects
  3. Using gRPC by default means you are using Protobuf. You could write your own client that uses Protobuf but not gRPC to interoperate with gRPC, or plugin other serializers to gRPC - but using gRPC would be easier
  4. True
  5. Yes you can