4 Service Discovery Patterns for your Microservice
Tags: architecture, discuss, microservices, distributedsystems
Published: 2021-07-31 06:33:41

Let's say you’re writing some code that invokes a service that has a REST API. In order to make a request, your code needs to know the network location (IP address and port) of a service instance. In a traditional application, the network locations of service instances are usually static. 

For example, your code could read the network locations from a configuration file that’s occasionally updated. But in a modern, cloud-based microservices application, it’s usually not that simple. It is much more dynamic.

Service instances have dynamically assigned network locations. Moreover, the set of service instances changes dynamically because of autoscaling, failures, and upgrades.Consequently, your client code must use service discovery.

## What is Service Discovery?

![Service Discovery For Microservices.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627707198767/iyKATW_Dg.png)

From the above image you can see, you can’t statically configure a client with the IP addresses of the services. Instead, an application must use a dynamic service discovery mechanism. 

***Service discovery is conceptually quite simple:*** its key component is a service registry, which is a database of the network locations of an application’s service instances.

The service discovery mechanism updates the service registry when service instances start and stop. When a client invokes a service, the service discovery mechanism queries the service registry to obtain a list of available service instances and routes the request to one of them.

In this article, I will mention about 4 service discovery patterns that you should consider for your microservices.

* [1. Self Registration Pattern](#1-self-registration-pattern)
* [2. Client Side Discovery Pattern](#2-client-side-discovery-pattern)
* [3. Server Side Discovery Pattern](#3-server-side-discovery-pattern)
* [4. 3rd Party Registration Pattern](#4-3rd-party-registration-pattern)

### The services and their clients interact directly with the service registry:

![Service discovery client and service with service registry.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627710400234/O_qjswyGo.png)

## 1. Self Registration Pattern

The first pattern is the Self Registration pattern. A service instance invokes the service registry’s registration API to register its network location. It may also supply a health check URL. 

The health check URL is an API end-point that the service registry invokes periodically to verify that the service instance is healthy and available to handle requests. 

A service registry may require a service instance to periodically invoke a “heartbeat” API in order to prevent its registration from expiring. 

A service instance registers itself with the service registry. Find out more details [here]( http://microser-vices.io/patterns/self-registration.html).


## 2. Client Side Discovery Pattern

The second pattern is the Client-side discovery pattern. When a service client wants to invoke a service, it queries the service registry to obtain a list of the service’s instances.

To improve performance, a client might cache the service instances. The service client then uses a load-balancing algorithm, such as a round-robin or random, to select a ser-
vice instance. It then makes a request to a select service instance.

Find out more details [here](http://microservices.io/patterns/client-
side-discovery.html)

### The deployment infrastructure handles service discovery:

![Service discovery via deployment infrastructure with service registry.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627711333552/yUTFYCnjl.png)

## 3. Server Side Discovery Pattern

In this pattern, Instead of a client querying the service registry, it makes a request to a DNS name, which resolves to a request router that queries the service registry and load-balances requests.

A client makes a request to a router, which is responsible for service discovery. Find out more details [here](http://microservices.io/patterns/server-side-discovery.html).

## 4. 3rd Party Registration Pattern

In this pattern, Instead of a service registering itself with the service registry, a third party called the *registrar*, which is typically part of the *deployment platform*, handles the registration.

Service instances are automatically registered with the service registry by a third party.
Find out more details [here](http://microservices.io/patterns/3rd-party-registration.html).

### Advantages of platform-provided service discovery:

The key benefit of platform-provided service discovery is that all aspects of service discovery are entirely handled by the deployment platform. Neither the services nor the
clients contain any service discovery code. Consequently, the service discovery mechanism is readily available to all services and clients regardless of which language or framework they’re written in.

### Disadvantages of platform-provided service discovery:

One drawback of platform-provided service discovery is that it only supports the discovery of services that have been deployed using the platform. For example, Kubernetes-based discovery only works for services running on Kubernetes.

<hr>

## Thank you for reading
Hope you find these resources useful. If you like what you read and want to see more about system design, microservices, and other technology-related stuff... You can follow me on 

- Twitter - <a href="https://twitter.com/vishnuchi?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-size="large" data-show-count="false">Follow @vishnuchi</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
- Subscribe to my weekly newsletter [here](https://www.getrevue.co/profile/vishnuch).