How does gRPC support health checking and service discovery?
Understand gRPC health checking via the Health service Check and Watch RPCs, plus how resolvers and load balancing handle service discovery.
Expected Interview Answer
gRPC standardizes health checking through the grpc.health.v1.Health service, which exposes Check and Watch RPCs that report whether a named service is SERVING or NOT_SERVING, while service discovery is handled by a name resolver plus load-balancing policy rather than by gRPC itself.
The Health service lets load balancers, Kubernetes probes, and clients query per-service status instead of just process liveness. For discovery, a gRPC channel uses a resolver (DNS, xDS, or a custom scheme) to turn a target name into a set of backend addresses, then a pick_first or round_robin policy chooses among them. Together, unhealthy endpoints are detected via health checks and rotated out by the load-balancing layer.
- Per-service health, not just process up/down
- Streaming Watch for real-time status changes
- Pluggable resolvers (DNS, xDS, custom)
- Works with Kubernetes gRPC probes
- Automatic rotation of unhealthy backends
AI Mentor Explanation
Before a match, the ground staff check each floodlight tower and signal which are working, while a fixtures desk tells teams which grounds are open. Health checking is the tower inspection reporting SERVING or NOT_SERVING per light; service discovery is the fixtures desk resolving a match name into an actual playable venue, so games only start where a healthy, available ground exists.
Step-by-Step Explanation
Step 1
Implement the Health service
Register grpc.health.v1.Health on the server and set each service's status to SERVING or NOT_SERVING.
Step 2
Expose Check and Watch
Check gives a one-shot status; Watch streams updates so clients react to changes immediately.
Step 3
Wire up probes
Configure Kubernetes grpc probes or a load balancer to call Check against the service name.
Step 4
Configure a resolver
Point the channel at a target scheme (dns:///, xds:///) so names resolve to backend addresses.
Step 5
Pick a load-balancing policy
Use round_robin or pick_first so requests spread across healthy resolved endpoints.
What Interviewer Expects
- Knowledge of the grpc.health.v1.Health service
- Difference between Check and Watch RPCs
- Per-service versus process-level health
- Role of resolvers and load-balancing policies
- Integration with Kubernetes probes
Common Mistakes
- Thinking gRPC has built-in service discovery
- Confusing process liveness with per-service health
- Using an HTTP endpoint instead of the standard Health service
- Ignoring Watch and polling Check in a tight loop
Best Answer (HR Friendly)
“gRPC includes a standard health-check service so systems can ask whether a specific service is ready to serve, not just whether the process is alive. Finding the right server is handled by name resolvers and load balancing, which route traffic to healthy backends.”
Code Example
import (
"google.golang.org/grpc"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
)
func main() {
s := grpc.NewServer()
hs := health.NewServer()
healthpb.RegisterHealthServer(s, hs)
// Mark a specific service as ready to serve
hs.SetServingStatus("myapp.Greeter", healthpb.HealthCheckResponse_SERVING)
}Follow-up Questions
- What is the difference between the Check and Watch RPCs?
- How does Kubernetes probe a gRPC service's health?
- What does an empty service name mean in a health check?
- How do resolvers and load-balancing policies work together?
MCQ Practice
1. Which standard service defines gRPC health checking?
The grpc.health.v1.Health service exposes the Check and Watch RPCs used for health checking.
2. What does the Watch RPC provide over Check?
Watch streams status updates so clients learn about SERVING/NOT_SERVING transitions in real time.
3. How does a gRPC client find backend addresses?
A resolver (DNS, xDS, custom) turns a target name into addresses, and a policy picks among them.
Flash Cards
What service handles gRPC health checks? — grpc.health.v1.Health, with Check (one-shot) and Watch (streaming) RPCs.
Check vs Watch? — Check returns current status once; Watch streams status changes over time.
Does gRPC do service discovery itself? — No — a name resolver plus a load-balancing policy handles discovery.
What are the health statuses? — SERVING, NOT_SERVING, and SERVICE_UNKNOWN per named service.