Microservices are small, loosely coupled distributed services. Microservices architecture evolved as a solution to the scalability, independently deployable, and innovation challenges with Monolithic Architecture. It provides us to take a big application and break it into efficiently manageable small components with some specified responsibilities. It is considered the building block of modern applications. Before understanding What is Service Discovery let’s understand what is the need for Service Discovery in Microservices.
What’s Service Discovery in Microservices?
Suppose we have Service-A & Service-B and we have our Load Balancer placed inside a different server. Now let’s introduce our Discovery Service. Now what this discovery service will do now whenever Service-A and Service-B want to communicate with each other then whenever we are starting our Microservices we’ll be registering them with Discovery Service. And this discovery service right now will know what is the IP and port number of Service-A and what is the IP and port number of Service-B. All detailed information will be there with Discovery Service. Similarly, if we have many different instances of Service-B, all this Service-B which is running in different servers will be registering their information with Discovery Service. So it is one central location where we’ll be managing our host and the port number information inside this particular server. This is basically called registration because all the services whenever they are starting off they are registering themselves with the discovery service and now the discovery service is maintaining all their information inside a particular map or a list or a database. We called it a Service Registry.
So, Service Registry is a crucial part of service identification. It’s a database containing the network locations of service instances. A Service Registry must be highly available and up-to-date. Here, inside Service Registry we have 4 different instances of Service-B and they are running in some port number and some IP address. Similarly, for Service-A we have one different instance.
Now Service-A wants to connect to Service-B. Now the load balancer once get the request, it is gonna do a query with the discovery service that, hey, can you tell me what instances are there for Service-B? Now the load balancer finds out that there are this many instances available where Service-B has been deployed. Now Load Balancer is going to dispatch to one of the servers by looking into Service Registry. It can take all four instances of Service-B and whoever has less load then to balance the load, it can send the request to there.
Note: Don’t mix up load balancing and service discovery. The Load Balancer job is to do load balancing while the Service Discovery job is to do discovering service info. When we build Microservices and call other microservices, we need service discovery to find the hosts and IP info and if multiple hosts are available then the load balancer helps to pick one and make a call in a load-balanced way.
Types of Service Discovery
There are two types of Service Discovery
- Client-Side Service Discovery
- Server-Side Service Discovery
In this article, we are going to explain Server-Side Service Discovery.
Server Side Service Discovery in Microservices
So, the example we have taken above we call it as Server-Side Discovery. The entire concept is called a Server-Side Discovery. Why so? Let’s understand it step by step.
- Step 1: The client (Service-A) makes a request to a server that is it may be a Router, Load Balancer, or Middleman. In this case, it’s a Load Balancer.
- Step 2: The server (Load Balancer) does a query with a Discovery Service.
- Step 3: The Discovery Service responded back with available numbers of instances of Service-B that the Load Balancer can call.
- Step 4: Then the Load Balancer server picks up one of the Service-B instances and makes a call.
- Step 5: Here the Service-A(Client) doesn’t talk to the Discovery Service directly. It calls another server (Load Balancer) which helps to discover Service-B URL info.
And this complete pattern we called Server Side Discovery.
Advantages: In this approach, the Load Balancer does the job of load balancing. This is the major advantage of this approach. Undoubtedly, developing this level of abstraction makes the Service Consumer more lightweight, as it doesn’t have to deal with the lookup procedure. So there’s no need to implement the discovery logic individually for each language and framework that the Service Consumer uses.
Disadvantages: The disadvantage is we must set up and operate the Load Balancer unless it’s already given by the deployment environment.
Example:
- NGNIX: Nginx is a web server that can also be used as a reverse proxy, load balancer, mail proxy, and HTTP cache. Nginx is a free and open-source software.
- AWS ELB: Elastic Load Balancer (ELB) is a service provided by Amazon in which the incoming traffic is efficiently and automatically distributed across a group of backend servers in a manner that increases speed and performance. It helps to improve the scalability of your application and secures your applications.