Let’s build Service Discovery Server — System Design

Siddharth Gangwar
5 min readApr 30, 2022

--

Service Discovery — helps in locating the server helpful when we use scalable micro-services architecture.

What actually is a service discovery?

According to books Service Discovery has the ability to locate a network automatically making it so that there is no need for a long configuration set up process. Service discovery works by devices connecting through a common language on the network allowing devices or services to connect without any manual intervention. (i.e Kubernetes service discovery, AWS service discovery).

Why do we need service discovery?

Sooner the instance got an update and they restarted the Gmail server. Boooom, the private IP changed and now, you need to update Google Calendar service where we used to send mails using Gmail.

Some will say, this can be done, it’s just about maintain the Google Calendar micro-service. But, rethink is this Idea scalable?

What if we do have multiple micro services with multiple instances of each micro service for high availability where all of them contact each other for some task. Google Activity service which tracks all activity of yours on Google Apps. Calendar using Gmail, Youtube using Gmail and many more.

Scalable & intelligent approach would be to build a service discovery server. Which would let you know the address to reach any particular micro service.

Let’s brainstorm how can we build our own

Let’s assume the same example of owning a calendar micro service which requires to mail a reminder before the event happens :)

There are two types of service discovery:

  1. Client side service discovery server
  2. Server side service discovery server

Before jumping on the types, let me tell you more in general about service discovery servers. Each microservice hereby is registered with the service discovery and if any micro service change itself it will simply update its address registered with the service discovery so that other micro services would be able to utilize it.

Server side service discovery server

You need to use the mail service and to discover the address for the same, now we are going to send a request which will return the address for the mail service in response.

Now, you can consume the mail service only thing required was the private IP that we got from the service discovery server. This structure is known as a server-side service discovery server.

Client side service discovery server

On the client-side, if you want to use a mail service then you send the API and the service discovery server will act as middleware and redirect the API to the mail server, and whatever gets the response will forward it to the calendar service.

Let’s try to compare both the approaches

Which one is fast?

The server-side service discovery is faster. The reason behind this is there are just two API calls, one is just a get request with the address of the microservice and the second is the usual work that is to be done with the micro service.

In client-side service discovery, the API call is made to the service discovery server with all the parameters and then the service discovery server forwards it to the micro service, the same API and then response travels the whole way from micro service to service discovery server to the client.

Which is more reliable?

Well in terms of reliability, if we compare the server-side service discovery server seems to be more reliable because it just stores the address and reverts to a GET request.

If we look at the client-side service discovery server then it maintains the connection with the client until the request is fulfilled by the micro service. If our service discovery server went down, and we spin a new instance then the client won’t know whether to terminate the previous connection or not and also the status of the previous request.

Responsibilities are delegated to the server-side discovery server.

What industry is opting?

Nowadays if we look at it, then people have their load balancers configured so that instead of maintaining a service discovery server. AWS Load balancer gives us most of the things that are required for service discovery.

So, now the request comes to your main server (calendar server) then if you want to consume the mailing server just request load balancer again and it will fulfill the request. This approach reduces the workload and provides more productivity among the servers.

BONUS: Why using private IP over public IP or DNS?

The answer is present in the question itself. If you use a public IP address it will take time to resolve and private communication is pretty fast. LAN games have less latency than Online games ;)

Bonus Knowledge: Google uses gRPC protocol, google and jump into a rabbit hole.

Similar articles

If this article helps then do clap and follow me on medium. Let’s connect for a coffee if you are in Bangalore, India & do follow me on Twitter.

--

--

Siddharth Gangwar

I'm a problem solver at heart. Whether the challenge is big or small, I'm passionate about finding efficient solutions to any type of problem.