Tuesday, 26 March 2019

Microservices


Microservices is a solution of monolithic architecture, there are several issue with monolithic architecture. In monolithic design system were very complex, if a failure occurs whole application will down.  Need a big setup to maintain and deploy a big application, all the issues of monolithic design are resolved with micro service architecture.
If we talk about solution of monolithic design so it’s a divide and conquer approach
Divide a big application into independent small application and deploy and make it accessible for you app, so for a big application there will be several micro services, if one service is down so whole system will not down, only small part of application will be impacted.
Micros services can developed and deployed independently. The only issue with the segregation of functionally and logic so that each application can run independent in a loosely coupled system.


Advantages of microservices-

Below are the major advantages of micro services-
1.      Indpendent development
2.      Independent deployment
3.      Technology independent – UI application will get the data from an API, no matter what technology or plateform under that API
4.      Plateform independent
5.      Better in application failure- failure in one service will not impact whole application.


Service Oriented Architecture (SOA)

SOA is a raw form of micro service solution, in a basic language we can say micro services are just a better implementation of SOA.
SOA is a collection of services and these services interact with each other over the network either with data or communication protocal.


Microservices

Microserviecs are evolution of SOA. In microserviec each function can be split to one services and these services will be independent and full stack in nature.
Functional decomposition plays and important role in a big application, it make application more reliable, flexible and scalable.


Design approach for microservices-

We can follow multiple design practice for developing micro services as per application and business needs, few of them are as follows-

Chained pattern - micro services are chained in such a way that the output of one service will be the input for other service. This is very useful in secured and firewalled protected system, in this way internal service will not interact with web ui or application only few ui service will interact with the application, other will work under secure system.

Shared resource –
This is major benefit of misroservices, shared resources can be easily used with different micro services. Suppose one independent application provide the livestock rate so multiple application can communicate with this service and get the data.


Stack Memory vs Heap Memory


Stack Memory –
Stack memory can be considered as local/private memory, all the local variables and functions are defined in stack memory.
When stack memory is full, Java runtime throws java.lang.StackOverFlowErrorwhereas

Heap Memory –
Heap memory is used to store Objects and JRE classes. This is publically available for all running the threads means heap memory is globally accessible in the application.
When heap memory is full, it throws java.lang.OutOfMemoryError: Java Heap Space error


Heap and Stack Memory in Java Program
public class MainTest {

private int classLvlVar = 0; // class level variable heap memory
      
       public static void main(String[] args) { // stack memory
              int localVar = 1; //stack memory
              // instance is created in heap memory while reference variable maintest will be in stack memory
              // which will refer to heap instance.
              MainTest mainTest =  new MainTest();
             
              System.out.println(localVar); // stack memory
              mainTest.getVar(); //stack memeory
             
       }
      
       private int getVar() {
              return classLvlVar//stack memory
       }
}


If there is no memory left in the stack for storing function call or local variable, JVM will throw java.lang.StackOverFlowError, while if there is no more heap space for creating an object, JVM will throw java.lang.OutOfMemoryError: Java Heap Space.