Pages

Friday, January 19, 2024

RPC/GRPC


gRPC is gaining popularity for service-to-service communication. Here's why:

- Speed - gRPC is built on HTTP/2 and Protobufs for maximum throughput and minimal latency. Much faster than JSON over HTTP.

- Efficiency - The compact Protobuf binary format means smaller payloads than JSON. Less data sent = better performance.

- Type Safety - Protobufs are strongly typed, eliminating many bugs.

- Polyglot - Write services in many popular languages, the IDL works across them all.

- Streaming - gRPC supports bidirectional streaming for real-time data transmission.

- Great Ecosystem - Tons of tools exist for code gen, load balancing, monitoring, and more.

Of course, gRPC has some drawbacks:

- More Complex - Defining Protobuf schemas and setting up gRPC can have a learning curve.

- Not Human Readable - Protobufs are binary, so not as easy to troubleshoot as JSON. Tools like grpcurl help a little bit.

- Limited Browser Support - gRPC works best for backend microservices, not front-end apps.

For most backend use cases, gRPC delivers an efficient, robust and high-performance communication framework perfect for modern microservices.

gRPC is an open source remote procedure call framework created by google in 2016. It was rewrite of their internal RPC infrastructure that they used for years. 

Local procedure call is a function call within a process to execute some code. A remote procedure call enables one machine to invoke some code on another machine as if it is a local call function from the user's perspective. gRPC is a popular implementation of RPC. Many organization has adopted gRPC as a preferred RPC mechanism to connect a large number of microservices running within and across the data centers. 

What makes gRPC so popular?

First, gRPC has a thriving developer ecosystem. It makes it very easy to develop production quality and type-safe API that scale well. The core of this ecosystem is the use of protocol buffers as its data interchange format. Protocol is a language agnostic and platform-agnostic mechanism for encoding structured data. gRPC uses protocol buffers to encode and send data over the wire by default. While gRPC can support other encoding formats like JSON. Protocol buffers provide several advantages that make it the encoding format of choice for gRPC. Protocol buffer supports strongly-typed schema definitions. The structure of data over the wire is defined in a proto file. Protocol buffers provide broad tooling support to turn the schema defined in the proto file into data access classes for all popular programming languages. A gRPC service is also defined in a proto file by specifying RPC access method parameters and return types. The same tooling is used to generate gRPC client and server code from the proto file. Developers use these generated classes in the client to make RPC calls, and in the server to fulfill RPC requests. By supporting many programming languages, the client and the server can independently choose the programming language and ecosystem best suited for their own particular use-cases. This is traditionally not the case for most other RPC frameworks. The second reason why gRPC is so popular is because it is high performance out of the box. Two factors contribute to its performance. First is that a protocol buffer is a very efficient binary encoding format. It is much faster than JSON. Second, gRPC is built on top of HTTP/2 to provide a high performance foundation at scale. The use of HTTP/2 brings many benefits. gRPC uses HTTP/2 streams. It allows multiple streams of messages over a single long lived TCP connection. This allows the gRPC framework to handle many concurrent RPC calls over a small number of TCP connections between clients and servers. 

Typical flow from gRPC client to gRPC Server

Eg Say order service is grpc client and payment service is grpc server. When the order service makes a grpc call to payment service, it invokes the client code generated by gRPC tooling at build time. This generated client code is called a client stub. gRPC encode the data passed to the client stub into protocol buffers and sends its to the low-level transport layer. gRPC sends the data over the network as a streams of HTTP/2 data frames. Because of binary encoding and network optimization, gRPC is consider as 5 times faster than JSON. The payment service receives the packet from the network, decodes them and invokes the server application. The results returned from the server application get into protocol buffers and sent them to transport layer. The order service receives the packet, decode them and send the result to client application. gRPC is very easy to implement. But why don't we see it widely used over web client and web servers? One reason is gRPC relies on lower level access to HTTP/2 primitives. No browser currently provide the level of control required over web request to support gRPC client. It is possible to make gRPC call from a browser with the help of proxy. This technology is called gRPC web. However, the feature set is not fully compatible with gRPC and its usage remains low compared to gRPC. 

When should we use gRPC?

gRPC is the inter-service communication mechanism of choice between microservices in data centers. Its broad support for many programming languages allow services to choose their own languages and developer ecosystem best suited for their own use cases. It is also increasingly used in native mobile clients. Its efficiency and performance makes a lot of sense in the energy and bandwidth constraints environments that are mobile devices.  


RPC (Remote Procedure Call) and gRPC are two different remote communication technologies used in distributed systems. Here's an overview of each:

1. **RPC (Remote Procedure Call)**:

   - **Definition**: RPC is a communication protocol that allows a client to invoke procedures or methods on a remote server as if they were local function calls.

   - **Characteristics**:

     - RPC abstracts the communication details between client and server, making remote interactions appear similar to local function calls.

     - It typically operates over network protocols such as TCP/IP and HTTP, and it may use various serialization formats like JSON or XML for data exchange.

     - RPC frameworks provide features for defining service interfaces, generating client/server stub code, and handling network communication and serialization/deserialization.

     - Examples of RPC frameworks include Apache Thrift, XML-RPC, and Java RMI (Remote Method Invocation).


2. **gRPC**:

   - **Definition**: gRPC is an open-source RPC framework developed by Google that uses HTTP/2 as the underlying transport protocol and Protocol Buffers (protobuf) as the serialization format.

   - **Characteristics**:

     - gRPC builds on the traditional RPC model but introduces modern features such as bidirectional streaming, flow control, and multiplexing over a single connection.

     - It uses Protocol Buffers (protobuf) as its default serialization format, which offers better performance, efficiency, and backward/forward compatibility compared to other formats like JSON or XML.

     - gRPC supports multiple programming languages, including C++, Java, Python, Go, and others, and provides auto-generated client and server stub code based on service definitions written in the Protocol Buffers language.

     - It offers support for features like authentication, load balancing, and retry policies out-of-the-box, making it suitable for building scalable and reliable distributed systems.

**Differences**:

- **Transport Protocol**: RPC frameworks can operate over various network protocols such as TCP/IP or HTTP, while gRPC specifically uses HTTP/2 as the underlying transport protocol.

- **Serialization Format**: RPC frameworks may support different serialization formats like JSON, XML, or binary protocols, whereas gRPC primarily uses Protocol Buffers (protobuf) for efficient and language-neutral serialization.

- **Features**: gRPC introduces modern features like bidirectional streaming, flow control, and multiplexing, which may not be available in all RPC frameworks.

- **Language Support**: While RPC frameworks may support a variety of programming languages, gRPC is specifically designed to support multiple languages with auto-generated client and server stubs.


**Use Cases**:

- **RPC**:

  - Legacy systems that rely on traditional RPC-based communication.

  - Environments where flexibility in transport protocols or serialization formats is required.

  

- **gRPC**:

  - Cloud-native applications and microservices architectures that require efficient and scalable communication.

  - Real-time applications like chat, gaming, or streaming that benefit from bidirectional streaming and low latency communication.

In summary, while RPC is a generic term for remote procedure call mechanisms, gRPC is a specific implementation of RPC developed by Google with modern features and Protocol Buffers as the default serialization format. The choice between them depends on factors such as performance requirements, language support, and compatibility with existing systems.




What is gRPC?

gRPC is language-agnostic, making it an excellent choice when the client and server are built using different programming languages

gRPC is a high-performance, open-source remote procedure call framework initially developed by Google. It leverages HTTP/2 for transport, Protocol Buffers as the interface description language, and provides features such as authentication, load balancing, and more.

Key Features of gRPC:

- Speed - gRPC is built on HTTP/2 and Protobufs for high throughput and low latency.

- Efficiency - The compact Protobuf binary format means smaller payloads than JSON.

- Type Safety - Protobufs are strongly typed.

- Polyglot - Write services in many popular languages.

- Great Ecosystem - Many tools for code gen, load balancing, monitoring, and more.

Drawbacks of gRPC:

- More Complex - Defining Protobuf schemas and setting up gRPC can have a learning curve.

- Not Human Readable - Protobufs are binary, so not as easy to troubleshoot as JSON.

- Limited Browser Support - gRPC works best for backend microservices, not front-end apps.


No comments:

Post a Comment