What is service discovery and how do client-side and server-side discovery differ?
Understand service discovery in microservices and how client-side and server-side discovery differ, with a service registry, examples and interview answers.
Expected Interview Answer
Service discovery is the mechanism by which microservices find the current network location (host and port) of the other services they need to call, without those addresses being hardcoded, because instances come and go dynamically in the cloud.
Services register themselves in a service registry when they start and deregister when they stop, so the registry always holds the live list of healthy instances. In client-side discovery, the calling service queries the registry itself and picks an instance to call, doing its own load balancing. In server-side discovery, the client calls a fixed endpoint such as a load balancer or gateway, and that intermediary queries the registry and forwards the request, keeping discovery logic out of the client.
- Removes hardcoded network addresses
- Handles instances scaling up and down dynamically
- Routes around failed or unhealthy instances
- Enables load balancing across instances
- Decouples callers from where services actually run
AI Mentor Explanation
Service discovery is like finding out which players are actually on the field right now. A registry is the team sheet updated live as substitutes come on. In client-side discovery the captain reads the sheet himself and decides who to pass to; in server-side discovery he simply tells the twelfth man, who checks the sheet and relays the ball to whoever is available.
Step-by-Step Explanation
Step 1
Service registers
On startup each instance registers its host and port in the service registry and sends health signals.
Step 2
Registry tracks health
The registry keeps only healthy instances by expiring entries that stop sending heartbeats.
Step 3
Caller needs a service
A service needs to call another and must resolve a live address rather than a hardcoded one.
Step 4
Client-side path
The caller queries the registry directly, gets the instance list, and load-balances the call itself.
Step 5
Server-side path
The caller hits a fixed load balancer or gateway, which queries the registry and forwards the request.
What Interviewer Expects
- Definition tied to dynamic instances and a service registry
- Clear contrast between client-side and server-side discovery
- Understanding of where load balancing happens in each
- Awareness of health checks and deregistration
- Examples such as Eureka (client-side) or a load balancer/DNS (server-side)
Common Mistakes
- Hardcoding IP addresses instead of using a registry
- Confusing service discovery with an API gateway
- Forgetting that instances must deregister or expire when unhealthy
- Not knowing which side performs load balancing in each model
Best Answer (HR Friendly)
“Service discovery is how the small services in an app find each other when their addresses keep changing. Sometimes a service looks up the address itself (client-side), and sometimes it asks a middle layer that looks it up for it (server-side).”
Code Example
// Query the registry, then load-balance yourself
List<Instance> instances = registry.lookup("order-service");
Instance target = loadBalancer.choose(instances);
Response res = httpClient.get(target.url() + "/orders/42");Follow-up Questions
- What is a service registry and how does it stay accurate?
- How do health checks affect discovery?
- What are the trade-offs of client-side vs server-side discovery?
- How does DNS-based discovery work in Kubernetes?
- What happens when the service registry itself goes down?
MCQ Practice
1. What problem does service discovery solve?
Service discovery lets services locate the current host and port of other services whose instances change dynamically.
2. In client-side discovery, who performs load balancing?
In client-side discovery the client queries the registry and chooses an instance, so it load-balances itself.
3. In server-side discovery, the client calls?
The client calls a fixed intermediary that queries the registry and forwards the request to a healthy instance.
Flash Cards
What is service discovery? — The mechanism for finding the current network location of service instances that change dynamically.
What is a service registry? — A live database of available, healthy service instances that register on start and deregister on stop.
Client-side discovery? — The caller queries the registry and load-balances the request itself.
Server-side discovery? — The caller hits a fixed load balancer or gateway that queries the registry and forwards the request.