gRPC connection: use keepAlive or idleTimeout?

xialin picture xialin · Sep 13, 2019 · Viewed 7.4k times · Source

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.

Answer

Eric Anderson picture Eric Anderson · Sep 14, 2019

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.