05
Demo 05 — REST vs gRPC: Two Protocols
Spring Boot 4.0.5 / Java 21
⏱ ~10 min
Run this demo
View source on GitHub ↗
cd demo-05-grpc
chmod +x demo.sh
./demo.sh
One Spring Boot app simultaneously serves REST on :8080 and gRPC on :9000. Same service, two protocols, honest comparison including the localhost caveat.
Spring Boot 4.0 gRPC support
Spring Boot 4.0 introduces first-class gRPC support via spring-grpc-spring-boot-starter. No third-party libraries needed.
<dependency>
<groupId>org.springframework.grpc</groupId>
<artifactId>spring-grpc-spring-boot-starter</artifactId>
</dependency>
# application.yml
spring:
grpc:
server:
port: 9000
The honest localhost caveat
gRPC unary is slower than REST on localhost. Network cost is zero on loopback. gRPC’s advantages only materialise with real pod-to-pod network latency.
What gRPC wins even on localhost:
- Server streaming: 1 connection vs 1,000 REST requests
- High concurrency (c=500): HTTP/2 multiplexing vs 500 TCP connections
In production (pod-to-pod): gRPC wins ~3-4x throughput, ~73% p50 latency.
Streaming modes
# Live stream — one push per second (count=0)
grpcurl -plaintext -d '{"host":"localhost","count":0}' \
localhost:9000 MetricsService/StreamMetrics
# Benchmark — 1000 messages as fast as possible
grpcurl -plaintext -d '{"host":"localhost","count":1000}' \
localhost:9000 MetricsService/StreamMetrics
Prerequisites
hey, grpcurl, ghz — see Prerequisites guide.