How to Tune gRPC Keepalive and Health Checks
Detect dead gRPC connections early with keepalive and health checking: pings, timeouts, server policy and avoiding keepalive abuse.
On this page
Introduction
A gRPC connection can appear open while the path has silently died: a NAT timeout, a load balancer that dropped the flow, or a peer that crashed without closing. Without activity, the client does not notice until it sends a request and waits for a timeout. Keepalive sends periodic pings to detect the dead connection early, and health checking verifies the server is ready to serve. Configuring both well means the client fails fast and reconnects instead of hanging.
Why a gRPC Connection Can Be Dead
gRPC runs over HTTP/2 on a long-lived TCP connection, which is the source of its efficiency and of this failure mode. Network equipment drops idle flows (NAT and firewall timeouts), a load balancer may drop a flow it does not see traffic on, and a peer can crash without a clean TCP close. In all of these, the client’s connection looks open but the peer is gone.
Without keepalive, the client discovers this only when it sends a request and the response never comes, so every caller waits for a timeout. Keepalive detects the dead connection in seconds by sending a ping it requires an answer to, and the client reconnects before the next request.
# a dead-but-open gRPC connection
NAT / firewall idle timeout -> flow dropped silently
load balancer drops idle flow
peer crashes without TCP close
# client's connection looks open; without keepalive it learns only on timeout
# keepalive: periodic ping with a required answer => detects death in seconds
Client Keepalive Configuration
The client sets a keepalive time (how often to ping when there is no activity) and a timeout (how long to wait for the ping’s acknowledgement before declaring the connection dead). When the timeout fires, the connection is closed and, depending on policy, a new one is established.
Choose the time relative to the idle timeout of the network in between: too long and the flow dies before the first ping; too short and you ping pointlessly and may be throttled. The timeout should be short enough to detect death quickly but long enough not to fire on a slow but alive link.
import * as grpc from '@grpc/grpc-js';
const client = new OrderClient('dns:///orders:443', grpc.credentials.createSsl(), {
'grpc.keepalive_time_ms': 30_000, // ping when idle for 30s
'grpc.keepalive_timeout_ms': 10_000, // wait 10s for the ack, then close
'grpc.keepalive_permit_without_calls': 1, // ping even with no active RPC
'grpc.http2.max_pings_without_data': 0, // allow pings without data
});
// on the connection failing, the client reconnects before the next request
Server Keepalive Policy
The server has its own keepalive enforcement policy: how often it allows a client to ping, and the minimum ping interval it tolerates. A client that pings more often than the policy allows can have its connection closed or receive GOAWAY with an ENHANCE_YOUR_CALM error, which looks like a mysterious disconnect.
Align the client and server: a client pinging every thirty seconds needs the server to permit that, and the server’s minimum-ping-interval should be at or below the client’s interval. Document the policy so the client’s keepalive is not mistaken for an attack.
// Server-side keepalive enforcement policy (grpc-go).
import "google.golang.org/grpc/keepalive"
srv := grpc.NewServer(
keepalive.EnforcementPolicy{
MinTime: 20 * time.Second, // allow pings >= every 20s
PermitWithoutStream: true, // allow pings with no active RPC
},
keepalive.ServerParameters{
Time: 60 * time.Second, // server pings idle clients after 60s
Timeout: 20 * time.Second,
},
)
Client-Side Health Checking
Keepalive detects a dead transport; health checking detects a server that is connected but not ready to serve, such as one still starting or one whose dependency is down. The standard gRPC health-checking service lets a client query a service’s status, and the client can use it to pick a ready backend or to fail fast.
Configure health checking on the client so it only routes to backends reporting SERVING, and use the same signal for readiness in orchestration. A backend that is connected but unhealthy should be removed from the pool, not sent requests that will fail.
# transport vs readiness
keepalive -> is the TCP/HTTP2 connection alive at all?
health checking -> is the server READY to serve (deps up, warm)?
# client: route only to backends reporting SERVING
# orchestration: the same health signal drives readiness probes
Load Balancing and Reconnection
A gRPC client can load-balance across backends if it is given the addresses (via DNS with a suitable resolver, or a service config). The client maintains subchannels and, when one fails, moves traffic to the others and reconnects the failed one. This is what makes a dead connection to one backend invisible to callers.
Pick the right policy: round-robin across a known set, or a pick-first that switches on failure. The choice interacts with keepalive and health checking: with health checking, unhealthy subchannels are excluded; with keepalive, dead ones are detected and reconnected. Together they keep the client serving from live backends.
const client = new OrderClient('dns:///orders-headless:443', grpc.credentials.createSsl(), {
// round-robin across the resolved backend addresses
'grpc.service_config': JSON.stringify({
loadBalancingConfig: [{ round_robin: {} }],
healthCheckConfig: { serviceName: 'orders' },
}),
'grpc.keepalive_time_ms': 30_000,
});
// a failed subchannel is excluded/reconnected; callers keep using live ones
Diagnosing Keepalive Problems
Symptom: connections closing with GOAWAY and ENHANCE_YOUR_CALM. Cause: the client pings faster than the server’s policy allows. Fix: raise the client’s keepalive time or lower the server’s minimum, so they agree. This is the most common keepalive misconfiguration and it looks like a server bug.
Symptom: requests hang for the full deadline on an idle-then-used connection. Cause: no keepalive, so the client did not notice the flow died. Fix: enable keepalive with a time shorter than the network’s idle timeout. Measure the network’s idle timeout rather than guessing it.
# symptom -> cause -> fix
GOAWAY ENHANCE_YOUR_CALM client pings faster than server policy
-> raise keepalive_time or lower server MinTime
hang until the RPC deadline no keepalive; dead flow undetected
-> enable keepalive below the network idle timeout
server disconnects on connect keepalive_permit_without_calls mismatch
-> align client and server policy
Common Pitfalls and Conclusion
Do not set a very short keepalive time; it wastes bandwidth and trips the server’s policy. Do not forget to align the client’s interval with the server’s enforcement policy. Do not rely on keepalive alone; add health checking so an unready server is excluded. And do not assume a connection is alive because it is open; keepalive is what makes that true.
gRPC keepalive detects a connection that has silently died, and health checking detects a server that is connected but not ready. Set a keepalive time below the network’s idle timeout with a short timeout, align the client interval with the server’s policy, enable health checking for readiness, and use a load-balancing policy that excludes failed subchannels. Then a dead connection is noticed in seconds, not at the next request’s timeout.
Sources: gRPC: Keepalive guide, gRPC: Health checking, grpc-go: keepalive package.
FAQ
- Why does the server disconnect my gRPC client?
- Most often because the client pings more frequently than the server's keepalive enforcement policy allows, which trips GOAWAY with ENHANCE_YOUR_CALM. Raise the client's keepalive time or lower the server's minimum ping interval so they agree.
- Keepalive or health checking?
- Both, for different questions. Keepalive asks whether the connection is alive, detecting a silently dropped flow. Health checking asks whether the server is ready to serve, detecting a connected but unready backend. Together they keep the client on live, ready backends.
Report a problem with this articleOpens a pre-filled issue on GitHub.