Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

What are Microservices

4.19/5 (24 votes)
3 Apr 2017CPOL5 min read 47.8K  
Breaking down of a large project into small executable modules is Microservices.

Introduction

Today world is talking about Microservices, so I thought why not write an article on my understanding on Microservices.

So far I felt this would be appropriate mostly for environment that are cloud based and can see huge movements in that direction.

What are Microservices? 

Breaking down of a large project into small executable modules is Microservices, where each module is independent and can be executed without depending on other modules. Modules will communicate with each other via API’s.

What we do in Monolithic App’s:

We create a solution which typically consists of a Presentation Layer, Application Layer, a Middle Layer, and Data Access Layer. To run this we will create a package and deploy, which will be responsible for all of them. Now since we have clubbed them under one umbrella that is package, they become Monolithic even though we have different layers with their responsibilities.

So problem is although we have separation but still at the end we combined them under one umbrella and made them dependent. If any one part fails, the whole application suffers.

Take a real world example:

We are making pizza, what we need for that is vegies, Cheese, Pizza base, sauces.

We arranged all this items from different places and now we are ready to make Pizza. We will apply sauces to the base; add vegies and finally cheese and ready to bake. Now imagine Veggies supplied to pizza are rotten. What would be end result? Your whole pizza is decay, although we have material from different places but since they are combined and now it cannot be supplied, as not good for eating. Although out of 4 ingredients only one was rotten, because of which pizza became useless. This is case with Monolithic approach.

Few drawbacks of Monolithic app

1.     Change in technology as everything is under one umbrella, so tough to think.

2.     Scalability is challenging.

3.     Failing of any one component can lead complete application down.

4.     Lot of dependency on interconnected module, because of which developer has to wait.

With this let’s now talk about Microservices:

When any application is created we create various methods to achieve functionality of it. Now imagine if each method is an independent service and is individual deployed without waiting for other methods to complete.

This will definitely improve developers time as dependency on other modules is cut off and the method should independently be tested. Developer has to focus only on the functionality of their method. Anytime they can make any change to the code and redeploy without having any impact on other modules.

Here is the biggest advantage of Microservices that is scalability.

If any service is bottleneck for the application, it can be taken care separately without impact other services which was tough and lengthy process in Monolithic apps.

Performance of each service could be monitored and based on the result tuning can be performed on individual if required.

Real world example:

Let’s take same example of Pizza, now we have all items packed in separate unit and tested. While testing veggies, we already knew that they are rotten. So we can skip them and add remaining things to get pizza ready.

Advantages of Microservices are:

  1. Easy to understand, as developer has to focus on one service at a time. Even if a new developer is introduced, focus will be on single service functionality.
  2. Services could be mix of technology, so dependency on technology or up-gradation to new technology becomes easier.
  3. Since code is broken into small pieces maintainability increases.
  4. Monitoring becomes simple, as each service as its own process and deployment.
  5. Data can be saved by individual services, so can use database as per individuals design logic. Ex one service using mongoDB, other service is on MYSQL.
  6. Deployment to various environments from Dev to QA and from QA to UAT will be just one click.

Now we have four services under one package and we observe that service-2 is performing slow, in that case we don’t have to bring entire package down. We can just tweak service-2 and deploy as they all are deployed independently.

Drawbacks of Microservices:

  1. Code can be redundant, as each service has to perform individually, so inorder to achieve functionality many times same peace of code will be repeated.
  2. Testing will be time consuming, as each service has to be tested individually and then complete functionality with app talking to each other should be tested.
  3. Deployment of final solution will be trick as all individually hosted service should be able to communicate with each other, which will not be that straightforward.

SImple UI to show difference in packing:

Deployment of Microservices:

Each Microservices based application will contain multiple services which may be written in different languages and frameworks. Each service must have its own database, which means each service act as an application having its own deployment. Service instances depend upon demand and have to be isolated from one another. Each instance need proper CPU and memory. Moreover deploying of the application should be cost effective and reliable.

Following are the patterns that can be used for deploying Microservices:

  1.   Multiple service instances per host
  2.   Single service instance per host
  3.   Service instance per VM
  4.   Service instance per Container

Best way to deploy Microservices is deploy under containers, as they serves the purpose of isolation and dedicated hardware allocation.

Conclusion:

Microservices are growing very fast and becoming a boom in the market. Every new technology has its advantages and disadvantages, but based on use we can decide what to choose.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)