Imagine a monolithic application with a few features mixed together:
- Customers
- Products
- Orders
- Payments
- Notifications
And the business comes to you with a new feature. You must switch to another SMS gateway, pretty easy. But it wasn’t quick and fast; it was long and tedious.
You may not see these problems in small projects, but you will encounter them when the app grows. I find these problems to be common in bigger apps:
- When deploying this new feature, you must deploy the entire application
- The process of deployment also affects other mission-critical parts of the application
- High cost of change - the change may require a lot of time compared to its scope
- Running automated tests takes lots of time cuz of the broad scope
- When you encounter a breakdown in new code, your whole application may stop working
- Manual tests are long and complex because of changes spread across the whole application
This is when the business starts to lose trust in your team, but this is not about you. You may be doing your best, but the environment you are in may be poorly designed and require immediate change. You may be the best, but that’s not your fault.
Is this a signal to move the application to a microservices architecture?
What Problem Are We Actually Trying to Solve?
When you look at your problems, you have to start analyzing which problems you want to solve. In the above example, we had a few:
- Deployability
- Difficulty changing code
- Difficulty testing
- Fault tolerance
Deployability: This is where microservices or a service-based architecture may help. You want to deploy only the part of the application that you change.
You don’t need to worry about parts that haven’t changed, and you shouldn’t. This gives you peace of mind about other parts of the application, and you don’t need to worry unless they participate in the process. The rest is not affected, and there is no need for testing.
Even if there is an error, you know where to look for it - only one service was changed.
Basically - you want to deploy only what was changed. Deploying the others is not needed; they are outside this scope. Safer.
Note: separate deployment is not everything. You may still need to coordinate the deployment of a few services when you make a change. And now the problem may lie somewhere else - tbh, in my opinion, there may be backward compatibility issues.
Difficulty changing code:
This is especially a problem in a big ball of mud. You may be forced to change every part of the application when you need to implement a simple change.
This is a problem because the cost of implementation grows, and what’s more, the complexity of the change grows, which then leads to a greater risk of failures.
Those problems may be addressed by a modular monolith, a service-based architecture, or microservices. Even a monolith, if modular, may fix this problem, but well-designed bounded contexts are very important.
A change in one feature mustn’t involve changes elsewhere.
This also leads to the next problem.
Difficulty testing:
When the code gets complex and lots of parts of the app have been changed, the scope of testing becomes bigger and harder to determine.
Testing becomes hard, the cost grows, and the scope of testing is blurred. This is not a pleasant job. There is a change, but where exactly? Side effects are tested, and so is other logic along the way.
Fault tolerance:
This is something that makes our lives more peaceful. When you deploy your application, you don’t want to break the whole app (you don’t want to break anything, but life is life). If something bad happens in the changed code, it may lead to a breakdown of the whole application.
Imagine that you were making changes to the notifications module and introduced a memory leak, causing it to stop working. What happened?
You will probably feel that this is a bad situation, but it is much better than the whole application stopping.
In well-designed microservices, when you encounter a breakdown, messages will be delivered eventually (usually, this should be fine, but MFA codes may have expired, and the business process won’t succeed).
You see that? Even if something breaks, some parts of the application work and will be consistent eventually.
There is one more thing that we didn’t mention. We didn’t have a problem with it in our example, but let’s discuss it to cover all the modularity drivers:
Scalability:
We have to distinguish two things: the database and the code. Let’s start with the code.
You can scale a monolith, of course. The code will be split across servers. But it is not efficient.
Imagine you have a monolithic application that has a notifications module, among others. The whole app needs 2GB of RAM because of its cache. The marketing department comes to you and tells you that they are going to run a campaign and you need more capacity in the notifications module.
What happens? You need to scale the app, for example, three times. Then you have to use those 2GB for each instance, even when the other modules don’t need scaling.
Now imagine if you could scale only the notifications module, which uses 300MB of RAM. It would be possible to scale it three times with 1 GB of additional usage. Such a saving! 6GB vs 1GB (not even a whole GB).
Basically, you can scale each module independently, and that’s your target.
You may think that a service-based architecture will be fine, but not really. If you share a database, it may be hard to implement database scaling.
When you have a smaller database, it may be easier to have read replicas or implement sharding.
Dealing with a few tables is easier than dealing with hundreds - and you scale only the part that you need, not the parts that work fine and are already efficient.
Does the Monolith Really Need to Be Split?
You shouldn’t have a preference for your architecture. It should be designed for the problems you want to solve.
I’ve found that a smaller application can be a monolith; startups may use this architecture to get started. At this point, there is a relatively low cost of implementing it, and a microservices architecture may be overkill.
But I would go with a modular monolith to make it easier to split in the future when the app grows. This will significantly reduce the costs of moving to microservices.
When you don’t have problems with your architecture, costs are optimal. Deployments don’t hurt, so why change? Preference is not a reason.
Issues during development and while running the app - those should be the signals that we are starting to have problems with our application.
Or maybe the problem is somewhere else? Maybe with the deployment process? Maybe more frequent deployments would be better? Maybe a lack of automated tests?
What Does More Modularity Cost?
OK, we have discussed the pros. Don’t be over the moon just yet. When dealing with a more complex architecture, you have to pay the price somewhere else.
Did you think that there were no cons? ;) Did I catch you? ;)
We get maintainability, testability, deployability, scalability, and fault tolerance. But the cons?
You lose the simplicity of local calls (in the case of services); they are replaced by HTTP calls or message broker calls. They introduce dynamic coupling. You will have to deal with eventual consistency.
That means you have to handle network errors. You have to be prepared for them and use some proven patterns such as Outbox or Inbox.
The next thing - you have to keep contract backward compatibility in mind when making changes. You have to keep in mind that something depends on you and you mustn’t break the contract. This is what leads you to API versioning, sync or async. That leads to costs - maintaining two versions of the contract (in the case of breaking changes in the API). It’s always easier to have only one and change it when we want, but it’s not possible.
You have to ask yourself one question: do the pros outweigh the cons?
TL;DR
In our example, the problem with the notification service may be a signal to change. We had some limitations that this one change showed us. But one swallow does not make a summer. You have to observe your development more closely: do the problems persist?
If so, do some analysis. Maybe it’s time to change if you can’t live with these problems.
What’s important is to check whether your problems lie elsewhere. Maybe the problem is the development process? Maybe making smaller changes and deploying more often would solve the problem? Don’t blindly focus on microservices; changing the architecture has a cost and may not be needed.
But if you really need a change, do it. The point is not to make blind moves.
The goal is not to achieve independent deployments, the best scalability, etc. The goal is to understand your architecture and its problems, then assess whether the cost of change is justified.
Ps. Inspiration for this article is a book: Software Architecture: The Hard Parts written by Neal Ford, Mark Richards, Pramod Sadalage, and Zhamak Dehghani.
Comments
Join the conversation using your GitHub account. You can also browse the discussions on GitHub.