gRPC is the high-performance Remote Procedure Call framework developed by Google that uses $\mathrm{HTTP}/2$ for transport and Protocol Buffers for serialization — enabling efficient bidirectional streaming, strict type-safe contracts, and 5-10x faster inter-service communication than REST/JSON, making it the standard for internal microservice communication and ML model serving APIs.
What Is gRPC?
- Definition: An $\text{open-source} \mathrm{RPC}$ framework that generates client and server code from .proto schema files — allowing a Python client to call a Go service's methods as if they were local function calls, with $\mathrm{HTTP}/2$ multiplexing, Protocol Buffers encoding, and optional TLS security.
- Origin: Developed by Google as the successor to their internal Stubby RPC framework — $\text{open-sourced} \in 2015$ and now a CNCF (Cloud Native Computing Foundation) graduated project.
- HTTP/2 Foundation: gRPC runs exclusively over $\mathrm{HTTP}/2$ — gaining multiplexed streams (multiple concurrent RPC calls on one TCP connection), header compression, binary framing, and server push over the same connection.
- Four Communication Patterns: Unary (one request, one response), server streaming (one request, multiple responses), client streaming (multiple requests, one response), bidirectional streaming (multiple each way) — all on the same connection.
- Code Generation: protoc + gRPC plugin generates complete client stubs and server base classes from .proto files — a Go service and Python client generated from the same .proto are guaranteed type-compatible.
Why gRPC Matters for AI/ML
- Model Serving: TensorFlow Serving, Triton Inference Server, and Torchserve support gRPC endpoints — sending large tensor payloads via binary Protobuf is significantly more efficient than JSON REST for image and audio ML inputs.
- Streaming Inference: gRPC bidirectional streaming enables token-by-token streaming responses from LLM serving — the server streams tokens as they are generated, the client receives and displays them without waiting for the full response.
- Microservice AI Pipelines: RAG pipelines spanning retrieval service → reranking service → generation service use gRPC for inter-service calls — type safety ensures embedding vector dimensions match across service boundaries.
- Feature Store Serving: Online feature stores (Feast, Tecton) expose gRPC APIs for low-latency feature retrieval — binary encoding reduces latency in the feature serving hot path for $\text{real-time} \mathrm{ML}$ inference.
- Fleet-Scale Logging: ML training and inference systems log structured events via gRPC to logging backends — high-throughput binary streaming at millions of events/second with minimal serialization overhead.
Core gRPC Concepts
Service Definition (.proto):
syntax = "proto3";
service RAGPipeline {
// Unary: single request, single response
rpc $Retrieve(RetrieveRequest)$ returns (RetrieveResponse);
// Server streaming: single request, stream of responses (LLM token streaming)
rpc $Generate(GenerateRequest)$ returns (stream GenerateChunk);
// Bidirectional: stream of requests, stream of responses
rpc $EmbedBatch(stream$ EmbedRequest) returns (stream EmbedResponse);
}
Python gRPC Server:
import grpc
from concurrent import futures
import $rag_{pb2_{\text{grpc}}}$
class $RAGServicer(rag_{pb2_{\text{grpc}}}.RAGPipelineServicer)$:
def $Retrieve(self$, request, context):
docs $= vector_{db}.\operatorname{search}(request.query, top_k=request.top_k)$
return RetrieveResponse(documents=docs)
def $Generate(self$, request, context):
for token in $llm.\operatorname{stream}(request.prompt)$:
yield GenerateChunk(token=token) # Streams tokens as generated
server $= grpc.\operatorname{server}(futures.ThreadPoolExecutor(\max_{\text{workers}}=10))$
$rag_{pb2_{\text{grpc}}}.add_RAGPipelineServicer_to_{\text{server}}(RAGServicer()$, server)
server.add_insecure_port("[::]:50051")
Python gRPC Client:
import grpc
import $rag_{pb2}, rag_{pb2_{\text{grpc}}}$
with $grpc.insecure_{\text{channel}}("\text{rag-service}:50051")$ as channel:
stub $= rag_{pb2_{\text{grpc}}}.RAGPipelineStub(channel)$
Stream tokens from LLM
for chunk in $stub.Generate(GenerateRequest(prompt="Explain$ gRPC")):
gRPC vs REST
| Aspect | gRPC | REST/JSON |
|---|---|---|
| Protocol | $\mathrm{HTTP}/2$ | HTTP/1.1 or 2 |
| Format | Binary (Protobuf) | Text $(\mathrm{JSON})$ |
| Streaming | Native (4 modes) | SSE/WebSocket needed |
| Type safety | Enforced by schema | Optional (OpenAPI) |
| Performance | 5-10x faster | Baseline |
| Browser support | Limited (gRPC-Web) | Universal |
| Best for | Internal services, ML serving | Public APIs |
gRPC is the RPC framework that makes high-performance distributed ML systems practical — by combining $\mathrm{HTTP}/2$ multiplexing with Protocol Buffers encoding and auto-generated type-safe clients, gRPC eliminates the serialization overhead and type mismatches that plague JSON-based microservice communication, enabling the kind of efficient inter-service data transfer that $\text{large-scale} \mathrm{ML}$ inference pipelines require.