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.
Protocol Buffers vs JSON
By using protocol buffers, we can achieve faster and more efficient communication, especially for devices with slower CPUs:
- Payload Size: To store the same information, JSON object sizes are larger than protocol buffers.
- CPU Usage: Parsing JSON is more CPU intensive than paring protocol buffers (as protocol buffers are binaries).
HTTP2 vs HTTP1
- Multiplexing: HTTP2 only needs to open 1 TCP connection, and the connection will remain open after communication is completed.
- Server Push: HTTP2 allows servers to push multiple messages to client (like preload).
- Header Compression: HTTP2 supports header compression.
- Binary vs Text: HTTP2 is binary, which is more efficient and works well with protocol buffers.
Protocol Buffers Tutorial
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 asstr(message)
orprint 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
Unary
- The client sends one request message to the server, and receives one response message from the server.
- Sample code in Protocol Buffer:
Server Streaming
- 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:
Client Streaming
- 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:
Bi Directional Streaming
- 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: