When microservices exchange thousands of requests per second, the protocol layer becomes a major bottleneck. Moving from REST/JSON over HTTP/1.1 to gRPC over HTTP/2 yielded a 40% reduction in internal network latency and lower CPU usage across Auth Service and API Gateway.
Out-of-the-box gRPC settings, however, are rarely tuned for high-throughput topologies. Here are the specific optimizations—and gotchas—we encountered.
1. HTTP/2 Connection Pooling
Unlike HTTP/1.1, which opens separate TCP connections per concurrent request, HTTP/2 multiplexes many streams over a single TCP connection.
While this reduces TCP handshake overhead, a single TCP connection per backend can bottleneck on HTTP/2 MAX_CONCURRENT_STREAMS (typically 100–250) or suffer TCP head-of-line blocking when a single lost packet stalls every multiplexed stream on that socket.
For our highest-QPS internal callers, we pooled 4 to 8 *grpc.ClientConn channels per target and round-robined RPCs across them:
type ConnPool struct {
conns []*grpc.ClientConn
index uint32
}
func (p *ConnPool) Get() *grpc.ClientConn {
idx := atomic.AddUint32(&p.index, 1)
return p.conns[idx%uint32(len(p.conns))]
}Spreading multiplexed streams across multiple physical TCP sockets reduces head-of-line stalls and improves p99 tail latency under burst traffic.
2. Keep-Alives and Enforcement Policies
In cloud environments, firewalls, NAT gateways, and Edge Load Balancer instances silently drop idle TCP connections. Without active HTTP/2 PING probes, gRPC clients can hang waiting on half-open sockets.
However, enabling aggressive client keepalives without updating the server is a classic gRPC footgun: **grpc-go servers default to EnforcementPolicy{MinTime: 5 * time.Minute, PermitWithoutStream: false}, so a client pinging every 20s will be disconnected with GOAWAY (ENHANCE_YOUR_CALM / too_many_pings).
Both sides must be tuned together:
// Client-side keepalive
clientKA := keepalive.ClientParameters{
Time: 20 * time.Second, // Ping if idle for 20s
Timeout: 3 * time.Second, // Close if no ACK in 3s
PermitWithoutStream: true,
}
// Matching server-side enforcement policy (required to avoid ENHANCE_YOUR_CALM)
serverPolicy := keepalive.EnforcementPolicy{
MinTime: 15 * time.Second,
PermitWithoutStream: true,
}3. Load Balancing: Moving Beyond Layer 4
Because gRPC multiplexes RPCs over long-lived HTTP/2 connections, standard Layer 4 load balancers (including Kubernetes ClusterIP Services) pin each connection to one backend pod, causing severe hot spots during autoscaling.
To balance per-RPC instead of per-connection, we point clients at a Kubernetes headless Service** using the dns:/// resolver and explicit round_robin policy:
conn, err := grpc.Dial(
"dns:___STASH_0___
grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"round_robin":{}}]}`),
grpc.WithKeepaliveParams(clientKA),
)One crucial caveat: grpc-go's built-in DNS resolver polls rather than watching Kubernetes Endpoints, so newly scaled pods are not discovered instantaneously. We pair headless DNS with server-side keepalive.ServerParameters{MaxConnectionAge: 5 * time.Minute} (or an L7 Envoy/xDS control plane) to force periodic graceful connection rotation across newly added pods.