14 Apr '15 An example of a Microservice

An example of a Microservice

After writing on Microservices, I received a few feedbacks from people who found the topic fascinating but struggled to see how to effectively apply it in reality. So I decided to develop a little microservice, to use as a practical example.

Introducing MicroTranslator

MicroTranslator is a microservice that I have built, which is supposed to handle locale and translations in an application. Its tech stack is the following:

Note: at the time of this writing (14 Apr ’15) I am still working on MicroTranslator. It will be production-ready (documentation included) in a few days. Sorry about that!

It comes with REST API

MicroTranslator provides simple-to-use REST API. For this reason, you need to know little to no PHP to use it. Simply fire up an instance of PHP on whatever machine you like, and then use the APIs to get the translations on the other Microservices that compose your application.

Now this is important, in my opinion. It means that you can quickly begin working on your application without having to focus on a tedious task such as handling translations, but also that, at any point in time, you can completely get rid of it and rewrite it with, perhaps, a faster language. Or you can simply upgrade your hardware and keep relying on it.

“I could have done this myself”

You may think now, “why should I use your stuff when I can write my own translation manager, using the tools that I like the most?” The simple answer here is “You shouldn’t (use my translation manager)!”

But I would also like to point out that, unless translations represent the core of your application, there is no reason why you would want to waste your time rewriting a library that does the same thing. But even if you do, what you should be avoiding is to tie it all up with the rest of your application. Using a Microservices architecture will allow you to replace parts of your application quickly and seamlessly. And, it will give you the power of choosing the right tools, framework, database, etc for the task.

Point by point

Like I said, one of the reasons why I worked on MicroTranslator is because I wanted to create a real-life example out of my previous article on what is a Microservice.

Let’s have a look at the points I outlined back then:

  1. It is a Stand-alone service/application.: Yes, MicroTranslator is stand-alone. It doesn’t need, and actually, it doesn’t want, to be included into your PHP application. You can connect to it, get and send to it all the data that you need, but you will not find MicroTranslator-specific code in your application. Just generic REST API calls.
  2. It is quick to (re)write.: Absolutely! I wrote MicroTranslator in a weekend plus a few other free hours during the week. You can totally rewrite it in a week or less as soon as you feel like it.
  3. It focuses on one task: It only cares about your translations. It doesn’t even want to know how you use them.
  4. It is Language-agnostic.: Well, MicroTranslator is clearly written in PHP, but you can use its APIs from any application, regardless of the language/framework/tools you are using.
  5. It is aware of failures. This clearly depends on the rest of the application. But, for example, you can cache the translations that you get from MicroTranslator, so that, should it be unreachable at a certain point in time, you can still handle the situation graciously. One thing is sure: if MicroTranslator goes down, it won’t directly affect your main application.

In practice, it’s not that difficult

It’s amazing how simple things can look like when we see them in real-life examples. In fact, despite the buzzword-mania and all the hype around Microservices, something that we often fail to understand is how simple they are. Building a Microservice is as simple as a few days of work. Building an entire application based on a Microservices Architecture is as simple as being able to separate everything into small services/applications that are only concerned about one little thing, and they do that very well. This is, by the way, also one the keys of greater productivity.

Question: have you already tried to build a Microservice, or even an entire application based on a Microservices architecture? I would love to have a look and learn from you as well. Share your work on Facebook or Twitter.