The service discovery pattern uses a centralized server named “service registry” to maintain a global view of microservice network locations. Microservices update their location in the service registry at fixed intervals. Clients can connect to the service registry and get information about the location of the microservices.
There are 2 main service discovery patterns available to implement service discovery for microservices.
In traditional monolithic applications, services run in specific, predefined locations. Thus, it is easy to track their location and clients can easily find and invoke services.
However, when it comes to microservices, their location on the network is assigned dynamically and often changes thanks to features like autoscaling. This behavior makes it difficult to track microservice locations ahead of time.
The service discovery pattern was introduced as a solution, which allows developers to find network locations for microservices without injecting or bundling services.
The service registry stores records about the network locations of microservices. Microservice instances register themselves and indicate their location in the service registry. Clients can then find the location of the microservices using the service registry and invoke them directly to meet their needs.
In the client-side service discovery pattern, the client is responsible for finding the network locations of the microservices and load-balancing requests between them.
The client first queries the service’s registry and retrieves the locations. The client then uses its dedicated load balancer to select a microservice and submits the request.
The server-side discovery pattern solves one of the important client-side discovery problems by decoupling the client from the service registry. With this approach, the client does not have a dedicated load balancer. Instead, the load balancer acts as an intermediary and is responsible for communicating with the service’s registry.
First, the client requests the microservice through the load balancer. The load balancer then queries the service registry and finds the location of the appropriate microservice. Finally, the load balancer routes the request to the microservice.
This pattern is widely used in modern applications because both clients and microservices are independent of the service registry. Most importantly, you don’t have to implement server-side discovery load balancers from scratch, as many deployment environments provide load balancers.
For example, you can use AWS Elastic Load Balancer or proxy servers in Kubernetes environments as server-side discovery load balancers.