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 the Problem/Challenge?
Imagine you are writing microservices. Your company has adopted the microservices architecture you have Address Service, Employee Service, Course Service, Student Service, etc, etc. You have many spring boot applications and all the spring boot application has been deployed into many different servers and you might have thousands of applications. Right now all these applications let’s say Course Service wants to connect to the Address Service, Student Service wants to connect to the Course Service, and wants to get some course-related data then how will these servers communicate with each other? We will simply make a REST call and all these servers will communicate with each other using the REST API. But the real challenging part is when a server wants to connect to another server then before this server connects to this server it needs to know the IP address, and it needs to know the port number where this particular application is running in the server.
This is not going to be a simple job to manage the thing where you have thousands and thousands of applications. How you will manage the server IP? How you’ll maintain their port number? Because every server, when you want some data from another server, it needs to connect to them, and in order to connect to them it needs to know the IP and the server address of that and it will be a really critical job to handle all the IP and the server ports where you have thousands of servers where you have split your one application into thousands of different modules and deployed into different servers. Don’t you think managing the IP and the server URL will be critical? In the case of a Monolithic Application, there we have only one server so we used to remember the server IP and the port but now your monolithic has been split into thousands of applications how’ll be handling that? And for that Spring Cloud is providing us with Service Discovery and Service Registry to handle this problem.
What’s Service Discovery and Service Registry 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
Point to Remember:
- Client-Side Service Discovery Example: Netflix Eureka, Zookeeper, Consul
- Server-Side Service Discovery Example: NGNIX, AWS ELB