githubEdit

gRPC

gRPC protocol

Introduction

gRPC is a high-performance, open-source RPC framework originally developed by Google. It uses Protocol Buffers (protobuf) as both its interface definition language and underlying message serialization format. gRPC supports bidirectional streaming and runs on HTTP/2, providing features like multiplexing, header compression, and flow control. It is widely used in microservice architectures and cloud-native systems.

Key Features

  • Protocol Buffers: Strongly-typed, language-neutral IDL with efficient binary serialization

  • HTTP/2 Transport: Multiplexed streams, header compression, and bidirectional communication

  • Streaming: Unary, server-streaming, client-streaming, and bidirectional-streaming RPCs

  • Multi-Language: Code generation for Go, Java, Python, C++, Node.js, Rust, and more

  • Deadlines/Timeouts: Built-in deadline propagation across service boundaries

  • Interceptors: Middleware chain for authentication, logging, and tracing

  • Load Balancing: Client-side and proxy-based load balancing support

Service Definition

gRPC services are defined using .proto files:

syntax = "proto3";

package helloworld;

service Greeter {
  // Unary RPC
  rpc SayHello (HelloRequest) returns (HelloReply);

  // Server-streaming RPC
  rpc SayHelloStream (HelloRequest) returns (stream HelloReply);
}

message HelloRequest {
  string name = 1;
  int32 age = 2;
}

message HelloReply {
  string message = 1;
}

Code Generation

Usage Example (Go)

Server

Client

gRPC in Kubernetes

gRPC services require HTTP/2-aware load balancing. Use headless services with client-side load balancing or a proxy like Envoy:

Health Checking

gRPC defines a standard health checking protocol. Configure Kubernetes probes with grpc type (Kubernetes 1.24+):

Reference:

Last updated