Skip to content
tezvyn:
Menu

Service Discovery: How Services Find Each Other

Source: Wikipedia: Service discoveryMediumHow cards are made

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.

Interview question

What problem does service discovery primarily solve in dynamic microservice environments?

  • a.It automatically scales up or down the number of service instances based on demand.
  • b.It allows services to find the current network locations of other services without relying on static, hardcoded addresses.Correct
  • c.It centralizes the logging and monitoring of inter-service calls.
  • d.It enforces consistent API versioning and data contracts between communicating services.
Why?

Service discovery's primary role is to enable services to dynamically locate each other's network addresses, which are constantly changing in modern, distributed systems. While it facilitates features like auto-scaling and load balancing, it does not directly perform them, nor is it responsible for logging or API contract enforcement.

Just read this? Test yourself on what you have been reading.

Read the original → en.wikipedia.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on distributed systems — each one lists the topics its interview covers.

See open roles