1. What is GRPC
- GRpc is a high-performance, open source, and generic RPC framework designed for mobile and HTTP/2. Currently, C, Java, and Go languages are available: GRPC, GRpc-Java, and GRPC-Go. The C version supports C, C++, Node.js, Python, Ruby, Objective-C, PHP, and C#.
- GRPC is designed based on the HTTP/2 standard and brings features such as bidirectional streaming, flow control, header compression, and multiplex requests over a single TCP connection. These features allow it to perform better on mobile devices, saving power and space.
- Reference: GRPC Official Documentation English and GRPC Official Documentation Chinese
2. Why do we use GRPC
- Ecology is good: Back to Google. For example, Nginx also provides support for GRPC
- Cross-language: Cross-language and automatically generates SDKS
- High performance: For example, protobuf performance is higher than JSON, for example, Http2.0 performance is higher than Http1.1
- Strongly typed: The compiler solves a big part of the problem for you
- Streaming (based on HTTP2.0) : support client streaming, server streaming, bidirectional streaming
3. How are the advantages of GRPC realized
3.1 High GRPC Performance: Why is protobuf performance higher than JSON?
1. What is protobuf?
- Protobuf is a binary format developed by Google for serializing data between different services. It is an Interface Description Language (IDL) language
2. How much faster is it than JSON?
- Six times faster, reference link
3. Why is Protobuf faster than JSON?
- The binary data flow and JSON data flow for Protobuf are shown below
- Compare the JSON data to the Protobuf data format
- Small size – No need for delimiters: TLV storage does not need delimiters (commas, double quotation marks, etc.) to separate fields, reducing the use of delimiters
- Small size – Empty field omission: If a field is not set with a field value, the serialized data of the field does not exist at all, that is, no encoding is required, and the KEY and empty value are passed in JSON
- Small size – Tag binary representation: The numeric value of a field is converted to a binary representation, which saves more space than the string representation of a KEY in JSON
- Fast encoding and decoding: the tag stores the type of the field and can directly know the length of the value, or when the value is a string, the length can be stored with the length. We can directly take n bytes after the length to be the value, and if we do not know the length of the value, we must do string matching
- For details on protobuf encoding, see varint and Zigzag encoding
3.2 GRPC high Performance: Why is http2.0 higher performance than HTTP1.1?
1. Multiplexing
- 1 http2.0 and HTTP.Also compare http1.1 Pipling*
- Schematic diagram
- HTTP /1.* : One request, one response, one connection, one connection for each request
- Http1.1 pipeling (pipeling) is a multi-threaded process that blocks multiple requests processed at any time in a serialized manner
- Http2: Multiple requests can be executed concurrently on a connection. A request task is time-consuming and does not affect the normal execution of other connections
- What other advantages of GRPC multiplexing – reduce TCP connections, reduce the server and client for memory, CPU, etc. – reduce TCP connections, ensure that TCP is not frequently triggered to re-establish, so that there is no frequent slow start – reduce TCP connections, so that network congestion can be improved
- Why can’t HTTP /1.1 multiplex while HTTP2.0 can? – Because HTTP /1.1 transmits text, while HTTP2.0 uses binary frame transmission
2. Head compression
- Fixed field compression: HTTP can gzip the body through HTTP to save bandwidth. However, many header fields in packets are not compressed, such as cookies and user Agent Accept, which need to be compressed
- Avoid duplication: In a large number of request and response packets, many field values are duplicated, so it is necessary to avoid duplication
- Encoding improvement: the field is ASCII encoding, low efficiency, change to binary encoding can be improved
- Through the aboveHPACK algorithmThe algorithm mainly consists of three parts
- Static dictionary: A dictionary of commonly used header fields, such as {“:method”:”GET”}, can be represented by a single number 2
- Dynamic dictionary: use a dynamic dictionary if some header fields are not in the static dictionary
- Huffman coding: compression coding
3. Binary framing
- At the binary framing layer, HTTP 2.0 splits all transmitted information into smaller messages and frames and encodes them in binary format, where the http1.x header is encapsulated in the Headers frame and our Request body is encapsulated in the Data frame.
- These frames can then be sent out of order and assembled according to the stream identifier at the beginning of each frame
- HTTP / 1.1If the key: value is delimited by a newline, the following problems may occur:
- Only one request or response can be processed at a time, because the data, which is delimited from the message, cannot stop parsing until it is complete
- Parsing this data without knowing how much memory is required puts a lot of pressure on the server
4. The server actively pushes resources
- Because the server is supported to actively push resources, part of the request can be saved. For example, if you need two files 1.html,1.css, or http1.0 you need two requests and the server returns two. Http2.0, however, allows the client to request once and the server to reply twice