An Introduction into gRPC

This is a basic introduction of gRPC.

Benefits of gRPC

gRPC uses protocol buffers and HTTP2, which will help the service communications to be much faster, and use less resources.

By using protocol buffers, we can achieve faster and more efficient communication, especially for devices with slower CPUs:

  1. Payload Size: To store the same information, JSON object sizes are larger than protocol buffers.
  2. CPU Usage: Parsing JSON is more CPU intensive than paring protocol buffers (as protocol buffers are binaries).
  1. Multiplexing: HTTP2 only needs to open 1 TCP connection, and the connection will remain open after communication is completed.
  2. Server Push: HTTP2 allows servers to push multiple messages to client (like preload).
  3. Header Compression: HTTP2 supports header compression.
  4. Binary vs Text: HTTP2 is binary, which is more efficient and works well with protocol buffers.

Sample Code

Proto To Python

One of the benefits of using protocol buffers is that, we can prepare one .proto description of the data structure, and it can be used across different programming languages, just by running:

protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/addressbook.proto

will generate addressbook_pb2.py in the destination folder.

Use the Classes

Then we can use the generated classes as the normal python classes:

There are also some built in methods we can use for the messages:

  • IsInitialized(): checks if all the required fields have been set.
  • __str__(): returns a human-readable representation of the message, particularly useful for debugging. (Usually invoked as str(message) or print message.)
  • CopyFrom(other_msg): overwrites the message with the given message's values.
  • Clear(): clears all the elements back to the empty state.

Backward/Forward Compatible

If we update the protocol buffers, and what to make them backward and forward compatible, we need to:

  • must not change the tag numbers of any existing fields.
  • must not add or delete any required fields.
  • may delete optional or repeated fields.
  • may add new optional or repeated fields but must use fresh tag numbers.

4 Types of gRPC API

  • The client sends one request message to the server, and receives one response message from the server.
  • Sample code in Protocol Buffer:
  • The client sends one request message to the server, and receives many response messages from the server.
  • Used when the server needs to send big data; or when the server needs to push data to the client.
  • Sample code in Protocol Buffer:
  • The client sends many messages to the server, and receives one response message from the server.
  • Used when the client needs to send big data; or when the client needs to push data to the server.
  • Sample code in Protocol Buffer:
  • The client sends many messages to the server, and receives many response messages from the server.
  • Used when the client and the server need to send lots of data asynchronously.
  • Sample code in Protocol Buffer:

Software Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store