This blog is about Java (advanced Java topics like Reflection, Byte Code transformation, Code Generation), Maven, Web technologies, Raspberry Pi and IT in general.

Montag, 1. Juni 2015

Microservices: a few thoughts



Microservices are a hot topic right now. At work we are discussing if we should move to microservices, too. In my opinion it would be the right choice. First of all our current software is very old and has many flaws. Therefore we need to rewrite the software anyway. Second the concept of microservices fits us very well and it's in general a great concept. 

Specialists versus Generalists
A problem we currently have is that everyone must know the complete project, which is quite big. We don't have modules on which the developers could specialize. This causes a lack of code ownership and deep knowledge and understanding of the code. Therefore the code gets worse and worse. With microservices you have strong "modules". So each team/person can focus on a group of microservices and know only about the interfaces of the other microservices.
The disadvantage is that you have a more difficult time with the project management. Because you have to align your features accordingly to the teams and their knowledge.
If you can handle that then the overall output and quality should be considerably better with specialized teams. More features mean mostly more money and that's a good thing. This is not really a microservice thing. It's just an argument for a good architecture and for specialized teams.

Impact of changes
Another nice benefit is, that you can delimit the impact of a change
better. Since a change in a monolith can always have strange side effects. This shouldn't happen with microservices.
It's very important that the microservices are fault tolerant. So if one microservice misbehaves then it must not poison the other microservices. The outage of one microservice must be contained. Meaning that a part of the functionality is missing or broken. But everything else must still work fine. With this concept you can take better care of your core microservices. The not so important microservices could have less quality. Thereby it's possible to optimize the development output - invest only as much as needed.

Homogeneous Stack versus Inhomogeneous Stack
Many people say that another benefit is that you could use different frameworks or even other programming languages. But in my opinion this doesn't make sense. At least if the team is not very big. Or you have very special requirements. Because a homogeneous stack makes everything much easier. Even the build tools, build configuration and the deploy pipelines should be the same. Otherwise you violate the DRY (don't repeat yourself), because you have some kind of "code/configuration/script duplication" and have to solve each problem for each programming/framework-stack.

Upgrades
You still have the benefit of upgrading the microservices independently. If a microservice runs stable and doesn't change then there is no need to upgrade it. With a monolith you don't have this option. If a library is upgraded then everything is affected. Even the code you didn't touch for years and worked perfectly until the upgrade. Because of that a library upgrade in a monolith is very dangerous and is done rarely. Microservices can be upgraded one after the other. So there is no big bang upgrade but many little upgrades. Furthermore you shouldn't have a jar hell like in a monolith. This makes upgrading easier, too.

Flexibility
In general you have much more flexibility with microservices. Because one huge monolith is very hard to change. What would it mean to change a core library in a monolith? For example to change a self written URL dispatcher to Spring MVC. Of course there is way too much logic in this layer. For the current project this change would mean a rewrite of a big part of the system. Which would be very error-prone. Like in the "Upgrades" section the problem is, that you have to change everything. It's not reasonable to change only a part of the monolith to Spring MVC. In some cases a partly change is just impossible. The general problem with partly changes are that you have to support still the old self written URL dispatcher and Spring MVC. The whole system gets more complex. And in a few years the next library will come that you will want to use.
But this isn't only true for core libraries. It's also true for smaller libraries. If there is a new major library upgrade, which breaks the old API, then you have to migrate a huge code base to the new version. It's not possible to write only one new feature with the new library version. For this single feature this doesn't pay out. So you don't do it and use the old library which forces you to write more code. Now you have a technical dept. Because all other features in the future are limited to the old library version too.

Reusability
How does the reusability go? First you have a class which uses the same method for the same task. The next step is to have a package for a more complicate task. If it gets even more complicated you write standalone and independent libraries and add them as a dependency. Microservices are the next step of the reusability.
If you have several systems which need to send emails, then you probably have in each system an email library dependency. Furthermore code to use this library and this code is most likely an code duplication. Because it doesn't pay off to create a library which will be used in each system
only for a few lines of code. In a microservice world you create a microservice which handles the emails. Then in all other places you just have to do one remote call and you are done. If you create a new system, for example a new batch job, then you have to use the email-microservice and you are done. No need to integrate an email library into the batch. If a new requirement comes in that all mails should be resent after an hour if the initial delivery failed then you only have to take care of this requirement once. Not several times - for each system which sends emails.

Scalability
This is a controversial topic, because not every software system needs to be extremely scalable. Many software systems work fine with just one big database (which is hopefully redundant). Therefore it's not always an argument for a microservice architecture. If you need scalability then microservices are great. There is one condition: the microservices need to be stateless. Then it's very easy to start up more instances of the microservices and scalethe system this way.

There is another kind of scalability besides of the technical scalability: the developer scalability. It most likely won't work to have hundreds of developers working on the same monolithic code base. But again not everyone has to face this challenge.

Conclusion
Microservices don't solve everything. In contrast this concept introduces new challenges and problems. Nevertheless I think that these challenges are manageable. The benefits you get from the use of the microservice archtecture are huge. If the system is complicated enough, you know that your team can handle the microservice-challenges and creates a good microservice architecture then you should definitely consider a microservice architecture. If one of the criterions aren't met then stick to your monolith.


A word of warning: a good microservice architecture and microservice infrastructure is very hard to create and you need many things: an excellent concept, a very good understanding of your domain - so that you can split up your monolith in the right way, excellent monitoring, continuous delivery, automatically deployments and fast deployments, handle failures gracefully - so that failures don't create a ripple effect, be able to handle your data if it is distributed over several databases, and so on.

1 Kommentar: