0
0
Microservicessystem_design~7 mins

gRPC for internal communication in Microservices - System Design Guide

Choose your learning style9 modes available
Problem Statement
When microservices communicate using traditional REST APIs with JSON over HTTP, the message size can be large and parsing can be slow, causing higher latency and increased CPU usage. This inefficiency becomes a bottleneck as the number of internal service calls grows, leading to slower response times and higher resource costs.
Solution
gRPC uses a compact binary protocol and HTTP/2 to enable fast, efficient communication between microservices. It defines service contracts with Protocol Buffers, which generate strongly typed client and server code, reducing errors and improving performance. This approach minimizes message size and supports multiplexing multiple calls over a single connection, lowering latency and resource consumption.
Architecture
┌───────────────┐       ┌───────────────┐
│   Service A   │──────▶│   Service B   │
│ (gRPC Client) │       │ (gRPC Server) │
└───────────────┘       └───────────────┘
        │                      ▲
        │ HTTP/2 + Protobuf    │
        └──────────────────────┘

This diagram shows Service A acting as a gRPC client sending requests over HTTP/2 with Protocol Buffers to Service B, the gRPC server, enabling efficient internal communication.

Trade-offs
✓ Pros
Reduces message size with binary serialization, improving network efficiency.
Supports multiplexing multiple requests over a single HTTP/2 connection, lowering latency.
Generates strongly typed client and server code, reducing bugs and improving developer productivity.
Built-in support for deadlines, cancellations, and streaming enhances robustness.
✗ Cons
Requires learning Protocol Buffers and gRPC tooling, adding initial complexity.
Less human-readable than JSON, making debugging network traffic harder without tools.
Not all languages or environments support gRPC equally, which can limit interoperability.
Use gRPC for internal microservice communication when you have high request volumes, need low latency, and want strong API contracts between services.
Avoid gRPC if your services require broad public API support with browsers or clients that do not support HTTP/2 or Protocol Buffers, or if your system is small with low communication overhead.
Real World Examples
Google
Google uses gRPC internally to connect its microservices efficiently, leveraging HTTP/2 multiplexing and Protocol Buffers for low-latency communication.
Netflix
Netflix adopted gRPC for internal service communication to reduce latency and improve throughput in their high-scale microservices environment.
Square
Square uses gRPC to enable fast, reliable communication between payment processing microservices, ensuring strong API contracts and efficient data exchange.
Code Example
The before code shows a simple REST call using JSON over HTTP, which is easy but less efficient. The after code uses gRPC with generated client stubs and Protocol Buffers, enabling faster, strongly typed communication with Service B.
Microservices
### Before: REST API call with JSON (naive approach)
import requests

def get_user_data(user_id):
    response = requests.get(f"http://serviceb/api/users/{user_id}")
    return response.json()


### After: gRPC client call with Protocol Buffers
import grpc
import user_pb2
import user_pb2_grpc

def get_user_data(user_id):
    with grpc.insecure_channel('serviceb:50051') as channel:
        stub = user_pb2_grpc.UserServiceStub(channel)
        request = user_pb2.UserRequest(id=user_id)
        response = stub.GetUser(request)
        return response
OutputSuccess
Alternatives
REST over HTTP/1.1
Uses text-based JSON over HTTP/1.1 without multiplexing or binary serialization.
Use when: Choose REST when interoperability with browsers and external clients is a priority and simplicity is preferred over performance.
Message Queue (e.g., Kafka, RabbitMQ)
Uses asynchronous messaging for decoupled communication rather than synchronous RPC calls.
Use when: Choose message queues when you need asynchronous processing, event-driven architecture, or loose coupling between services.
Thrift
Similar to gRPC but uses its own binary protocol and supports multiple transport layers.
Use when: Choose Thrift if you require cross-language support with custom transport protocols or legacy system integration.
Summary
gRPC improves internal microservice communication by using efficient binary serialization and HTTP/2 multiplexing.
It enforces strong API contracts through Protocol Buffers, reducing errors and improving performance.
gRPC is best suited for high-scale, low-latency internal calls but is less ideal for public APIs requiring broad client support.