Well well… let’s talk about microservices. I have been keen on following what was going on with microservices since its inception but I have never been its most vocal champion. I don’t believe in screaming microservices like a fanboy. It is mostly counter-productive.
With that being said, I see more and more people talking about them, but in many instances, it is approached like many other technologies or ideas in the past: “Oh look, it’s trendy, I must be doing it!” or “I am your manager, other managers tell me we should do microservices. So team, let’s do it!”.
This post will try to clarify a few things about why microservices may help you and some guidelines about building them.
Why do we need microservices in the first place?
Why indeed? Let’s be clear, microservices are not magical entities. If you believe switching to a microservices architecture will solve your problems, well, 99% of the time, you will be proved wrong. Let’s make it clear:
All or other things being equal, microservices will not benefit your project.
This does not start well, does it? Still, it is critical that we are honest about it.
Maybe it is the wrong question. A better one should be: “Why does our software fail to keep up with the business’s needs?”
Indeed, this is real issue here. The feeling that your software eventually hinders your capacity to mov the business forward. It’s a shame because it eventually ends up with some sort of bitterness and grief from within the company.
Obviously, hearing that microservices are small components that make you faster is attractive, but they can only help you if you understand that what truly matters is the shift towards designing for change.
In other words, we need microservices because we want to adapt our software more frequently to keep up with inevitable changes and the diruptive aspect of innovation. Inevitable doesn’t mean we should be defeated, quite the contrary! Indeed, the more we welcome and design for change, the more confident we will be in releasing fast and lean software that respond appropriately to its expectations.
What is a microservice by the way?
Russ Miles often plays the game of finding the wackiest definition of microservices, this one by Micah Blalock is certainly worth a read. Truth be said though, there is no strict definition of microservices and it’s a freaking good thing. The minute you have an industry-approved definition, you get ready for a feast much like the industry had with SOAP for instance.
Still, we can suggest some features that apply well to microservices:
They should strive at being simple and comprehensible. You can’t change something you don’t understand.
They are single-purpose components. In other words, they are small not because you count the number of lines but because they do one thing only. Usually, that one thing means your code-base for one microservice will remain small.
They should be treated like cattle not pets. Do not care for your microservices instances, care for your system’s general status. Kill and replace microservices that are unhealthy. This is quite a change when you come from a monolith application because you tend to care for its status much more.
A related one is that microservices ought to be independant. This will make it so much simpler to comprehend how to evolve your system.
A second related principle, microservices sould not share states. Decoupling your code while keeping coupling at the data level is a recipe for failure.
They should be keen on following Postel’s law: “Be liberal in what you accept but conservative in what you send”. The idea here is to play nicely with the change your system will constantly endure while reducing the coupling imposed by one microservice onto others.
They ought to be easy to roll out. Let’s recall that we want to keep up with fast pace of changes. We can’t achieve this if the release cycle is slow and complicated.
Honestly, there isn’t much more to it.
It is more the way they allow for an antifragile system to be built rather than how they are individually implemented. In other words, don’t over-think your microservices. Just make sure they help you design for change in a fast and confident manner.
How to build microservices?
At this stage, you should realise I do not believe there is one true way to implement microservices. However, there are definitely tools and approaches that will help along the way. I will enumerate a few of them, but please explore the field for what works for you. You would also do worse than reading Sam Newman‘s book “Building Microservices“.
First, you need to decide what are your microservices. Russ Miles describes a rather useful mechanism to scope your microservices using the Life Preserver diagram tool. You may even design around the CQRS pattern to further refine your microservices’ responsibilities.
Then, make a decision regarding the integration’s style you’re going to apply to your system. You don’t have to pick only one. Most of the time, you’ll hear about REST (or sort-of anyway) but you could also bet on a message oriented approach. If you use REST, you might want to use StopLight.io to design your API and even export it following the Swagger 2 format.
Recall that you never want to share state accross your microservices, at least not through a single common database for instance. What matters are the events flowing through. You may therefore rely on event-sourcing perhaps and reconstruct local states rather than care for consistency that is based on strong coupling. Finding the best event-store for your requirements and constraints may require some testing still but Kafka has gained some points.
Microservices are polyglot by nature. There is no need for you to keep implementing them using one single language. As per usual, chose the right tool for the job.
Please, do make sure you test your microservices.
Probably package up your microservices into portable images. This will reduce the complexity of dependency management and make it much simpler to distributed. You will likely benefit from Docker or Rocket.
Obviously, if you package up your microservices with Docker/Rocket, you will gain from managing their high-demand life-cycle through a service orchestrator. The field is quite busy but the major players are Google Kubernetes, Mesos/Marathon, Docker Swarm, Amazon ECS, CoreOS Fleet or Rancher. Really, this is worth taking the time setting a proper test for them because their documentation will not be enough to distinguish them.
Make sure your microservices can be discovered! Rely on a service discovery tool such as Consul or etcd.
Ensure your microservices play nicely with external loggers like LogStash as well as monitoring tools like DataDog or Prometheus. Honestly, in both areas, there are tons of products so make time to review some of them.
You will likely want to set some sort of proxy before your services (to control the rate or secure the requests for instance or load-balance between microservice instances) using something like Kong or HAProxy.
There are many related topics obviously but I will conclude with two critical aspects of building microservices properly:
- As a developer, you own your microservices. This means that you can’t stop caring for them once you have pushed the code out. Your job is to follow-up to production. Talk to your ops. Actually, just don’t talk to them, make sure you work *with* them!
- Stress your system! Remember you are building an antifragile system. You want to be confident you can test the plastic nature of your live system. Stress it!
That’s a lot to think about! Is there a simpler way?
Everything I have listed before should be on your shopping list when you build microservices. If you feel like it’s quite overwhelming, you will be happy to hear that platforms start emerging to take care of the entire operational aspect. For instance, AWS Lambda, Google Functions and (as it appears) the newest player in the field: Atomist. Make sure to review them.
 Note that I am using “business” in a very broad sense here, don’t get hang on it too much.