Service Discovery in Microsrvice
Service discovery is a key component of distributed systems. In order to make a request, the client need URL of a service instance. But cloud based microservices application, Service instances have dynamically assigned network locations, or changes dynamically autoscaling, upgrades, failures [Jason Wilder, “Open-Source Service Discovery”, http://jasonwilder.com, 2014]. Briefly, The problem is how do clients determine the location (IP address and port) of service.
There are two key parts of service discover in Microservice: Service Registration and Service Discovery.
- Service Registration: The process of a service registering its location to central registry. Usually, It register its host and port and may also authentication and environment details.
- Service Discovery: The process of a client get information from central registry to know about the location of services.
Service Discovery can be classified 2 main patterns: client side discovery and server side discovery.
The Client Side Discovery Pattern
The client determine the network locations of service instance by querying a service registry and It can use a load balancing algorithm to select which service instances to makes a request.
- Service instant send register request with its network location to the service registry when it startup.
- Service instants will be removed from the service registry when it terminates.
- Service register is heartbeat mechanism to get status of service registration.
The following diagram shows the structure of Client Side Discovery Pattern.
There are several benefits when using this pattern, such as flexible, application specific load balancing, fewer network hops, but one significant drawback of client side discovery pattern is client with the service registry. We need must implement register and discovery mechanism for each programming language or framework used by clients
The Server Side Discovery Pattern
When using Server side discovery, Clients just simply make requests to a service instance via a load balancer. The load balance will take the role of client in client side discovery pattern in service discovery and load balancing [Fawad Khaliq, “Fernando Sanchez, Service Discovery and Registration in a Microservices Architecture”, PLUMgrid, 2016]. The following diagram shows the structure of Client Side Discovery Pattern.
The server side discovery pattern has a variety of benefits and drawbacks. One great benefit of this pattern is that eliminates the need to implement discovery logic for each programming language or framework used by service clients. This server side discovery pattern also has some drawbacks: more network hop, need to setup and manage load balancer however this job we need to do even though we use client side discovery pattern
The Service Registry
As mentioned earlier, the service registry is a key part of service discovery. It is a database containing the network locations of service instances. Database of service registry need to be up to date. Clients can cache network locations learned from the service registry to improve performance. A service registry should be configured as a cluster of servers for high availability and need to use a replication protocol to maintain consistency data among servers in cluster.
Service instances must be registered with and deregistered from the service registry from the service registry. There are two main ways to handle register and deregistered. The first is the self registration pattern. The second way is the third party registration pattern.
The Self Registration Pattern
A service instance is responsible for registering and deregistering itself with the service registry. A service instance also sends heartbeat requests to prevent its registration from expiring. The following diagram shows the structure of self registration pattern.
The Third Party Registration Pattern
When using this pattern, service instance does not involve to register or deregister it to service register, Another system component, the service registrar handles the registration. The service registrar tracks changes of instances status by subscribing the deployment environment events (deployment, termination, scaling) and detect the instance crash by healthcheck mechanism. The following diagram shows the structure of third party registration pattern:
There are many third party open source project for Service Registrar such as etcd and Consul. Below is table for comparing some Service Registers and Service Discovery in terms: Purpose, Language, Dependencies, Integration Style.
Table 4.1: Comparing some Service Register and Service Discovery
There are many way to implement service discovery and select pattern for discovery. Each method also have Advantages also Disadvantages But the choice is your.