Microservice - Deep dive
By: Jolly Srivastava
Tags: System Design Interview, Api Gateway, Microservices, Service Registry

SERVICE REGISTRY:

Let us consider a situation where we have horizontally scaled search+product, inventory, merchants, and finance microservice. Let's say we have 3 instances of merchants running and one goes down. The question here arises that whose responsibility is it to inform API gateway about the network address of the 3rd instance which is now down and the network address of the new 4th instance which came up as a part of auto-scaling?

There can be a situation where the “shipping” service wants to interact with the “merchant” service. How will the shipping service get to know about the IP address of the merchant service?

These are the problem which we will solve using “service discovery”.

Service discovery is a pattern to identify network add of other services using service register which is kind of DB which contains the list of instance and IP address. so if the client wants to interact with the shipping service directly it can query service register and get the list of network addresses for that particular service.

The question here arises that how service registry will get to know the latest network address of any microservice?

Self registry: Let's say one instance gets added up in “shipping” microservice, shipping microservice then interacts with service registry and adds/updates its network address, port, and service name. If any of the running instances goes down then after every x no of minutes/seconds microservice keeps on updating their location, if any service fails to update then the service registry deletes that information. Third-party: in this case, a service registry asks microservices about their address and port. It keeps on checking microservices periodically and keep updating the service register/DB. The advantage of this is that now the registry knows exactly how many instances of service are running and the state of each and every service.

Let’s talk about discovery now. It is the counterpart of registration. We basically access all the information present in the registry.

This answers “There can be the situation where the “shipping” service wants to interact with the “merchant” service. How will shipping service get to know about the IP address of the merchant service?”

The client can now request service registry and gets the list of network address lets say ip1:<port1>, ip2:<port2> and ip3:<port3> in case of merchant also load balances the requests among these machines. client can’t keep on talking with service registry every time if it needs to interact with microservices, there will be a designated TTL. What will happen if the service registry updated itself but at the client end information still is not updating. This is one disadvantage of talking with services directly and is not advisable. Also, traffic on the service registry will be high as clients keep asking for the latest data continuously. This is what the client discovery is. There’s something known as server discovery as well where instead of a client talking with service registry, API gateway requests service registry to get the updated list of a network address of all the microservices and to interact with all the microservices.

Service Registry