Service Discovery
One of the challenges in micro-service architecture is how services communicate with each other. With dynamic change in the number of instances and the network location of the services, static IP addresses don’t work, consequently a more dynamic approach is required.
Service discovery is a way for services to find and communicate with each other in a highly decoupled architecture.
Service Registry
One of the key components of service discovery is the service registry. Service registry is a database of network location and instances of currently available services. A service instance registers itself to the service registry on startup and unregisters itself after shutdown or crash.
Service instances are registered in one of the two ways, the self‑registration pattern or the third-party registration pattern. In the self‑registration pattern, a service will register itself with a service registry, whereas a third-party registration pattern uses a service registrar which registers the service on startup and unregisters it in case of crash or shutdown.
Service discovery can be done on both the client and server-side, In client-side service discovery, the client is responsible to identify the network location of a service instance from the service registry, whereas in the server-side discovery client queries to an API gateway which retrieves all the instances of service from service registry and then applies load balancing logic to decide which one to use.
Client Side Service Discovery
In client-side service discovery, the client queries the service registry to get the network location of the available instances of a service and then uses some load balancing algorithm to select one of the available instances to make the request.
Server Side Service Discovery
In server-side service discovery, the client requests an API gateway which then queries the service registry to identify all the available instances of service, then API gateway uses some load balancing algorithm to select one of the available instances to make the request.
Server-side service discovery makes the client application lighter as it does not have to deal with the lookup procedure and makes a request for services to the service registry.
One of the examples of server side service discovery is AWS Elastic Load Balancing (ELB).
Conclusion
In a micro-service architecture, instance and the network location of services changes dynamically. Consequently for a client to request a service it must use a service‑discovery mechanism.