Monolithic vs. Microservices Architecture: Which Should I Use?
Are traditional monolithic architectures difficult to scale because of increasing complexity? Are microservices worth the effort and engineering? If you've struggled to answer these questions around the differences between monolithic vs. microservices architectures for your software projects, this post is for you.
In this roundup, I'll walk you through a holistic comparison between monolithic vs. microservices architectures. By the end of the post, you'll have some good insights as to which you should use and when.
Monolithic Architecture at a Glance
First, let's have a quick refresher on what a monolithic architecture is. It's the conventional and most commonly used architecture in software engineering. It advocates that all modules of your software should coexist in one place.
Thus, different components, modules, or submodules of the application are tightly coupled. They're also highly dependent on one another.
Example
Here's a simple example of a monolithic architecture. Let's say you have a travel and tourism application that allows users to purchase airline tickets and book hotels. At its simplest, your application will have four features or modules:
- Travel: Displaying airfares and booking airline tickets
- Hotel: Listing and booking hotel rooms
- Purchase: Allowing users to make payments
- Auth: Authentication service for signup, login, logout, etc.
Here's how the application's monolithic architecture would look:
Make sense? Let's now have a quick refresher on microservices architecture.
Microservices Architecture at a Glance
Microservices advocate breaking the monolithic application into small pieces or components. In other words, it takes the entire monolithic architecture and abstracts each module as a separate monolithic architecture in itself. It takes a modular approach toward building large-scale applications.
Each individual service or component of the microservice is loosely coupled with another. Further, they run independently as an autonomous body.
Example
Our previous tour and travel application can be converted to a microservices architecture. Here's how that would look:
Factors of Comparison
Let's now compare these two architectures on the basis of several factors. These factors can help you decide which architecture would suit you best, given your use case.
I hope you remember our tour and travel application example from earlier? We'll use it extensively in our comparisons! That will help us highlight these differences from a practical system's standpoint.
Development, Debugging, and Deployment
Just like user experience, developer experience is super important. The three D's (development, debugging, and deployment) greatly contribute to the developer experience when building an application.
Development
A monolithic architecture will most likely have a single codebase or repository for development. This means there's only a single codebase where all developers are independently contributing. They're pulling in the latest code and pushing and merging their own code from the same remote repository. As a development workflow, this is prone to code conflicts. As a result, dealing with these conflicts could slow down the pace of the workflow.
Imagine a team of developers working on the payment module of our app. Another team, at the same time, could be working on the auth module. However, they're both pulling in the latest code and pushing new code to the same repository. This isn't needed since their work is unrelated and may cause grounds for confusion.
On the other hand, each microservice can have its own separate codebase. Then, a small team of developers can work on these individual microservices. This results in fewer chances for code conflicts and speeds up the workflow since each codebase is now completely independent of the others.
So now the payment team doesn't have to worry about the latest changes in the codebase that the auth team has pushed. Similarly, the auth team can push their code without looping in the payment team for their changes. Definitely a neater, nicer, and merrier development workflow!
Debugging
The tightly coupled architecture in monoliths often leads to errors and bugs that are difficult to track. This is because changes in one component or module can often lead to unwanted changes in another related module.
Imagine you were working on fixing a bug in the auth module. However, since it's closely related to the payments module sitting in the same codebase, you accidentally introduced an unwanted behavior in the payments module. In this case, your monolith is at the disadvantage of being more prone to such bugs in the application.
Your microservice, on the other hand, is more robust in this case. No matter how many bugs you introduce in your auth module, your payment module is unaffected since it's loosely coupled with the auth module. Even accidental changes in your auth module could not affect the payment module since they're separate projects themselves.
Also, microservices simplify fault isolation. When something goes wrong, you immediately know which service needs repair. With monoliths, there's a lot of hopping around to extract the problem.
Deployment
So now let's say you managed to fix that bug in your auth module. Awesome! But what's next? You'll need to ship this code to production.
In a monolith, to update your auth module on the server, you'd have to redeploy your entire application. One of the huge downsides of monoliths is a small change in a supposedly isolated module could lead to deployment of the entire application. Further, if we reel in the associated downtime, it could be an upsetting situation for your business, users, and overall product.
With microservices, however, you only deploy what's necessary. So, in this case, you can independently deploy your auth microservice. Any associated downtime is only reflected in that auth microservice. All the remaining microservices continue to operate as they have been doing.
Infrastructure Cost and Complexity
Up until this point, you might think microservices are clearly better than monoliths, right? But at what cost? Complexity! And also, literally cost!
The more components your architecture has, the more difficult it is to manage and handle them. Consequently, the cost and complexity of the overall application become higher. Think about it. You have different services, each hosted independently. That means individual codebases, individual CI/CD pipelines, and different servers. Further, you also have different databases for each microservice. Again, the added cost and complexity of hosting your individual databases could be massive.
Monoliths are simple. Period. There's nothing fancy about them. Infrastructure wise, you only need a single server for your application and another one for your database.
Interservice Communication
Let's come back to our application example. Say a user opens the app to book a flight. Here, two modules of your application come into play. First, the travel one that lists all the flights and reserves a flight for you, generates your e-tickets, etc. Second, payment! You can't book a flight without paying for it, right?
But how does the travel module communicate with the payment one? For a monolith, this is simple. You could invoke your payment module from your travel module at the time of payment. Remember, all your modules are in a single place in your monolith. You can run any code from anywhere.
But what about microservices? In this case, your travel microservice will have to interact with your payment microservice. And if there was a complete travel package (e.g., airline tickets, hotel room, etc.) that the user wants to book, each microservice would need to interact with the others. Further, some specific use cases could have a lot of to and fro between different microservices. For instance, a user books a flight and a hotel for themselves then books another hotel for a guest for a business visit.
Clearly, interservice communication is easier in monoliths than microservices. However, that's not to say that microservices have poor interservice communication. It simply takes a different approach that may not be easy to grasp at first. Further, you could set up service orchestration to have your microservices communicate with each other more efficiently.
Performance
Who wouldn't want their services to be faster, efficient, and highly performant? Monolithic services can be faster than their microservice counterparts. Where this performance comparison becomes distinct is interservice communication.
With microservices, each interservice communication will most likely happen via an API call. In monoliths, this API call is converted into code calls or function invocations. In this sense, monoliths can be faster in performance.
Also, you could set up simple browser caching for your monolith. It's one domain and origin, right? To implement caching for your microservices, you'd have to put in more work than monoliths. However, on the upside, microservices are lightweight. In that aspect, they could perform better than a large and heavy monolithic application.
Security
It's difficult to declare a clear winner in this segment. This is because both can do better and worse than the other in some aspects of applications and API security.
For starters, I'll give more credit to monoliths in terms of managing, monitoring, and auditing security. It's simpler to do all that as compared to microservices because of the many moving parts involved. Further, interservice communication could also be an entry point for security attacks. You'd need an airtight implementation to ensure you don't leave any loopholes behind.
However, if one service of a microservices architecture is under attack, the others can remain safe. Sadly, with monoliths, an attack on one service means the demise of your entire application. Also, you can use expert security tools like Traceable AI to safeguard your microservices. It constantly learns about your microservices' interservice communication and provides insights on which services need tweaks to security. Or which services may be prone to attacks.
Scalability
If you're dealing with tens of thousands of API requests a minute, kudos! Your application is growing! So how do you scale your system with the rising user base?
For monoliths, you can perform horizontal scaling via a load balancer and multiple server nodes. This would enable your monolith to scale effectively without a single point of failure. So if one of your server nodes or your API server goes down, the other nodes can come to the rescue. However, one drawback that will always persist with scaling monoliths is the segregation of business logic.
As you scale, the need to separate your modules or components at the business level becomes more important. Of course, with microservices, you'd be taking care of that starting on day one. But you can't scale microservices horizontally. However, you can do granular scaling by scaling individual microservices as and when needed.
Summing Up
Based on the above comparisons, you need to decide which factors matter most to you. Are you just starting out? Go for monoliths. Have a small team? Go for monoliths. Do you wish to scale from a medium to moderately large application? Try horizontal scaling in your monolith.
But if you're approaching the scale of millions of requests per minute, give microservices a shot! If you have a large and diverse team, you could also use a variety of tech stacks for your microservices. If you can bear the cost and complexity of microservices, don't think twice!
This post was written by Siddhant Varma. Siddhant is a full stack JavaScript developer with expertise in frontend engineering. He’s worked with scaling multiple startups in India and has experience building products in the Ed-Tech and healthcare industries. Siddhant has a passion for teaching and a knack for writing. He's also taught programming to many graduates, helping them become better future developers.
The Inside Trace
Subscribe for expert insights on application security.