Sunday, November 25, 2018

Microservices-2 Introduction (SOA vs Microservices Architecture)






Microservices-1 Introduction (Monolithic vs Microservices Architecture)

A service-oriented architecture (SOA) is a style of software design where services are provided to the other components by application components, through a communication protocol over a network. The basic principles of service-oriented architecture are independent of vendors, products and technologies. A service is a discrete unit of functionality that can be accessed remotely and acted upon and updated independently, such as retrieving a credit card statement online. The communication can involve either simple data passing or it could involve two or more services coordinating connecting services to each other.

There are mainly two roles in SOA, a service provider and a service consumer. Below is the basic SOA architecture.





SOA is an architecture framework, it’s more generic. SOA is technology independent. We can implement SOA architecture using Webservices.

An enterprise service bus (ESB) implements a communication system between mutually interacting software applications in a SOA. It is an architecture with set of rules for integrating various applications together over a bus-like infrastructure. In this approach we integrate different applications by putting a communication bus between them. This enables applications to talk to the bus. 




ESB decouples systems from each other, allowing them to communicate without dependency on or knowledge of other systems on the bus. The concept of ESB was born out of the need to move away from point-to-point integration, which is difficult to manage.


Advantages of SOA:

1. Flexibility: SOA is quite flexible. Various applications can be developed in different technologies. Since applications are independent of each other there's flexibility to upgrade the technology stack versions. Also, we can incrementally adopt a newer technology without much difficulty.
2. Reliability: SOA services are complete and self-contained programs. This makes it easy for testing, debugging or any form of maintenance.

3. Development speed: Development is fast in SOA. Since the volume of code is quite less as compared to monolithic architecture it’s not difficult for new team members to understand and modify the code. Code quality is maintained well. These factors increase developer’s productivity.

4. Building complex applications: Services are re-usable and so it’s not difficult to build complex applications. Because the services are complete and self-contained applications, they are characterized by their re-usability. Services are re-used in different applications irrespective of the other interacting components in that system.

5. Scalability: The ability of services to be successfully run on different servers within an environment increases the scalability and availability of the service.
Continuous deployment: Agility is possible with SOA to a certain extent.

6. Continuous deployment: Agility is possible with SOA to a certain extent.


So, we can see that most of the features of SOA and Microservices architecture are similar. Both are different from a traditional, monolithic architecture where every service has its own responsibility. Both SOA and Microservices architecture benefit from a certain level of decoupling.  Having said that, there are differences between them which are listed below:


Differences between SOA and Microservices architecture: 

1. Scope and size: The fundamental difference between SOA and microservice architecture lies in the size and scope. In simple words SOA has an enterprise scope, while the microservices architecture has an application scope. The prefix "micro" in microservices refers to the granularity of the internal components, meaning they have to be significantly smaller than what SOA tends to be. Services within microservices generally have a single purpose and they do that one thing really well. On the other hand in SOA, services usually include much more business functionality, and they are often implemented as complete subsystems.




2. Re-usability vs decoupling (Bounded context): In SOA the prime focus is on re-usability, while Microservice architecture is more focused on decoupling. At an enterprise level arriving at certain level of re-usability is very essential. In microservices architecture, if some services are reused at runtime from various services, it results in dependencies that reduce agility and resilience. In Microservices architecture components generally prefer to reuse by duplicating code to improve decoupling. SOA encourages sharing of components, whereas microservices try to minimize on sharing through "bounded context". A bounded context refers to the coupling of a component including data as a single unit with minimal dependencies. SOA relies on multiple services to fulfill a business request, so systems built on SOA are likely to be slower than microservices.
3. Development: In both SOA and Microservice architectures, services can be developed in different programming languages and technologies, which brings flexibility with the technology diversity into the development team. The development is organized within various teams. In case of SOA, each team needs to know about the common communication mechanism. But with microservices architecture, the services can operate and be deployed independently of other services. So, it is easier to deploy new versions of microservices.

4. Communication: For communication among various services SOA uses Enterprise Service Bus (ESB). SOA supports multiple message protocols. The ESB could become a single point of failure which could impact the entire system. Since all the services communicate through the ESB, in case any of the services slows down, it could clog up the ESB with requests for that service. But in case of Microservice architecture there's no concept of ESB. Microservices are much better in error tolerance. If one microservice is down or clogged, then only that microservice will be affected, and all the other microservices will continue to handle requests regularly. Microservices usually use lightweight protocols such as HTTP, REST or Thrift APIs. ESB is a middleware. So, SOA uses middleware for communication, while microservices use API Gateway.

5. Interoperability: SOA supports multiple heterogeneous protocols through its messaging middleware component. Microservices architecture however, attempts to simplify it by reducing the number of choices for integration.

6. Scalability: In traditional SOA we would buy an ESB, we deploy the ESB, we deploy the individual services on that ESB, but if we need more scalability for an individual service we need to scale up the entire ESB infrastructure, we can’t just scale up individual services. In microservices architecture we can deploy individual services independently and scale up individual services.

7. Data storage: SOA services share the data storage. In Microservice architecture, each microservice is recommended to have an independent data storage.


Why organizations are moving towards Microservices architecture?

1. Microservices architecture is basically an evolution of SOA. Same principle still holds. Microservices architecture solves a lot of the problems that were there in SOA, like configuration issues, scalability issues. With microservices we can scale up features individually.
2. Agile and scrum is the major factor. We need to deliver things quicker.

3. DevOpps is the second factor. There’s a lot of deployments happen these days, so we need a good CICD pipeline for deployment and not rely on old traditional methods. The more we breakdown our application into smaller components the easier it is to push out these individual changes as we don’t have to ship that entire application with that feature.


Having said that, we just can’t simply throw the baby out of the bathtub, and upgrade SOA applications to Microservices. There are lot of really good Java enterprise technologies that are available to the SOA world, like transaction management, messaging, Object relational mapping to the DB, business rule management. All these things need to find their place in the microservices world. Ultimately applications still need to do same kind of operations like read/write from a DB, participate in transactions, and things like that.

In my next post on Microservices series I will write about the various challenges in implementing microservices architecture, we will learn about Spring Cloud and how Spring Cloud helps to overcome the challenges of Microservices architecture.

Sunday, October 21, 2018

Microservices-1 Introduction (Monolithic vs Microservices Architecture)






Recently, there has been a lot of fuss about Microservices. There are a lot of discussions happening about Microservices in almost all the IT companies. Microservices architecture can be easily understood when we compare it with traditional Monolithic architecture.

Almost every enterprise application has a similar kind of layered architecture:
  1. Presentation: The user interface
  2. Business logic: The application’s internal business logic
  3. Database access: Almost all the application needs to access DB, either SQL or NoSQL.
  4. Application integration: Quite often application needs integration with other applications. This is usually achieved via webservice calls (SOAP or REST), or via messaging. 

Even though applications have quite clear logically modular architecture, but usually most of the application is packaged and deployed as a monolith. There’s actually some advantages of doing this.




Advantages of Monolithic Architecture:
  1. Development is quite simple
  2. Testing is very simple. Just launch the application and start end to end testing. We can also do test automation using Selenium without any difficulty.
  3. Deploying the monolithic application is straight forward, just copy the packaged application to the server.
  4. Scalability is simple. We just need to have new instance of the monolithic application and ask loadbalancer to distribute load to the new instance as well. But as the monolithic application grows in size scalability becomes a serious issue.

The Monolithic architecture had really worked successfully for many decades. In fact, many of the most successful and large applications initially developed and deployed as monolith. Many large scale enterprise applications of big companies still being deployed as monolith. But with the changing market, and with the emergence of new technologies there has been a paradigm shift in the way IT industries used to work. There are some serious issues with Monolithic architecture which most of the companies are trying to address these days.


Disadvantages of Monolithic Architecture:

  1. Flexibility: Monolithic architecture is not flexible. We can’t use different technologies. The technology stack is being decided at the start, and the same to be followed all throughout. Once the development gets mature sometimes it become difficult to even upgrade the agreed technology stack versions, forget about incrementally adopt a newer technology.
  2. Reliability: It's not reliable. If one feature goes down, the entire application might go down.
  3. Development speed: Development is really slow in Monolithic architecture. Difficult for new team members to understand and modify the code of large monolithic application. Code quality declines over the time. With the increasing code base the IDE too much overloaded and becomes slower. The larger the application the longer it takes to start up. All these factors have a huge impact on developer’s productivity. 
  4. Building complex applications: Difficult to build complex application, as limitation in technologies.
  5. Scalability: Monolithic applications are difficult to scale up once it gets larger. We can create new instances of the monolith application and ask loadbalancer to distribute traffic to the new instances as well. But Monolithic architecture can’t scale with an increasing load. Each copy of application instance will access all of the data, which makes caching less effective and increases memory consumption and I/O traffic. Also, different application components have different resource requirements - one might be CPU intensive while another might memory intensive. With a monolithic architecture we cannot scale each component independently.
  6. Continuous deployment:  Continuous deployment are extremely difficult. Large monolithic applications are actually obstacle to frequent deployments. In order to update one component we have to redeploy the entire application


Because of all the above drawbacks of Monolithic applications Microservice architecture is becoming more and more popular day by day. So what is Microservice based architecture?

Well in short, the Microservice architectural style is an approach to developing a single application as a suite of small services each running in its own process and communicating with lightweight mechanisms, usually RESTful webservice or via messaging. These services are built around business capabilities and independently deployable by fully automated deployment machinery. There’s a bare minimum of centralized management of these services, which may be developed in different programming languages and use different data storage technologies. Microservices are small well-chosen independent deployable unit, which are cloud enabled.



Microservices architecture tackles all the drawbacks of Monolithic architecture:

  1.  Flexibility: Microservices architecture is quite flexible. Different microservices can be developed in different technologies. Since microservice is smaller, code base is quite less, so it’s not that difficult to upgrade the technology stack versions. Also, we can incrementally adopt a newer technology without much difficulty.
  2. Reliability: Microservices architecture can be very reliable. If one feature goes down, the entire application doesn’t go down. We can fix the issue in the corresponding microservice and immediately deploy it.
  3. Development speed: Development is pretty fast in Microservices architecture. Since the volume of code is quite less for a microservice it’s not difficult for new team members to understand and modify the code. They become productive right from the start. Code quality is maintained well. IDE is much faster. The microservice takes very less time to start up. All these factors considerably increases developer’s productivity. 
  4. Building complex applications: With Microservices architecture it's easy to build complex application. If the features of the application are analyzed properly, we can break it down into independent components which can be deployed independently. Then even the independent components can be further broken down into small independent tasks which can be deployed independently as a microservice. Deciding the boundaries of a microservice can quite challenging. It’s actually an evolutionary process. But once we decided on a microservice it’s easy to develop as there’s no limitation in technologies.
  5. Scalability: Scalability is a major advantage in Microservices architecture. Each microservice can be scaled individually. Since individual microservices are much smaller in size, caching becomes very effective.
  6. Continuous deployment: Continuous deployment becomes easier. In order to update one component we have to redeploy only that particular microservice.


As I mentioned earlier, Microservices architecture can be easily understood when we compare it with traditional Monolithic architecture. But prior to microservices there’s already similar kind of architecture available. Yes, I’m talking about SOA (Service Oriented Architecture). SOA has been there since last 2 decades. If you have already worked on SOA and are familiar with its concepts, it could be quite confusing to understand the differences between SOA and Microservices architecture. In fact, the two have more things in common than differences. In my next post on Microservices I will write about SOA, similarities among SOA and Microservices architecture, and of course the differences between them. 

Microservices-2 Introduction (SOA vs Microservices Architecture)

Sunday, September 2, 2018

Queen’s Attack



Given a square chess board with one queen and a number of obstacles placed on it.

Problem : Determine how many squares the queen can attack?


Given conditions:
  • The Queen can be standing at any square of given n x n chess board.
  • The rows are numbered from 1 to n, going from bottom to top. The columns are numbered from 1 to n, going from left to right.
  • Each square is referenced by a tuple, (r, c), describing the row, r, and columns, c, where the square is located.
  • Queen’s position is (qR, qC). In a single move the queen can attack any square in any of the 8 directions (left, right, up, down, and the 4 diagonals).

In the diagram below the green circles denote all the cells and the queen can attack from (4,4):



 


  • There can be obstacles on the chessboard, each preventing the queen from attacking any square beyond it on that path. Number of obstacles is determined by the value of k. For example, an obstacle at location (3,5) in the diagram above prevents the queen from attacking cells (3,5), (2,6), and (1,7): 


   
  • Given the queen’s position and all the locations of the obstacles, find the number of squares the queen can attack from her position (qR, qC).


Examples-1:






In the above scenario, it’s a 4 x 4 chessboard, queen’s position is (4,4), and there are no obstacles (k=0).

The answer is 9. The queen can attack 9 squares.





In the above scenario, it’s a 5 x 5 chessboard, the queen’s position is (4,3), with k=3 obstacles. 

Position of the obstacles are as below.

{
    {4, 2},
    {5, 5},
    {2, 3}
}

The answer is 10. The queen can attack 10 squares.


Solution:

I came across this problem in RackerRank. Initially I solved the problem by creating a “n x n” int array, with all elements 0. Queen’s position (qR, qC), I set the value to 1. For all the obstacle cells I set the value to -1. I wrote the logic, calculation was perfect. But my program passed only few test cases. There were many test cases with much bigger value of n. Let’s say for n=100000, if we create n x n matrix, it gives out of memory in my local computer. And it RackerRank it simply says “Test cases failed”. So, I tried different approach. The below algorithm worked, and this time all the test cases passed.
  1. Create 8 Lists for 8 different directions the Queen can attack.
  2. Iterate through all the obstacles. For each, identify if it falls in the path of any of the 8 directions. If yes, then add the points in the appropriate List.
  3. In all the 8 Lists, find the cells which are closest to the queen.
  4. Calculate all possible cells which the queen can attack.

You can check the code or download it from my GitHub account. It’s a java maven project.

https://github.com/jahidakhtargit/problem-solving





Sunday, August 26, 2018

IT Industry in India



A series of events occurred about couple of centuries ago that brought huge changes in the way people lived. That was the time when big machines and factories were built. Adoption of manufactured goods increased exponentially. It started the world's first Industrial revolution in England. Before the Industrial revolution the way people lived was entirely different, no electricity, no Television, no movies, no automobiles, no railways, and no machines. People didn't travel much, mostly they lived their entire life in their own village. 90% people lived in rural areas. The world was very quiet before the industrial revolution. Most of the people were poor, and only a tiny percentage of them were highly rich. There was no middle class people as such. After the industrial revolution there were big factories all over Europe and America. Thousands of workers started working, more and more people started living in urban areas, cities started to grow fast.



Basically there have been three basic industrial revolutions. The first was between 1750 and 1830 which was the rise of the steam engines, the locomotives, the telegraph and the textile industry. The second phase happened during 1870 to 1900, when the world witnessed the rise of electricity, the internal combustion engine, hydro power, plastics, chemicals and similar things that came on that era. The third phase of industrial revolution is the recent one which is the rise of computers, internet, and mobile phones. India missed out the initial two phases of the industrial revolution as it was not an independent country that time. But the nation has utilised the third phase i.e. the information technology revolution to its full potential.

According to Forbes India the seed for the information technology (IT) revolution was planted during Rajiv Gandhi’s time. During the 80s, USA and some other European countries had developed super computers, which were critical for developing satellites and nuclear weapons. These countries refused to transfer the knowledge of creating super computers to India, fearing India might use it to design missiles and warplanes rather than weather forecast. The supercomputer effort in India began in the late 1980s, when the US stopped the export of a Cray supercomputer. Without any other alternative India set up Centre for Development of Advanced Computing (C-DAC) in March 1988 with an objective to develop an indigenous supercomputer to meet high-speed computational demands in solving scientific and other developmental problems. To lead the project, PM Rajiv Gandhi turned to Vijay Pandurang Bhatkar. Just within three years in 1991, C-DAC rolled out India’s first indigenous supercomputer: PARAM 8000. For the first time ever, a developing country achieved such success in advanced computer development. Vijay Bhatkar is regarded as the Father of Indian Supercomputers. In 2015, he was honoured with Padma Bhushan for his extra ordinary contribution in the field of science and technology in India. Vijay Bhatkar once said, "Great nations are not built on borrowed technology".




Information technology has redefined human life in a radically new way. Computers have been with us for more than half a century now, starting from basic transistor driven devices, they have evolved through many generations in hi-tech and sophisticated equipment. Such machines connected the entire world. The IT revolution has touched every aspect of our life, be it banking, telecommunication, healthcare, transportation, hotel, real estate, and literally everything in today's word are embedded with information technology. According to Wikipedia information technology in India is an industry consisting of two major components: IT services and business process outsourcing (BPO). The sector has increased its contribution to India's GDP from 1.2% in 1998 to 7.5% in 2012. According to NASSCOM, the sector aggregated revenues of US$160 billion in 2017, with export revenue standing at US$99 billion and domestic revenue at US$48 billion, growing by over 13%. USA accounts for more than 60 per cent of Indian IT exports.

Information technology has connected the entire world, as if the world is like a global village, and it created an opportunity for lot of innovations. In the last decade we have seen the rise of social media, and how it has influenced human emotions. These days people can't imagine life without Facebook, Twitter, WhatsApp or LinkedIn to mention a few of social media. Everything is becoming online, be it shopping, food, movie tickets, Railways, flight ticket booking, or Government services, everything is becoming online. Information technology is really boosted with the mobile phone revolution. In today's world almost every human being carries the entire world with him/her in the form of a mobile phone with tremendous computing power. With the introduction of Global Positioning System (GPS) in the market, which initially started in the US for its military use, lot of innovations happened in the field. The mobile applications really changed the way we live. It simply gave a new dimension to the IT revolution. These days one doesn't need a computer or a laptop for personal IT related stuffs, everything can be done through smart phone. There are Apps for almost anything, you just browse it, and it would be available in seconds.

With such rapid development in the world of information technology, there's just too much of data flowing around. According to statistics an average internet user generates about a GB of data per day. Nowadays, even start-up companies have to deal with terabytes of data right from the very first year of business. Such huge volumes of data created doors for new technologies and innovations. Big Data, Data Analytics, Machine Learning, Artificial Intelligence. These modern technologies makes use of data to analyse them, and then create intelligence in the software to enhance their business. The world, and definitely information technology is aggressively moving towards automation, AI, Big Data, Data Analytics, Machine Learning, and IOT (Internet of Things) are the immediate future. 



Pretty soon all the cars, buses and trucks are going to be driverless. After a couple of decades quite possibly there wouldn't be any farmers working on the fields, the Robots would simply take over. There won't be much of human Military in future, it would be Machine Army. There's rapid development happening in the field of space and rocket science as well. With Elon Musk's SpaceX's very successful reusable launch system development program, the cost of space travel has reduced considerably. In the next few decades the present most popular form of long distance travel (aeroplane) would become outdated. Soon humans would start travelling by rocket, and one would be able to reach any city from any other part of the world in just 30 minutes.

Information technology has really changed the world, and it’s evolving at a massive pace. The future is quite uncertain though, with Machines taking over almost 80 percent of the human jobs. But it happened in the past as well. Automation results in mass job loss, but it creates opportunity for new innovations, and new kind of job market. It would be really interesting to see where this information technology would reach in next 30 years.



This article was first published in "Uruli", a biannual multilingual magazine published by Assam Association Bangalore.

Monday, August 20, 2018

Understanding Recursion with the Factorial Example



Programming languages usually have their own internal Stack. In case of Java it’s the Java Stack. When any new thread is launched, the JVM creates a new Java stack for the particular thread. It stores the thread’s state in discrete frames. The JVM performs PUSH and POP operations on the Java stack.

When a thread invokes a method, JVM creates a new frame and pushes it on to the Java stack. The newly pushed frame becomes the current frame. As the method executes, it uses the frame to store parameters, local variables, intermediate computations, and other data.

Recursion is the process in which a function calls itself. The function is called as recursive function. A recursive function performs a bigger task part by part by calling itself to perform the subtasks. Every recursive function has a base case, it’s the point when the function is able to return some value without calling itself. Below is the format of a recursive function.

if(Base case) {
            return Base Case Value;
} else {
            return (Compute some work followed by recursive a call)
}

Code to find the factorial of an integer:

public static int factorial(int n) {
            if(n==0 || n==1){
                        return 1;
            }
            return n * factorial(n-1);
}

Let’s try to visualise the flow of execution of the above piece of code. Factorial of 5 is 120.
Factorial of 5 = 5 * 4 * 3 * 2 * 1 = 120


When the factorial method is called with 5, JVM creates a frame and pushes it onto the Java stack.



Now the code returns '5 * factorial(4)'. Since, once again there’s a method call (same factorial method), JVM creates a new frame and pushes it on the Java stack.



Similarly, the method keeps calling itself till it reaches the base case.




Once base case is reached it returns without making any recursive call. In our case when factorial is called with 1, it simply returns 1. JVM pops the current frame, the result of the subtask(1) is required by the next frame for computation.



Now, the top most frame is again popped. The result is computed to be 2. This result is used to compute the next frame.



Now the current stack frame computes the result (3*2 = 6), and JVM pops the frame from the Java stack.



Now, the current frame computes the subtask result (4 * 6 = 24). JVM pops the frame from the Java stack.



Now, there’s only one frame on the Java stack. It computes the final result (5 * 24 = 120). 



JVM pops the frame, and the recursive function completes.

We will try out some more recursive functions. Hope you've enjoyed the post :)