Why gRPC Is Becoming the Default Choice for Microservices
The article explains why gRPC is gaining popularity by comparing its performance, streaming capabilities, and cross‑language contract advantages over traditional REST + JSON, and provides a step‑by‑step guide to building a complete gRPC service with Spring Boot 4.1.
Introduction
When a system’s traffic spikes, developers often see response times become unstable even though business logic and database indexes have not changed. The root cause is a narrow "communication lane" between services. An experiment that switched two high‑frequency service calls from REST + JSON to gRPC showed a three‑fold QPS increase and latency reduced to one‑third.
Why REST Is Slow
1. One request per HTTP/1.1 connection – each request creates a new TCP connection with a three‑way handshake.
2. JSON is verbose – field names are repeated in every request, inflating payload size.
3. Heavy serialization/deserialization – parsing text into token streams and object trees is far slower than binary parsing.
4. No connection reuse – each request is independent, preventing parallelism on a single socket.
These issues are negligible at low concurrency but explode under load.
How gRPC Is Faster
3.1 HTTP/2 Multiplexing
gRPC runs on HTTP/2, which allows many streams over a single TCP connection, reducing handshake overhead from 3 RTT per request to 1 RTT per session.
Connection model : HTTP/1.1 – one connection per request; HTTP/2 – multiplexed streams.
Connection establishment : 3 handshakes per request vs. 1 handshake per session.
Data transfer : Text vs. binary frames.
Concurrency : Serial/limited vs. true parallel streams.
Server push : Not supported vs. supported.
3.2 Protobuf Efficiency
gRPC uses Protocol Buffers (Protobuf) as its default serialization format. Protobuf is binary, sending only field numbers and values, which reduces payload size by 60‑80% and speeds up serialization 3‑5× compared with JSON.
Benchmark example:
Data volume: REST + JSON baseline vs. gRPC + Protobuf – 60‑80% reduction.
Serialization speed: baseline vs. 3‑5× faster.
Connection setup: 3 RTT vs. 1 RTT.
Request latency: 8‑12 ms vs. 2‑3 ms.
Throughput: 450 req/s vs. 1 200 req/s.
gRPC Architecture Overview
The contract layer (Proto file) defines service interfaces and data structures. Code generation produces type‑safe client and server stubs, ensuring cross‑language consistency.
Complete gRPC Service Example (Spring Boot 4.1)
5.1 Define the Proto File
syntax = "proto3";
package com.example.grpc;
service UserService {
rpc GetUser (UserRequest) returns (UserResponse);
rpc ListUsers (ListUsersRequest) returns (stream UserResponse);
}
message UserRequest {
int64 id = 1;
}
message ListUsersRequest {
int32 page = 1;
int32 size = 2;
}
message UserResponse {
int64 id = 1;
string name = 2;
string email = 3;
int32 age = 4;
}5.2 Add Maven Dependencies (gRPC 1.68.0)
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.68.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.68.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.68.0</version>
</dependency>5.3 Implement the Server
@GrpcService
public class UserServiceImpl extends UserServiceGrpc.UserServiceImplBase {
@Override
public void getUser(UserRequest request, StreamObserver<UserResponse> responseObserver) {
long userId = request.getId();
UserResponse response = UserResponse.newBuilder()
.setId(userId)
.setName("用户" + userId)
.setEmail("user" + userId + "@example.com")
.setAge(25)
.build();
responseObserver.onNext(response);
responseObserver.onCompleted();
}
}5.4 Configure the Server (application.yml)
grpc:
server:
port: 90905.5 Client Stub Generation and Usage
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 9090)
.usePlaintext()
.build();
UserServiceGrpc.UserServiceBlockingStub stub = UserServiceGrpc.newBlockingStub(channel);
UserRequest request = UserRequest.newBuilder().setId(1001L).build();
UserResponse response = stub.getUser(request);
System.out.println("用户信息:" + response.getName());Spring Boot 4.1 Official gRPC Support
Spring Boot 4.1 bundles four official gRPC starters (server, client, and their test variants) managed by the Spring Boot BOM. Adding the gRPC dependency in Spring Initializr automatically registers the service as a Spring bean, eliminating the need for third‑party starters.
Key annotations: @GrpcService – registers a gRPC service (similar to @Service). @ImportGrpcClients – enables automatic stub creation for client code. @GrpcAdvice – centralised exception handling for gRPC.
Why More People Use gRPC?
Performance Advantage
A 2026 benchmark shows gRPC throughput is 107 % higher and latency 48 % lower than REST for identical business logic. In a 1 000‑request test, REST averaged 250 ms latency while gRPC averaged 25 ms.
Streaming Communication
gRPC supports four RPC modes: Unary, Server Streaming, Client Streaming, and Bidirectional Streaming. This flexibility makes it suitable for real‑time logs, file uploads, AI‑driven conversations, and more.
Cross‑Language Compatibility
A single Proto file can generate client/server code for Java, Go, Python, C++, Node.js, C#, etc., allowing heterogeneous services to communicate with identical wire protocols.
Strong‑Typed Contract
The Proto file acts as an exact contract and documentation. Any change forces regeneration, instantly revealing incompatibilities.
Challenges and Considerations
Browser compatibility : Native browsers do not support gRPC; a gRPC‑Web proxy is required.
Debugging difficulty : Binary Protobuf payloads need tools like grpcurl or BloomRPC.
Learning curve : Teams must learn Proto syntax, code generation, and HTTP/2.
Kubernetes load balancing : Long‑lived connections can cause sticky sessions; headless services or service meshes are often needed.
Pros & Cons Comparison (REST + JSON vs. gRPC)
Data format : Text vs. binary (Protobuf).
Transport protocol : HTTP/1.1 vs. HTTP/2.
Connection reuse : ❌ vs. ✅ (multiplexing).
Serialization size : baseline vs. 60‑80 % reduction.
Serialization speed : baseline vs. 3‑5× faster.
Cross‑language support : Good vs. excellent.
Streaming : ❌ vs. ✅ (bidirectional).
Browser support : ✅ native vs. ⚠️ requires gRPC‑Web.
Debugging : Simple vs. higher difficulty.
Learning curve : Low vs. medium.
Typical use case : External APIs vs. internal microservice communication.
Suitable Scenarios
Microservice‑to‑microservice communication – strongly recommended.
Teams with mixed language stacks – strongly recommended.
Streaming data interactions (logs, AI agents) – strongly recommended.
High‑performance internal routing – recommended.
Public APIs – evaluate due to browser compatibility.
Simple CRUD apps – evaluate; REST may be sufficient.
Direct front‑end calls – not recommended without gRPC‑Web.
Conclusion
gRPC solves the concurrency bottleneck of HTTP/1.1 and the payload overhead of JSON by leveraging HTTP/2 multiplexing and Protobuf binary serialization. It delivers 2‑3× higher throughput and an order‑of‑magnitude lower latency for internal service calls, making it the preferred choice for high‑traffic microservice architectures, multi‑language teams, and streaming use cases. However, for public‑facing APIs or simple CRUD services, REST remains a more straightforward option.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
