Govur University Logo
--> --> --> -->
...

In a microservices setup, how do services find and talk to each other without needing to know each other's exact network addresses ahead of time?



In a microservices setup, services find and talk to each other without knowing exact network addresses ahead of time through a mechanism called Service Discovery. This is essential because service instances are dynamic; they can be created, destroyed, or moved, leading to constantly changing IP addresses and port numbers. Service Discovery solves this by providing a central directory for all active service instances.

The core component of this system is the Service Registry. The Service Registry is a highly available database that stores the network locations (IP address and port) of all currently available service instances, mapped to their logical service names. For example, it might list multiple instances of an "Order Service" along with their respective network addresses.

When a new service instance starts up, it must register itself with the Service Registry. This process is called Service Registration. There are two main patterns for registration: Self-Registration and Third-Party Registration. In Self-Registration, each service instance is responsible for registering its own network location, service name, and potentially other metadata with the Service Registry upon startup and deregistering upon shutdown. It also periodically sends heartbeats to the registry to indicate it is still alive and healthy. In Third-Party Registration, an external agent, often called a Registar or Service Registrar, monitors service instances and automatically registers and deregisters them with the Service Registry on their behalf, typically by observing changes in the infrastructure like a container orchestrator.

Once services are registered, client services need a way to look up the network addresses of the services they want to communicate with. This is the Service Discovery process for clients, which also has two main patterns: Client-Side Discovery and Server-Side Discovery. In Client-Side Discovery, the client service, or a dedicated discovery component integrated with it, directly queries the Service Registry for the network locations of all available instances of a target service. The client then uses a load-balancing algorithm to select one of these instances and make the request directly. For example, an "Order Service" needing to call an "Inventory Service" would ask the Service Registry for all instances of "Inventory Service," receive a list of IP:Port combinations, and then choose one to send its request to.

In Server-Side Discovery, the client service makes its request to a router, load balancer, or API Gateway, which acts as an intermediary. This intermediary is responsible for querying the Service Registry to find available instances of the target service. After obtaining the network locations, the intermediary forwards the client's request to one of the healthy instances. The client service itself does not interact directly with the Service Registry to find the target service's address. An example is a web browser (client) making a request to an API Gateway, which then discovers and routes the request to an appropriate microservice instance.

To ensure the Service Registry always has accurate information, health checks are continuously performed. Service instances or their registrars periodically report their health status to the registry. If an instance fails to report health or is explicitly marked unhealthy, the Service Registry removes it from the list of available instances, preventing clients from attempting to communicate with a failed service. This dynamic registration, discovery, and health checking mechanism allows microservices to operate resiliently in highly dynamic environments.