
























When Google introduced gRPC in 2015, one of the most significant architectural decisions was building it on top of HTTP/2 rather than the widely adopted HTTP/1.1. This was just about following some trend, but a deliberate choice that fundamentally shapes how gRPC performs and behaves. Let’s dig deeper into why this decision was made and how it impacts real-world applications.
Before diving into gRPC’s specific needs, let’s understand what HTTP/2 brings to the table that HTTP/1.1 doesn’t.
HTTP/1.1 is a text-based protocol. When you make a request, it looks something like this:
GET /api/users HTTP/1.1
Host: example.com
Accept: application/json
HTTP/2, on the other hand, is a binary protocol. The same request is encoded into binary frames that are more compact and faster to parse. This improves performance, especially when dealing with thousands of concurrent connections (such as in a microservices architecture).
The most interesting and important feature of HTTP/2 is multiplexing. In HTTP/1.1, each TCP connection can handle only one request at a time. If you want to make multiple requests, you either have to:
HTTP/2 allows multiple requests and responses to be interleaved over a single TCP connection using streams. Each stream has a unique ID, and frames can be sent for different streams without blocking each other.
Single TCP Connection
═══════════════════════════════
Stream 1 (ID: 1): [REQ A] -----> [RESP A]
| |
t1 t3
Stream 2 (ID: 3): [REQ B] -----> [RESP B]
| |
t1.5 t2.5
Stream 3 (ID: 5): [REQ C] -----> [RESP C]
| |
t2 t4
This is how the timeline view of multiplexed HTTP/2 requests looks.
t1 t1.5 t2 t2.5 t3 t4
| | | | | |
▼ ▼ ▼ ▼ ▼ ▼
┌─────┬──────┬─────┬──────┬─────┬─────┐
│REQ A│REQ B │REQ C│RESP B│RESP │RESP │
│(S:1)│(S:3) │(S:5)│(S:3) │A │C │
│ │ │ │ │(S:1)│(S:5)│
└─────┴──────┴─────┴──────┴─────┴─────┘
On the wire, this multiplexing looks something like this.
[H S:1][D S:1][H S:3][D S:3][H S:5][D S:5][D S:3][D S:1][D S:5]
REQ A REQ A REQ B REQ B REQ C REQ C RESP B RESP A RESP C
S:1, S:3, S:5 = Stream IDs
REQ = Request frames
RESP = Response frames
gRPC supports four types of RPC calls:
HTTP/1.1 simply cannot handle streaming scenarios effectively. While techniques like Server-Sent Events (SSE) or WebSockets exist, they’re either limited or require protocol upgrades that break the HTTP model.
HTTP/2’s stream-based architecture naturally supports these patterns. A bidirectional streaming gRPC call maps perfectly to an HTTP/2 stream where both client and server can send frames asynchronously.
Here’s a practical example…
Imagine a chat application where multiple users are sending messages simultaneously:
service ChatService {
rpc LiveChat(stream ChatMessage) returns (stream ChatMessage);
}
With HTTP/2, this becomes a single stream where:
In microservices architectures, services often make dozens of calls to other services to fulfill a single user request. With HTTP/1.1, this creates a bottleneck:
Service A → Service B (wait)
→ Service C (wait)
→ Service D (wait)
Even with connection pooling, you’re limited by the number of concurrent connections and the head-of-line blocking problem.
With gRPC over HTTP/2, Service A can make all these calls concurrently over a single connection:
Service A → Multiple streams to Services B, C, and D simultaneously
The multiplexing ensures that a slow response from Service B doesn’t block the faster responses from Services C and D.
gRPC makes extensive use of headers for metadata like authentication tokens, tracing information, and custom headers. In a typical microservices call chain, these headers are propagated through multiple services.
HTTP/1.1 sends headers as plain text with every request:
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
X-Trace-ID: 1234567890abcdef
X-User-ID: user123
Content-Type: application/grpc+proto
In a service mesh with hundreds of requests per second, this overhead becomes significant.
HTTP/2’s HPACK compression maintains a dynamic table of previously seen headers. After the first request, common headers are referenced by index rather than sent in full.
:method: POST → Index 3
:path: /UserService/GetUser → Index 62
authorization: Bearer... → Index 58
This reduces header overhead by 85-90% in real applications.
Consider a microservices application with 10 services, each making an average of 5 calls to other services under load.
HTTP/1.1 Scenario:
HTTP/2 Scenario:
In practice, the latency benefits of HTTP/2 for gRPC are most noticeable in:
Real-world numbers that I have observed
Consider a log streaming service:
service LogService {
rpc StreamLogs(LogRequest) returns (stream LogEntry);
}
HTTP/1.1 Approach:
HTTP/2 Approach:
The HTTP/2 implementation is not only simpler but also more robust and efficient.
HTTP/2’s flow control prevents fast producers from overwhelming slow consumers. In gRPC streaming:
The window size controls how much unacknowledged data can be “in flight” between sender and receiver at any given moment. Think of it as a buffering limit, not a message size limit.
This prevents memory exhaustion and provides natural backpressure, something that’s difficult to achieve cleanly with HTTP/1.1. Also, both client and server can send a WINDOW_UPDATE frame depending on their rate of consumption and production.
Single Request Scenarios - For simple, one-off requests, HTTP/1.1 might have lower latency due to:
Resource-Constrained Environments - HTTP/2 requires more memory for:
In embedded systems or extremely memory-constrained environments, this overhead might be significant.
Load Balancer Compatibility - Not all load balancers handle HTTP/2 efficiently:
Debugging Complexity - HTTP/2’s binary nature makes debugging more challenging:
And of course, talking about the most common - protobuf. gRPC’s use of Protocol Buffers pairs exceptionally well with HTTP/2
Protobuf’s binary serialization is naturally aligned with HTTP/2’s binary frames:
Protobuf’s schema evolution capabilities work well with HTTP/2’s header compression:
gRPC’s adoption of HTTP/2 was a strategic decision that enables the framework’s core value propositions - performance, functionality, and scalability.
If you are building distributed systems, understanding this relationship between gRPC and HTTP/2 is crucial. It helps you build performant production environments.
As you design and implement gRPC services, keep these underlying mechanics in mind – they will inform your decisions about service boundaries, streaming strategies, and performance optimization.
The key is understanding how to leverage these capabilities effectively in your specific use case.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。