Service Discovery In Microservices

GaurangMittal
The Fresh Writes
Published in
4 min readDec 24, 2022

In this article, I’ll try to explain why service discovery is necessary, what client-side and server-side service discovery are, and what a service registry is.

Need Of Service Discovery

Context

Assume that as we transition from a traditional monolithic to a modern microservices architecture, we notice that our services are becoming bottlenecked, so we decide to introduce multiple instances of the services we consume to balance the load (req/res).

By doing so, the clients of the service as well as the routers placed between the services should know the location of the service instances.
But wait a second, these are dynamic in nature, i.e., their number is constantly changing depending on the load we are receiving.

So thereby this brings the need to introduce a functionality that could help us in identifying these instances exactly. which brings me to my point about using Service Discovery.

Introduction

Clients of a service use either client-side discovery or server-side discovery to determine the location of a service instance to which they need to send requests.

a. It is a database of services, their instances, and their locations.

b. Every service instance needs to register with the service registry on startup and deregister on shutdown.

c. Clients of the service and/or routers query the service registry to find the available instances of the service.

d. They invoke Health Check APIs to find out the condition of the service instance and whether it can handle the incoming request.

e. Service registry instances must be deployed on fixed and well-known IP addresses. Clients are configured with those IP addresses.

f. A few examples exist, such as Netflix Eureka and Apache Zookeeper.

Service Instances Registration Pattern

A. Self Registration Pattern — Service instances register themselves.

B. 3rd party registration pattern — A 3rd party registers, the service instances with the service registry.

→ The clients of the service registry need to know the exact location of the service registry instance as well, and they should be highly available.

Service Discovery Patterns

1. Client-Side Service Discovery

credits : Chris Richardson

How does the client of a service — the API gateway or another service — discover the location of a service instance?

Solution

a. In client-side service discovery, the clients of the calling service query the service registry. The registry replies by sending the location of the service instances that are free to take the incoming request.

b. The client-side application needs to be coupled with the service registry request mechanism.

c. The request to the service registry is generally made via a HTTP-based or RPC mechanism.

Advantages of Client-Side Service Discovery

  1. Less Latency as less network hopes.
  2. Less moving parts as we are attaching the calling mechanism with the client.

Disadvantages of Client-Side Service Discovery

  • This pattern couples the client with the Service Registry.
  • You need to implement client-side service discovery logic for each programming language or framework used by your application, e.g., Java/Scala, JavaScript/NodeJS. For example, Netflix Prana provides an HTTP proxy-based approach to service discovery for non-JVM clients.

2. Server-Side Service Discovery

credits : Chris Richardson

a. The client makes a request to the router (aka load balancer), which needs to redirect the request to that service instance, which can handle the request at that point in time.

Then the router calls the Service Registry by making a query to it.

c. The service registry let us know which instance is free to take up the task using the approach we discussed in the first part of this article.

d. We are decoupling the client from any kind of implementation related to the service registry.

e. The router just needs to know the location of the service registry to get the work done.

f. Sometimes the service registry is a part of the router (also called the “inbuilt registry”).

For example, AWS ELB acts as a registrar as well as balancing traffic (both external and internal).

Advantages of Server-Side Discovery

  • Client code is simpler since it does not have to deal with discovery. Instead, a client simply makes a request to the router.
  • Some cloud environments, such as AWS Elastic Load Balancer, include this functionality by default.

Disadvantages of Server-Side Discovery

  • The router is another system component that must be installed and configured. It will also needs to be replicated for availability and capacity.
  • The router must support the necessary communication protocols (e.g., HTTP, gRPC, Thrift, etc.) unless it is a TCP-based router.
  • More network hops are required than when using Client-Side Discovery.

--

--