A topic that is profoundly discussed in software engineering or system design domain is whether to choose a Monolithic or a Microservice architecture while developing and deploying a server-side application.
In this article, we are going to discuss in detail about Monolithic and Microservice architecture, advantages and disadvantages of each and in the end will see which architecture to choose based on the system's requirements.
Monolithic Architecture:
Generally, when we develop a server-side application we do it as a layered architecture which contains the following layers:
- The presentation layer in which HTTP requests are handled and responds with data as JSON/XML
- The business layer where the application's business logic is written
- The third-party layer, which contains the code to handle third-party services like caching, queuing
- The Database Access layer, which is responsible for performing read/write/delete operations on the database
Despite having a layered architecture, while deploying all the code is packed together and deployed as a single-tier application. This is what a Monolithic Architecture is.
Advantages of using Monolithic Architecture:
- It is simple to develop. Time and Resource required for a monolithic architecture is comparatively less
- Deployments are easy when we are using monolithic architecture, we just have to pack our codebase together and copy it to the server
- As all the services are on the same server so problems of network latency and security are less
Disadvantages of using Monolithic Architecture:
- For any new developer, it is difficult because he/she has to understand the functionality of the whole application, even if his/her work is related to a particular functionality
- The whole application has to be redeployed on each update
- Reliability is an issue here. A bug in a certain feature of the application can bring down the whole application
- If a single part/feature of the application is getting high traffic, then we need to scale up the whole application. This is inefficient and might lead to unnecessary allocation of resources
- Adapting to a new technology is difficult as because the whole application might get affected due to it
Microservice Architecture:
In a microservice architecture, the idea is to split the whole application into a number of smaller services instead of building a single monolith application. These smaller services are called microservices. Each microservice is a small-application on its own having all the layers, i.e presentation layer, business layer, third-party layer and data-access layer and is deployed separately. These microservices communicate with each other via RPC or any message-based API.
In a Microservice architecture, instead of having a single database each service can have its own database. Although it might result in duplication of data but having a database per schema is needed if we want to benefit from microservice architecture. Another advantage of this is that a microservice can use the type of database that best suits its needs.
Have a look at the diagram below:
Here, an application is divided into four microservices - User Profile, Chat Service, Payment Service and Analytics. The Analytics and Payment services have a database of their own and the Chat and User Profile services are using a shared database. Each of the microservices communicates with each other as per requirement. The clients access the microservices via an intermediary known as the API Gateway which is responsible for handling tasks like load balancing, caching, monitoring, etc.
Advantages of using Microservice Architecture:
- If we update any particular service, only that service needs to be redeployed instead of the whole application
- For a new developer, it is easy because here he/she has to focus only on the particular service in which he/she will work. The working knowledge of the whole application is not necessary
- Microservices are fault-tolerant. If any particular service is down then also the other services continue to function
- Every microservice can be developed using technologies that best suits its needs. Like Analytics service can be built using Python and the Payment service can be built using Java
- In a microservice architecture, the services can be scaled independently. If any particular service is receiving high traffic, only that particular service needs to be scaled up
- As the no. of microservices increases the system becomes more complex. So, it becomes difficult to handle than a simple monolith application
- In terms of network usage, it is costly as because each service continuously communicates with each other over the network via RPC
- As there are inter-service communications, microservices are comparatively less secure than monolith applications
- Debugging in microservice applications is kind of challenging as because the flow of control may be over multiple services and it is difficult to find where the error actually occurred
When to choose what?
A Monolithic Application is best suited for applications that are simple and lightweight. If it has only a few functionalities and there is not much prospect of the application evolving then using microservices might be overhead. Also when the development team is small and cohesive it is better to go with monolithic architecture. On the other hand, if the application is an evolving application with lots of features and functionalities then Microservice architecture will help. When the team is large and diverse, then also adopting to microservice architecture is a good option.
So, I hope after reading this article you can now understand the difference between monolithic and microservice architecture. We saw with detailed examples of how these two work and the advantages and disadvantages of each. Also, in the end, we discussed the scenarios about where to choose monolith and where to choose microservices. Do mention your thoughts and views in the comments. Stay safe and Happy Coding!
0 Comments