Search anything:

Application layer with Microservices and Service Discovery

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

This article elaborates the working of service discovery of microservices in the application layer along with the complete idea of Application layer with Microservices and Service Discovery.

Table of content:

  1. What is an Application Layer?
  2. Some functions of the Application layer
  3. What are Microservices?
  4. What is Service Discovery?
  5. Why Use Service Discovery?
  6. DNS Approach Implementation of Service Discovery
  7. Limitations of DNS approach to service discovery
  8. A Simple Implementation of Service Discovery Using ZooKeeper

This is an important topic in System Design. We will dive into it now.

What is an Application Layer?

The seventh layer in the OSI model is the application layer.The application layer directly interacts with applications and provides common web application services such as FTP,DNS, HTTP, SMTP and TELNET.


When you are able to separate the web layer from the the application layer, you are able to scale easily and also make the necessary changes on both layers independently. The single responsibility principle which states that a class should have one single responsibility and no more than one can be applied to services that occur on the application layer. This principle helps teams working on small services work together, plan more, and grow rapidly. The single responsiblity principle helps reduce coupling, increases simplicity and makes it easier to work.

Asynchronous processor also known as worker helps enable asynchronism in the application layer. For example, a consumer application processing events from Oracle Event Hub Cloud Service topics. Worker applications can also be used to invoke external applications and APIs.

Some functions of the Application layer

Some functions of the Application layer are:

  • It helps develop network based applications.
  • The application layer provides basis for email forwarding and facilities for sotrage.
  • It also allows a user to retrieve, manage and access files in a remote computer.

What are Microservices?


Microservices are cloud nature arhitectural approach in which a single application is composed of many independently deployable smaller services. It is different from the traditional monolithic architecture where all the business logic of the application is kept at one place. Microservices enables agile development and easy delivery of complex enterprise applications. Microservices are mostly preferred to the traditional monolithich architecture because the use of microservices makes it easier to manage and scale large code bases. The following are the major characteristics of microservives:

  • Microservices usually have their own tech stack with their own database and data management model.
  • Microservices communicate with one another over a REST API, event streaming and message brokers.

What is Service Discovery?

Service discovery is the way applications are able to locate microservices and vice versa on a network.

Why Use Service Discovery?

When you are writing some code that has to invoke a service that has a REST API, your code needs to know the network location of the service in order for a successful request to be made. In contrast, running applications on physical hardwares do not need service discovery since it's easier to locate the service instance.

However, in cloud based micorservice applications, it's difficult to locate the network location of service instance. This is because service instances in microservices have dynamically assigned network location due to autoscaling, failures and upgrades. Thus, the client must have an accurate service discovery mechanism.

There are two models of service discorvery:

  1. Client Side Service Discovery:


In client side discovery, services who want to be discovered registers on the discovery server. Now, the discovery server knows the address of all the registered services. When the client makes a request for a particular service say Microservice 3, the discovery server provides the URL of the requested service to the client. From here, the client makes a direct API call Microservice 3.

  1. Server Side Discovery


All microservices who want to be discovered registers on the discovery server just as in client side discovery. The client makes a request to the discovery server for a service say, microservice 1. The discovery server then forwards the request to microservice 1 without the client making a direct contact with the microservice.

DNS Approach Implementation of Service Discovery

In this implementation, the client uses DNS libraries for service discovery in the DNS approach. Each microservice recieves an entry in a DNS zone file, and does a does a DNS lookup to find a microservices. In some cases, a proxy such as NGINX which can periodically check the status of DNS for service discovery can be used when a microservice is configured accordingly. This approach works with any language and requires minimal code changes.

Limitations of DNS approach to service discovery

  • The DNS does not provide a real time view. Thus, adjusting TTLs is insufficient when different clients have different caching semantics.
  • Since the addition and removal of zone files are expensive, there is an operation overhead of managing zone files.
  • There is also the need to add additional infrastructure for resilience and health check which ends up making the DNS approach complex.

A Simple Implementation of Service Discovery Using ZooKeeper

ZooKeeper is an Apache project which provides a distributed consistent hierarchical configuration store.It is can be used for maintaining configuration information, naming, providing distributed synchronization, and providing group services. All of these services are used in some form or another by distributed applications. Zookeper is one of the several ways to register and query services. Other applcations used for this same purpose include etcd, consul, Kubernetes, and Netflix Eureka.

Note: the code below is Implemented in Java.

Service Registration

String znode = "/services/" + name;

if (curatorFramework.checkExists().forPath(znode) == null) {

String znodePath = curatorFramework
        .forPath(znode+"/_", uri.getBytes());

uriToZnodePath.put(uri, znodePath);

Service Discovery

String znode = "/services/" + name;

List<String> uris = curatorFramework.getChildren().forPath(znode);
return new String(curatorFramework.getData().forPath(ZKPaths.makePath(znode, uris.get(0))));

With this article at OpenGenus, you must have the complete idea of Application layer with Microservices and Service Discovery.

Application layer with Microservices and Service Discovery
Share this