Service discovery enables horizontal scaling in microservices by automatically tracking which instances of a service are currently running, so new instances become reachable the moment they start without manual configuration.
Key Points: • When a service scales out under load, each new instance registers itself with the service registry (e.g., Eureka or Consul) on startup. • Clients or load balancers query the registry to get the current list of healthy instances rather than relying on a static, outdated list. • Health checks continuously remove unhealthy or terminated instances from the registry, so traffic isn't sent to instances that no longer exist. • This automatic registration/deregistration is what makes autoscaling (e.g., Kubernetes Horizontal Pod Autoscaler) work seamlessly with service-to-service calls. • Without service discovery, scaling out would require manually updating configuration or DNS entries every time instance counts changed.
Example: When Kubernetes scales the Order Service from three pods to eight under load, each new pod is automatically registered as a Kubernetes Service endpoint, so the API Gateway immediately starts routing a share of traffic to them without any manual intervention.
Interview Tip: A concise interview answer is:
"Service discovery lets new instances register themselves automatically as they come up, so when a service scales out to handle load, callers immediately see and use the new instances through the registry instead of needing manual configuration updates. Combined with health checks, it also ensures traffic never goes to instances that have failed or been terminated."