Editorial Q&A · This problem and its answer were written by the TechLab editorial team to document a common issue. It isn't a question from a community member. If you've hit a variation of it or know a better approach, add a reply below.
The problem
Two Go services talk over gRPC: an order service (client) calls an inventory service (server) to reserve stock before confirming an order. The inventory service runs on Kubernetes in AWS, and the order service, running in a different cluster, reaches it through an internal AWS Network Load Balancer (NLB).
After 5–10 minutes without calls, the first RPC fails with UNAVAILABLE. Calls immediately afterwards succeed. The inventory pods are healthy and haven't restarted. The suspicion is that something between the two services closes the idle TCP connection and the client only notices when it next sends a request.
Environment:
- Client and server: Go with
google.golang.org/grpc - Network path: client pod → internal NLB → inventory service pods
Code Snippets:
OrderProcessingService (Client-side connection setup):
package main
import (
"log"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
pb "your/proto/package" // Assume pb is defined for generated gRPC client
)
var inventoryServiceClient pb.InventoryServiceClient
func init() {
// Internal NLB in front of the inventory service
inventoryServiceAddr := "inventory-nlb.internal.example.com:50051"
conn, err := grpc.Dial(
inventoryServiceAddr,
grpc.WithTransportCredentials(insecure.NewCredentials()), // For simplicity in internal cluster comms
)
if err != nil {
log.Fatalf("did not connect to inventory service: %v", err)
}
inventoryServiceClient = pb.NewInventoryServiceClient(conn)
log.Printf("Successfully connected to inventory service at %s", inventoryServiceAddr)
}
func main() {
// ... application logic ...
// Example RPC call later in the code (simplified):
// _, err := inventoryServiceClient.ReserveStock(context.Background(), &pb.ReserveStockRequest{ProductId: "prod1", Quantity: 1})
// if err != nil {
// log.Printf("Error reserving stock: %v", err) // This is where UNAVAILABLE errors appear
// }
}InventoryService (Server-side setup):
package main
import (
"context"
"log"
"net"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "your/proto/package" // Assume pb is defined for generated gRPC server
)
// server implements your/proto/package.InventoryServiceServer
type server struct {
pb.UnimplementedInventoryServiceServer
}
func (s *server) ReserveStock(ctx context.Context, req *pb.ReserveStockRequest) (*pb.ReserveStockResponse, error) {
log.Printf("Received ReserveStock request for product %s, quantity %d", req.ProductId, req.Quantity)
// Simulate stock reservation logic
if req.ProductId == "non-existent" {
return nil, status.Errorf(codes.NotFound, "Product %s not found", req.ProductId)
}
return &pb.ReserveStockResponse{Success: true, Message: "Stock reserved"}, nil
}
func main() {
lis, err := net.Listen("tcp", ":50051")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterInventoryServiceServer(s, &server{})
log.Printf("server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}Expected behaviour: gRPC keeps the connection usable, or reconnects transparently, so the application never sees an error caused by an idle period.
Actual behaviour:
After about 5–10 minutes without traffic, the first ReserveStock call fails with an error like:
rpc error: code = Unavailable desc = error reading from server: read tcp 10.0.12.34:41872->10.0.40.7:50051: read: connection reset by peerThe next call succeeds on a fresh connection.
The question: why does the idle connection fail, and how should client and server be configured so it doesn't?
