Service Discovery: How Services Find Each Other
Instead of hardcoding IP addresses, services ask a central registry for the live address of other services they need to call. This is crucial in dynamic microservice environments where IPs change constantly.
WHY IT EXISTS: In static, old-school systems, you could hardcode a server's IP address into a configuration file and it would work for years. Modern distributed systems, especially with microservices and containers, are dynamic. Service instances are created, destroyed, and moved constantly, making their IP addresses ephemeral. Hardcoding addresses is impossible, creating the need for an automated way for services to find each other.
THE MENTAL MODEL: Think of service discovery as a dynamic contact list for your applications. Instead of memorizing a friend's physical address (which might change), you just look up their name in your phone to get their current number. Similarly, a service doesn't know the database's IP address; it just knows it needs to talk to the 'database-service' and asks a central registry (the contact list) for its current, live network location.
HOW IT WORKS: There are two main patterns. First, client-side discovery: the client application queries a service registry (like Consul or Eureka) to get a list of available service instances, then chooses one to connect to. Second, server-side discovery: the client makes a request to a single router or load balancer, which then queries the registry and forwards the request to a healthy instance. In both cases, services must register themselves with the registry upon startup and de-register when they shut down, often by sending a periodic heartbeat to prove they are still alive.
WHEN TO USE IT: Use service discovery in any distributed system with more than a handful of components, especially in microservice architectures. It is essential when using container orchestrators like Kubernetes or cloud platforms where virtual machine and container IPs are unpredictable and change frequently. It is the foundation for enabling auto-scaling, high availability, and zero-downtime deployments.
WHEN NOT TO USE IT: For very simple, monolithic applications or systems with a few stable, long-lived services with static IPs, it can be overkill. If you have one web server and one database server whose IPs have not changed in five years, manually configuring the connection is simpler and introduces fewer potential points of failure.
ONE CANONICAL EXAMPLE: Kubernetes provides built-in service discovery. When you create a Deployment for your application, you also create a Service object. This Service gets a stable DNS name (e.g., 'user-api'). Kubernetes continuously updates this Service's endpoints with the real, transient IPs of the running application pods. Any other pod in the cluster can then simply send a request to the 'user-api' name, and Kubernetes handles routing it to a healthy, running pod, even as those pods are created, destroyed, or rescheduled.
Read the original → en.wikipedia.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.