Learn Microservice Architecture in simple and easy steps starting from basic to advanced concepts with examples
Have you ever wondered, What is Microservices and how the scaling industries integrate with them while building applications to keep up with their client expectations?
To get an idea of What is Microservices, you have to understand how a monolithic application is decomposed into small tiny micro applications which are packaged and deployed independently. This blog will clear your understanding of how developers use microservices to scale their applications according to their need.
In this article, you will learn about the following:
Why Microservices?What Is Microservices?Features Of Microservice ArchitectureAdvantages Of Microservice ArchitectureBest Practices To Design MicroservicesCompanies Using Microservices##
Now, before I tell you about microservices, let’s see the architecture that prevailed before microservices i.e. the Monolithic Architecture.
In layman terms, you can say that its similar to a big container wherein all the software components of an application are assembled together and tightly packaged.
Listed down are the challenges of Monolithic Architecture:
Figure 1: What Is Microservices – Challenges of Monolithic Architecture
Inflexible – Monolithic applications cannot be built using different technologies Unreliable – Even if one feature of the system does not work, then the entire system does not workUnscalable – Applications cannot be scaled easily since each time the application needs to be updated, the complete system has to be rebuiltBlocks Continous Development – Many features of the applications cannot be built and deployed at the same timeSlow Development – Development in monolithic applications take lot of time to be built since each and every feature has to be built one after the other**Not Fit For Complex Applications – **Features of complex applications have tightly coupled dependencies
The above challenges were the main reasons that led to the evolution of microservices.
Microservices, aka Microservice Architecture, is an architectural style that structures an application as a collection of small autonomous services, modeled around a business domain.
** Figure 2**: What Is Microservices – Microservices Representation
In Microservice Architecture, each service is self-contained and implements a single business capability.
Consider an E-commerce application as a use-case to understand the difference between both of them.
Figure 3: What Is Microservices – Differences Between Monolithic Architecture and Microservices
The main difference we observe in the above diagram is that all the features initially were under a single instance sharing a single database. But then, with microservices, each feature was allotted a different microservice, handling their own data, and performing different functionalities.
Now, let us understand more about microservices by looking at its architecture. Refer the diagram below:
Figure 4: What Is Microservices – Microservice Architecture
Different clients from different devices try to use different services like search, build, configure and other management capabilitiesAll the services are separated based on their domains and functionalities and are further allotted to individual microservicesThese microservices have their own load balancer and execution environment to execute their functionalities & at the same time captures data in their own databasesAll the microservices communicate with each other through a stateless server which is either REST or Message BusMicroservices know their path of communication with the help of **Service Discovery **and perform operational capabilities such as automation, monitoringThen all the functionalities performed by microservices are communicated to clients via API GatewayAll the internal points are connected from the API Gateway. So, anybody who connects to the API Gateway automatically gets connected to the complete system
Now, let us learn more about microservices by looking at its features.
Figure 5: What Is Microservices – Features Of Microservices
Decoupling – Services within a system are largely decoupled. So the application as a whole can be easily built, altered, and scaledComponentization – Microservices are treated as independent components that can be easily replaced and upgradedBusiness Capabilities – Microservices are very simple and focus on a single capability Autonomy – Developers and teams can work independently of each other, thus increasing speedContinous Delivery – Allows frequent releases of software, through systematic automation of software creation, testing, and approval Responsibility – Microservices do not focus on applications as projects. Instead, they treat applications as products for which they are responsible Decentralized Governance – The focus is on using the right tool for the right job. That means there is no standardized pattern or any technology pattern. Developers have the freedom to choose the best useful tools to solve their problems Agility – Microservices support agile development. Any new feature can be quickly developed and discarded again##
Figure 6: What Is Microservices – Advantages Of Microservices
Independent Development – All microservices can be easily developed based on their individual functionalityIndependent Deployment – Based on their services, they can be individually deployed in any application Fault Isolation – Even if one service of the application does not work, the system still continues to functionMixed Technology Stack – Different languages and technologies can be used to build different services of the same applicationGranular Scaling – Individual components can scale as per need, there is no need to scale all components together##
In today’s world, complexity has managed to creep into products. Microservice architecture promises to keep teams scaling and function better.
The following are the best practices to design microservices:
Figure 7: What Is Microservices – Best Practices To Design Microservices
Now, let us look at a use-case to get a better understanding of microservices.
Let’s take a classic use case of a shopping cart application.
When you open a shopping cart application, all you see is just a website. But, behind the scenes, the shopping cart application has a service for accepting payments, a service for customer services and so on.
Assume that developers of this application have created it in a monolithic framework.Refer to the diagram below:
Figure 8: What Is Microservices – Monolithic Framework Of Shopping Cart Application
So, all the features are put together in a single code base and are under a single underlying database.
Now, let’s suppose that there is a new brand coming up in the market and developers want to put all the details of the upcoming brand in this application.
Then, they not only have to rework on the service for new labels, but they also have to reframe the complete system and deploy it accordingly.
To avoid such challenges developers of this application decided to shift their application from a monolithic architecture to microservices.Refer to the diagram below to understand the microservices architecture of shopping cart application
Figure 9: What Is Microservices – Microservice Architecture Of Shopping Cart Application
This means that developers don’t create a web microservice, a logic microservice, or a database microservice. Instead, they create separate microservices for search, recommendations, customer services and so on.
This type of architecture for the application not only helps the developers to overcome all the challenges faced with the previous architecture but also helps the shopping cart application to be built, deployed, and scale up easily.
There is a long list of companies using Microservices to build applications, these are just to name a few:
Figure 10: What Is Microservices – Companies Using Microservices
Organizations are quickly moving towards Microservices architecture & hunting for professionals with Microservices Certification. I hope that you have read my previous blog on ***What is Microservices ***that explains the architecture, compares microservices with monolithic and SOA, and also explores when to use microservices with the help of use-cases.
In this Microservices tutorial, the following topics will be covered:
Let us explore the concepts of microservices through a use-case on Mediamore.com.
Mediamore is an entertainment company which provides streaming media and videos online. It consists of various genres of TV Shows in different languages. Like many other companies mediamore started its journey with a monolithic architecture.
Let us now explore the monolithic framework of mediamore.
Figure 11: Monolithic Architecture of Mediamore – Microservices Tutorial
Refer to the above diagram. We can infer that all the features such as the search, user-info, recommendations, video playlist and others are put on a single database using single code.
Now, let me tell you the challenges faced by the developers while using a monolithic framework by using some scenarios.
Scenario 1: Scalability:* Let’s assume that the developers want to update the playlist according to most popular tv shows and also simultaneously want to update all videos to HD quality.*
The developers cannot scale the application simultaneously. New instances of the same application have to be created every time a new feature has to be developed or deployed.
Scenario 2: Agility:* Assume that developers want to make immediate changes in the application.*
The monolithic application can definitely accommodate these changes. But, the problem here is that the developers have to rebuild the code for every small change.
Scenario 3: Hybrid Technologies: Suppose developers of this application are comfortable with various technologies like JAVA, C++,.NET, C#.
Even though they are comfortable with various technologies, they still have to build large and complex applications on a single technology.
Scenario 4: Fault Tolerance:* Let’s suppose that a specific feature is not working in the application.*
The complete system goes down because of this problem. In order to tackle this problem, the application has to be re-built, re-tested and also re-deployed.
So, how did the developers of mediamore overcome these complexities?
Developers thus decided to re-architect their monolithic application into multiple individual deployable components, called as microservices.
Here lies the million dollar question!
Microservices is an architecture wherein all the components of the system are put into individual components, which can be built, deployed, and scaled individually.
Let me explain you with a simple analogy.
You must have seen how bees build their honeycomb by aligning hexagonal wax cells. They initially start with a small section using various materials and continue to build a large beehive out of it. These cells form a pattern resulting in a strong structure which holds together a particular section of the beehive. Here, each cell is independent of the other but it is also correlated with the other cells. This means that damage to one cell does not damage the other cells, so, bees can reconstruct these cells without impacting the complete beehive.
Figure 12: Beehive Representation of Microservices – Microservices Tutorial
Refer to the above diagram. Here, each hexagonal shape represents an individual service component. Similar to the working of bees, each agile team builds an individual service component with the available frameworks and the chosen technology stack. Just as in a beehive, each service component forms a strong microservice architecture to provide better scalability. Also, issues with each service component can be handled individually by the agile team with no or minimal impact on the entire application.
The next question that may come to your mind is how do the different components of microservice architecture work together.
But, before that, let me list down the components of the microservice architecture.
Refer to the below diagram.
Figure 13: Microservices Architecture – Microservices Tutorial
Clients – Different users from various devices send requests. Identity Providers – Authenticates user or clients identities and issues security tokens.API Gateway – Handles client requests.Static Content – Houses all the content of the system.Management – Balances services on nodes and identifies failures.Service Discovery – A guide to find the route of communication between microservices.* Content Delivery Networks – Distributed network of proxy servers and their data centers.
Let me now brief you on how these components work together on mediamore by considering a scenario.
Alice is an avid user of mediamore. She uses mediamore regularly to watch her favorite series online. She recently missed watching an episode of her favorite TV show.
When Alice logs in to the application, she sees the most recommended content on her home page. After some searching, she finally finds her TV Show.
But, what if Alice wants to get her TV Show with a single click.
How will the developers work together to fulfill Alice’s request?
Alice’s request is passed on to the Identity Provider. Identity provider thus authenticates Alice’s request by identifying her as a regular user on mediamore.
These requests are passed to the API Gateway which acts as an entry point for Alice to forward her requests to the appropriate microservices.
Each feature has its own working microservice, handling their own data. These microservices also have their own** load balancers** and **execution environments **to function properly.
Figure 14: Microservices Architecture of Mediamore – Microservices Tutorial
Refer to the diagram below. Each microservice is handled by a small agile team such as content team, video uploading team, most trending team, search team etc.
Figure 15: Division of Teams of Mediamore – Microservices Tutorial
These small teams of developers relate each and every piece of content with the metadata that describes the searched content. Then, metadata is fed into another microservice i.e. the search function which ensures Alice’s search results are captures into the content catalog.
Then, the third microservice i.e. most trending microservice captures the trending content among all the mediamore users according to their geographical locations.
The content from this microservice is what Alice sees when she first logs into mediamore.
These individually deployable microservices are put in specific containers to join the application. Containers are used to deliver the code to the sector where deployment is required.
But before they join the application to work together, they have to find each other to fulfil Alice’s request.
How do these microservices find each other?
Microservices use service discovery which acts as a guide to find the route of communication between each of them. Microservices then communicate with each other via a stateless server i.e. either by HTTP Request/Message Bus.
Figure 16: Communication Between Microservices – Microservices Tutorial
These microservices communicate with each other using an Application Program Interface(API). After the Microservices communicate within themselves, they deploy the static content to a cloud-based storage service that can deliver them directly to the clients via Content Delivery Networks (CDNs).
So, when Alice searches for her TV Show, the search microservice communicates with the content catalog service in API about what is Alice searching for and then these microservices compare the typed words with the metadata they already have.
Figure 17: Representation of how to search operation is performed with the help of API – Microservices Tutorial
Once the teams of developers capture the most typed words by Alice, the analytics team update the code in recommendations microservice and compare Alice’s most viewed content and preferences to popular content among other users in the same geographical region.
This means that the next time Alice logs on to the application, she not only sees the most popular content but also finds a personalized playlist which contains the shows she has previously viewed.
In this way, Alice’s request is fulfilled by the development team in a quick manner as they did not have to build the complete application again and just had to update the code to deploy this new functionality.
So this way microservices invoke parallel environments to satisfy millions of customers with varying interests.
To demonstrate the concepts of microservices, I have created 3 Maven Projects called as Doctor_Microservice_Edureka, Diagnosis_Microservice_Edureka, and Patient_Microservice_Edureka using Spring Boot.
Refer to the snapshot below.
Before you understand how these 3 projects interact with each other. Let me brief you on the files of these projects.
To explain this I will consider the project Patient_Microservice_Edureka and list down its basic files.
Refer to the snapshot below.
Pom.xml – Dependencies are added for the creation of REST services.
Application.java – Identical class in all three projects. Acts as an initiator to Spring Boot.
ApplicationConfiguration.java – Research configuration class responsible for exposing REST services for application users.
Patient.java – A simple class consisting of input such as the patient’s name, id, email.
PatientRest.java – Starts the implementation of the REST services in the project.
In this way, similar files are created for the other 2 projects with some additional files in Doctor_Microservice.
REST services are thus created to search patients and the diagnosis. Keys of patient and diagnosis are passed as a parameter to a method(PatientDetails) in Doctor_Microservice.This method gets the data of the patients and diseases.
Refer to the snapshot above. Here, we observe that Patient and Diagnosis classes are included in Doctor_Microservice_Edureka. These classes are cloned from their original projects.
Then to start the REST services, I have initialized **Patient_Microservice_Edureka **on port 8081, Diagnosis_Microservice_Edureka on 8082 and the Doctor_Microservice_Edureka on port 8083.
With the above configurations, when I run each service simultaneously, 3 different console windows run in the Eclipse console window.
As you can see below that after we run the projects, Spring Boot generates a boot log. This log consists information on initializing tomcat and its associated resources. The last line of the log indicates us whether our application has started or not.
To test if the patient and diagnosis services are functioning properly, one a browser (Mozilla Firefox) and go to http://localhost:8081/ and** **http://localhost:8082/ URLs. You should get the outputs as shown below:
Finally, to test the functionality of the Doctor_Microservice_Edureka, I simulated the information of the patient with id 2, suffering from a disease of id 3 and consulting a doctor with consultation 4.
The URL used is as follows: http://localhost:8083/doctor?idPatient=2&idDiagnosis=3&consultation=4
Refer to the snapshot below for the output.
In this way, 3 Microservices interact with each other to produce the desired results.
I hope you have enjoyed reading this Microservices Tutorial. We have seen a simple example to understand how different microservices communicate with each other.
#java #web-development #spring-boot #docker #microservices