I have services (both writtenin scala) deployed in kubernetes cluster, lets name them as external service and internal service
- External service - 5 pods, expose REST API, consume internal service grpc API thru grpc client
- Internal service 1 pod, expose grpc API
Problem - When I call internal service grpc API by port-forwarding using some grpc client e.g. BloomRPC, latency is very good within 1 sec (avg. 300 ms) but when I consume same grpc API using grpc client (written in scala), it always takes on average > 2 seconds. I understand first call will take time due to TPC connection handling but it always takes 2-4 seconds even after JVM warm-up. I am creating grpc channel only once at start of service (in main method, asynchronous stub) and reusing the same, no client side grpc load balancing enabled. How to improve latency?
class MyServiceGrpcClient(val channel: ManagedChannel) {
val myServiceStub = MyServiceGrpc.stub(channel)
def this(host: String, port: Int) {
this(ManagedChannelBuilder.forAddress(host, port).usePlaintext().build())
}
@throws[InterruptedException]
def shutdown(): Unit = {
channel.shutdown.awaitTermination(5, TimeUnit.SECONDS)
}
}
object ExternalServiceApp extends App {
val host = sys.env.getOrElse("HOST_NAME", "internal-service-name") //Its k8s ClusterIP service name
val port = sys.env.getOrElse("PORT", "1234")
val internalGrpcClient = new MyServiceGrpcClient(wesHost, wesPort).myServiceStub
val routes = new ExternalRoutes(internalGrpcClient)
//Akka http routes logic, route API calls grpc API
}
class ExternalRoutes(grpcClient: MyServiceGrpcClient){
override def getResponse(request: Request): Future[Response] = {
grpcClient.internalGrpcApi(request).map { resp =>
resp
}
}
}