Service Discovery In Distributed Architecture

Usha Devasi
3 min readApr 10, 2020

Service Discovery is one of the building blocks of microservices.

There are many ways to facilitate the process of Service Discovery.
But before starting let’s see what we will archive from this article.

  • What is a Service Discovery?
  • Why we need Service Discovery?
  • What are the types of Service Discovery and how they are different from each other?
  • What are the Components of Service Discovery?
  • What tools/processes that help to achieve Service Discovery?

What is a Service Discovery?

It is a process of finding the network location of the service for inter-service communication. In other words just go with the literal meaning which is Discovering/finding some service.

The need for Service Discovery :

Service Discovery in Monolithic Service

Service Discovery was part of monolithic Structure too but we never saw it that way. Monolithic Service is logically compromised by lots of services we can think of them as segregation of different packages. Let’s talk logically and take an example and see what happens 🧐, assume there are a lot of services and out of service, A wants to call service B. In Traditional/monolithic way service B would expose a method, mark it as public, and then service A can just call it. They’re in the same application. It’s just a function call(in-memory function). So while A is calling a function in B, it may just a few nanoseconds because here we are doing an in-memory jump, and it’s all in-process so we need not worry about what gonna happened to the data, how it will get there, did we encrypt it?

Challenges in Distributed System

But all of this changes as we come into this distributed world, specifically talking about microservices. So now we have a Service A that wants to talk to service B. Ahh 😱 , but where is the service B 🤔? It’s no longer running on the same machine. It’s no longer part of the same application as it used to be and now we’re going over a network.

What to do? So, let’s give an idea to store the services in one place as we do in the phone number Directory which is having a name and the phone number in it. We will call it a Service Directory as we gonna store services instead of phone numbers.
This Directory will help service A to get the location/IP address of service B to communicate over the network. Now see, this directory is nothing but service registry in terms of microservices (a key component of Service Discovery)

Different Components:

  1. Service Provider: These are those which will register itself into the service registry when it enters into the system and de-register while leaving the system.
  2. Service consumer: are those who get the location of a provider from the registry, and then talk to the provider.
  3. Service Registry: handles the latest locations of the service providers and serve service consumers.

Types of Service Discovery :

  1. Client-side Service Discovery
  2. Server Side Service Discovery

Client-Side Service Discovery: Clients query the service registry, select an available instance, and make a request as shown in the below diagram.Here one extra step is present as the client first asks the registry to get the service location and then contact Service.

Client-side Service Discovery

Server Side Service Discovery: clients make requests via a router, which queries the service registry and forwards the request to an available instance. In easy words this means here the client goes to the registry and tells him to contact the corresponding service on his behalf as shown in the diagram.

Server-side Service Discovery

Who will help in Service Discovery SetUp?

In the market there are lots of service registry available such as Netflix Eureka, Hashicorp Consul, Apache zookeeper, etc which will be required when you set up service discovery infrastructure but in some deployment environments, service discovery is inbuilt, e.g. K8s, Marathon.

Check out how to configure Eureka as Service Registry with Spring Cloud here.

--

--

Usha Devasi

Tech Lead/ Engineering Manager, Mentor, Coach, Certified Professional Scrum Master and SomeOne who is Passionate about Learning and exploring.