Looking at gRPC Java doc - ManagedChannelBuilder, there're two options to manage the connections. It seems idleTimeout() is the default/preferred configuration. But when I tried to search for a comparison, most of the posts are talking about keepAlive option.
I'm curious about what's the common practise and what are the pros and cons of these two options?
idleTimeout
Set the duration without ongoing RPCs before going to idle mode. In idle mode the channel shuts down all connections, the NameResolver and the LoadBalancer. A new RPC would take the channel out of idle mode. A channel starts in idle mode. Defaults to 30 minutes.
This is an advisory option. Do not rely on any specific behavior related to this option.
keepAliveWithoutCalls
Sets whether keepalive will be performed when there are no outstanding RPC on a connection. Defaults to false.
Clients must receive permission from the service owner before enabling this option. Keepalives on unused connections can easilly accidentally consume a considerable amount of bandwidth and CPU. idleTimeout() should generally be used instead of this option.
GRPC uses keepalive ping as a way to check if a channel is currently working by sending HTTP2 pings over the transport. It is sent periodically, and if the ping is not acknowledged by the peer within a certain timeout period, the transport is disconnected [1].
A gRPC channel should be reused when making gRPC calls. Reusing a channel allows calls to be multiplexed through an existing HTTP/2 connection. If a new channel is created for each gRPC call then the amount of time it takes to complete can increase significantly.
There are two possible solutions: Create a separate channel for each area of high load in the application. Use a pool of gRPC channels to distribute RPCs over multiple connections (channels must have different channel args to prevent re-use so define a use-specific channel arg such as channel number).
Keep alive pings are sent when a period of inactivity exceeds the configured KeepAlivePingDelay value. The client will close the connection if it doesn't receive any frames within the timeout. Timeout must be greater than or equal to 1 second. Set to InfiniteTimeSpan to disable the keep alive ping timeout.
Use keepalive to notice connection failures while RPCs are in progress. Use idleTimeout to release resources and prevent idle TCP connections from breaking when the channel is unused.
idleTimeout is preferred over keepAliveWithoutCalls because it tends to reduce the overall load in the system. keepAliveWithoutCalls is used when you are willing to spend client, server, and network resources to have lower latency for very infrequent RPCs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With