Monolithic vs Microservices Architecture: Which One To Choose
November 14, 2023
Often entrepreneurs who want to create some service may hear such strange words as "microservices" or "monolith" from software developers. What is a monolith? What are microservices? Why is it necessary to know them? What is the difference between these architectural approaches in development?
Before starting to implement their idea, entrepreneurs should find answers to these questions and decide which approach better fits their business requirements and capacity. Why? Firstly, the entrepreneurs are responsible for their project and hope for its success. Secondly, they invest their money in it and do not want to throw it away.
To make an informed decision, we will tell you in this article what monolithic and microservices architecture are. To truly grasp the essence of these approaches, let's examine the case of a factory that produces a particular device - let's say, telephones.
Let us imagine that the monolithic application is the factory. In the initial phase, it is best to set up one factory and create the phones in one place. We know that the quality assurance engineers can check the devices at every step of production, that we have to pay a stable rent, that our engineers work in one place and that all our wishes are applied to all products.
If something goes wrong, we can find the cause as quickly as possible because all the production chains are at our fingertips, and it doesn't take long to fix these problems. In addition, we have no delays between departments. When we need buttons for a new version of a phone, we can simply knock on the nearest wall and the worker in the department behind the wall will bring us a box of these buttons.
The workflow seems simple and well-established, doesn't it? But there are also some disadvantages. First, it is very difficult to change some production machines. We have to stop all production circles to change some technologies in the processes to be developed. If some part of the conveyor breaks down, we have to stop all the processes.
When we decide to hire a new employee in our factory, we need to spend a lot of time describing all the processes and connections between the units of our production process. If we want to set up additional departments in our factory for new production features, we will face the problems of expanding the infrastructure. We will need to find additional locations near the plant, get approval from the municipal authorities, lay a new power line, etc.
Thus, we can conclude that there are pros and cons to the monolithic approach.
Meanwhile, microservices can be compared to several small factories. They create parts for phones in different places and assemble everything together in another place. This will have certain advantages. Each factory will be small and this will affect the cost of renting land. Also, we can choose different countries and regions for our factories. For each factory, we can hire specialists of narrow expertise - it will be cheaper than employing full stack engineers.
If we decide to add a new part to our phone, such as a stylus, all we need to do is choose a cheap region with all the necessary infrastructure and build a new factory for it. The quality of each factory's output can be checked separately. If we decide to change some technologies, it is enough to stop only one factory - the rest will work correctly.
However, there are certain disadvantages of this approach as well. First, it is very difficult to find a problem in the technological process when it occurs. For example, we have a defect in the device. On the finished product, we have buttons that have no subdivision between the individual button pairs. We need to check at what stage the problem occurred. The button factory, along with the partition factory, assured us that everything was in perfect condition. Additionally, the assembly plant skillfully put all the components together. Nevertheless, despite these efforts, we unfortunately ended up with a defective device. We have to spend a lot of time checking the cause of the problem - we have a big hole in the partition wall, small buttons, or a problem assembling a mechanism.
Another type of problem can also occur: all the factories are working well, but the transport companies are on strike and we can't assemble the parts. Furthermore, we have to pay taxes in different regions, or even countries - we need an additional accountant for that.
As you can see, microservices also have a list of pros and cons.
Monolithic vs Microservices Architecture
No doubt, the above example of factories and phone production gives a better understanding of the difference between monolithic and microservices.
Now it is time to switch to the world of IT and draw the conclusion. To do so, we will briefly list the advantages and disadvantages of the two architectural approaches.
Advantages of Monolithic Architecture:
1) Simplicity of development.
Monolithic architecture is relatively simple to understand and develop. All the components of the application are packaged together, making it easier to manage.
2) Easier deployment.
Monolithic applications require fewer deployment steps than microservices because the entire solution is packaged in a single module. Thus, deployment processes tend to be simpler and faster for monolithic applications.
3) Unified code organization.
All components of the monolithic architecture are tightly integrated, making it easy to share code and libraries across the application. This unified structure leads to better organization and consistency of the entire code base.
4) Improved performance.
Monolithic applications can deliver better performance due to the absence of communication overhead between services. Interaction of multiple services over the network does not cause additional delay, resulting in better performance.
Disadvantages of Monolithic Applications:
1) Upgrade complexity.
Upgrading a monolithic application is one of the most difficult tasks. If we suddenly want to add a new feature to the monolith or perform an update, you will have to redeploy the entire application. This will take more time and effort than upgrading a single component.
2) Poor scalability.
Scaling monoliths is not so easy because the whole application is scaled. Even if only one particular component really needs it, we still have to scale the whole application.
3) Low resistance to failures.
If one of the modules of a monolithic application fails, the performance of the entire product is affected. The reason for this is that the connections between the various components are too tight and too strong.
4) Inflexible technology stack.
Monolithic architectures are built with a single technology stack, making it difficult to introduce new technologies or switch to other tools. This rigidity can hinder innovation and slow down development.
Advantages of Microservices:
1) Faster deployment.
If we, developers, want to make changes to specific "pieces'' of the application, we simply need to deploy the microservices that need to be updated. We don't need to spend time and effort deploying the entire application with microservices.
2) Improved fault isolation.
If one of the microservices within an application fails, it won't shut down the entire system at once. And after we fix the failed component, we can update only that component without having to deploy the entire application again.
3) Flexible technology stack.
Microservices provide the flexibility to try out a new technology stack on an individual service as needed.
4) Improved scalability.
Microservices architecture enables better scalability because individual services can be scaled independently.
Disadvantages of Microservices:
1) Higher complexity.
The degree of complexity of microservices is higher compared to monolithic architecture. The systems are split into multiple services, the architecture becomes more complex, and understanding the interactions between the different services can be a challenge.
2) Poor distribution.
With microservices, data is often exchanged between services via the network, which leads to delays, network outages and increased load.
3) Data management.
Microservices may have their own databases or data stores, making it difficult to maintain data consistency between services. Maintaining data integrity usually requires extra effort, which companies only realize when they're confronted with the problem. This problem is solvable, but requires far more resources than many realize.
4) Dependencies between services.
Because microservices communicate via APIs, changes to one service can affect others. The result? Version control issues and potential compatibility problems, especially when upgrading or changing services.
Migrating From Monolith to Microservices
"Which architecture is better: microservices or monoliths?" As you've probably already guessed, the answer to the question is: both. Both are good and bad at the same time.
The best way is to combine two software architectures. When we start a new project, we must use a monolithic approach. But we need to build each service according to the development patterns (when each part of the code has only one responsibility) and have a complete test coverage for each case in these services.
If we adhere to these two principles, we will be able to restructure monolith application to the microservices architecture. To tell the truth, we will have microservices architecture in monolith architecture, if we follow these two principles.
Martin Fowler, author and international public speaker on software development, also suggested combining both approaches into one in his article "MonolithFirst." According to him, in a monolithic project, it is easier to observe the cohesion of modules and add new functionality. The system is then broken down into several parts, and these are redesigned into standalone microservices.
This overcomes the main disadvantage of creating a microservice architecture from scratch: it is not necessary to spend a lot of time on the initial division of the system into components and arranging interaction between them. It is important to choose the right moment to start dividing into services: too early - and microservices will not be filled with the necessary business logic, too late - the boundaries of modules will be blurred, and the division into microservices will be difficult.
A sure sign that it's time to start splitting is when the cost of new functionality becomes higher than its usefulness. For example, if an application needs to connect to several payment systems, it is better to separate this logic into a separate service than to adapt the whole system for them.
Both monolithic and microservices architectures have advantages and disadvantages. Also, it is important to note that the choice between monolithic architecture and microservices is not binary. Hybrid approaches are also possible, such as "microservices within the monolith", when some parts of the system are transferred to microservices, while the rest of the system remains in the monolithic architecture. The decision should be made based on the specific requirements, complexity and goals of the project.