Microservices Patterns: Service Discovery Patterns

Microservices Pattern Series — Part 03: A deep dive into the world of Service Discovery Patterns — Client Side and Server Side Service Discovery.

Crishantha Nanayakkara
Cloud Native Daily

--

Due to the dynamic nature of a microservices application, it is difficult to invoke a service with a fixed IP address of the service. This is where the concept Service Discovery comes into the picture. This will help the client to know where the service instance is located. In such a situation the Service Discovery component will act as a Service Registry.

A Service Registry is a centralized server / a database containing the location of service instances. In a microservices setup, services update their locations in the service registry at regular intervals. Then service consumers can connect to the service registry and fetch the locations of those services. Netflix Eureka is a popular “Application-Level” open-source Service Registry.

In service discovery, there are two types of patterns.

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

Client Side Service Discovery

Multiple steps are involved in this.

Figure 01 — Client Side Service Discovery
  1. The locations of the Service Provider instances are added to the Service Registry (Self Registration Pattern).
  2. When the Service Consumer wants to request for a particular service, the service discovery mechanism will query the Service Registry to obtain a list of available Service Provider instance locations.
  3. The location list will be sent back to the Service Consumer after fetching it from the Service Registry.
  4. Finally, the Service Consumer will route the service request to one of the Service Provider instances.

Self Registration Pattern

In the Self Registration Pattern, a service instance registers itself with the Service Registry (See Figure 01 — Step 01). This happens with the Service Registry on startup and unregistered on shutdown.

The service instance may invoke Service Registry’s registration API to register its network location. In addition to that, Service Registry invokes an API to the Service Provider instance for a health check.

Pattern 2: Server-Side Discovery Pattern

Instead of a Service Client querying the Service Registry, it makes a request to a DNS name, which resolves to a Platform Request Router that queries the Service Registry and Load Balances requests.

The key benefit of this pattern is, unlike the client-side discovery pattern, all aspects of service discovery are entirely handled by the deployment platform. This is a major advantage and a hassle-free approach for any development party. Deployment platforms such as Kubernetes (K8) has a built-in Service Registry and a service discovery mechanism to cover the Server Side Discovery Pattern.

The only limitation of this approach is, that you are a bit coupled to the deployment platform that you are using for the Service Registry. For example, if you using Kubernetes as the deployment platform, you are basically coupled to Kubernetes for the Service Discovery. If your other service components are also running on Kubernetes this will not be an issue. However, nowadays most of the deployments happen on cloud native Kubernetes containerized environments, this is not a major limitation.

Figure 02 — Server Side Discovery Pattern

Multiple steps are involved in this.

  1. Service instances are registered with the Service Registry by the registrar (Third Party Registration Pattern).
  2. The Service Client makes a request to the service network location from the Router and the Router queries the request from the Service Registry
  3. Then the Router load balances the request from the available Service Provider instances.

Third-Party Registration Pattern

In the third-party registration (See Figure 02 — Step 02), instead of a self-registration with the service registry, a third party called the registrar handles the registration.

References

  1. Client Side Discovery Pattern: https://microservices.io/patterns/client-side-discovery.html
  2. Server Side Discovery Pattern: https://microservices.io/patterns/server-side-discovery.html
  3. Spring Cloud Hystrix: https://howtodoinjava.com/spring-cloud/spring-hystrix-circuit-breaker-tutorial/
  4. Kubernetes Service Discovery: https://www.densify.com/kubernetes-autoscaling/kubernetes-service-discovery/
  5. https://www.baeldung.com/cs/service-discovery-microservices

--

--

Crishantha Nanayakkara
Cloud Native Daily

Enterprise Architect, Consultant @ FAO (UN), Former CTO, ICTA Sri Lanka