Hi, this will be a series of articles on microservices with Java, where we will be learning each and everything about microservices i.e why, what, when is it necessary to know anything. Let us start in detail.
Why Microservices?
As we move forward we all know we have one more architecture pattern i.e Monolithic Pattern that was used, so there's a certain disadvantage of that architecture.
Monolithic Architecture
Let us first explain what exactly monolithic architecture means, so basically all the components of monolithic applications are interconnected or interdependent.
In other words, it will give tightly coupled software where each component along with its associated components must be present in order to execute or compile the code.
It means all the components are actually dependent on each other.
It is like a big container not divided into small components so even though these components are there, they are pretty tightly coupled right.
Example
Let us explain it with the example and make use of the use case of e-commerce application to check what monolithic architecture exactly means with this example.
So as you see we have common customer service, product service, and cart service which a customer can access through their browsers, and when you launch the application it is displayed as a single monolithic application, it is a single service. So in order to scale it, you can run multiple instances of this application behind the load balancer.
Advantages
- Very simple and one direction to develop as a goal of the development tools.
- Simple to deploy as we have to launch the application at runtime.
- Simple scale build load balancer.
Disadvantages
- Large and complex application as rescale development slows down and modularity breakdown.
- Slow development as applications become difficult to understand and modify.
- Blocks continuous development and deployment.
- Unscalable, Unreliable and Inflexible.
What is Microservice Architecture?
According to docs, microservice is an older style that structures an application as a collection of small autonomous services, modeled around a business domain.
Microservice Architecture
In Microservices architecture, each service is self contained & implements a single business capability. In simple words, a large application can be broken down into multiple services which together access a larger system. It means we have multiple services but all of these services do not share the data structure but they will be commuting via API’s.
Advantage of breaking down into a single microservice is that it can focus on only one single business capability which definitely leads to better quality & easy to understand.
Example
Assuming monolithic application all the components are in single modules but here all the components are divided into separate modules which communicate with each other using a well-defined interface usually REST.
In microservice, we have request & response which is an independent stateless transaction and here data is federated. Each microservice is responsive to its own data model and data because each communication is handled by different instances. As you can see a microservices which is a small independent & loosely coupled.
Features
- Small Focused: As microservice applications are easily maintained and developed by development tools.
- Loosely Coupled: As each microservice is independent of each other, this makes development & deployment much quicker.
- Language Neutral: To be a good microservice it should be language neutral i.e in some services it is written in python for faster deployment whereas some in java are rich in libraries, so it doesn't affect the other services.
- Bounded Context: Each microservice doesn’t need to understand implementation of other microservices.
Advantages
- Independent Development
- Independent Deployment
- Fault Isolation
- Mixed Technology Stack
- Granular Scaling
Monolithic vs SOA vs Microservices
Monolithic Architecture
Monolithic is like a container, which is basically containing a number of components and if they are deployed and developed together i.e known as a monolithic application.
Challenges in Monolithic Architecture
- Not Flexible: Monolithic applications can’t be built using different technology.
- Unreliable: Even if one feature doesn't work, the entire system will not work.
- Scalable: Not scalable, if we want to scale, we need to rebuild the whole application.
- Blocked Continuous Development: All the features can’t be built & deployed at the same time.
- Slow Development: Development is slow, as each component has to be built one after the other.
SOA (Service Oriented Architecture)
In SOA,
- Services are broken down so they are called coarse grained.
- Communication between services takes place with messaging.
- Components/features share the same database for storage.
- Designed as a reuseable component/utility which can be used by anyone in the service.
Example
A software application has four features so four features are broken down into a single feature.
Microservices Architecture
In microservices,
- Services are broken down into features and this feature is further divided into several tasks called fine-grained.
- Communication between services takes place via REST calls.
- Components or features share the different databases for storage resulting in faster retrieval of data.
- It can be reused but intentionally it is not desinged to be reused.
Summary
When it comes to choosing between monolithic, SOA, or microservices. Monolithic is like a big container which consists of all components in one, while SOA i.e Service Oriented Architecture is dividing a whole application into different components/features, while microservices is dividing an application into different components or features and these components or features are further divided into separate tasks making service easy to read.
All architecture has its pros and cons depending upon its usage.
What did we learn?
- Why microservices?
- Monolithic Architecture
- What is Microservice Architecture?
- Monolithic vs SOA vs Microservices