Micro-Services — Theory

R.R. Dev
5 min readDec 1, 2023

--

Hi everyone! As I said in my previous post, I’ll talk about micro-services, YEY! but I’ll split it on 3 posts (not necessary one after another), the first one will be this post you’re reading, the theory, the second one will be a practical exercise, I’ll teach you how to create a micro-services architecture with Java and Spring Boot and the third one will be how to deploy this project to AWS to have a functional application online, what do you think? Awesome right? but for now let’s start with the theory in order to understand what we gonna do in the future.

What are Micro-Services?

Well, let me tell you a bit more about monolithic before, in the past, we have our application in this architecture, having everything in the same project, to me, for example, the university taught me always the MVC model in one single projects, sounds familiar? If the teacher gave us some homework like, “Build a sales point website that should have a responsive UI, a backend that can save user data, cart data, product data and orders data” what we would do it’s creating a project and then have three folders, model, view, control, and have all the entities in model, or maybe divide model in four, model/user, model/product, model/cart, model/order, and do the same for all the other areas, well, this is a great architecture if you have school project but in the real world, this kind of architecture could be a problem, let’s see why.

Related with a previous post, Clean Architecture — Make it clean, it is a bad idea having everything on the same place, you haven’t read the post yet right? Okay let me, say this, on which scenario would be easier finding a book, in a library with just one shelf with all the books there or in a library where all the books are in multiple shelfs with its own category? You got it right? Well, that’s the correct comparisons between monolithic and micro-services.

For a more formal description a micro-services architecture is an architectural style that structures an application as a collection of services that are:

  • Independently deployable
  • Loosely coupled
  • Organized around business capabilities
  • Owned by a small team

Real World

Let’s see how the real world looks taking as an example the homework we mentioned before, a website with a UI, a backend responsable to save user, products, cart and orders data, the details right now are not important, it doesn’t matter what kind of data or processes we need on that project but what it matters it’s the way we can create our micro-services.

First, as you can see, it’s pretty clear which micro-services we need to add, one for user data, other one for products, cart and order, see? So, each micro-service has its own responsibility, for example, user micro-service will care for sing-in, sing-up, authenticate, and roles (Just an example), product micro-service has to store product data, price, amount, photos, and the other will do the same, none of them really don’t cares about what the others are doing, they have its own processes, and yes, now you have the thinking of “What if I need something from a micro-service in another micro-service?”, Right, great question, well, everything is relative, and some of the ways to do it are listed here:

Rest API

If you don’t know what Rest API is, I’m assuming you are pretty new and this is not the subject of the post, however, let’s give some context, a Rest API it’s not another thing than just a listener, this listener it’s able to catch HTTP request from a client, this is the big picture, for more information, you can look for a tutorial or something, I don’t know if I’m going to post something related but yeah, you can have an idea with this.

Now, you can imagine how micro-services can communicate each other, yes, you can make internal HTTP request to another micro-service, let’s make an example: Imagine you have a cloud with multiple micro-services, user-service and cart-service on this case, you need to get the information of the user when display the cart information, what you need to do it’s make an internal HTTP request from cart-service to user-service and TA-DA! You’ll have the data in milliseconds and keeping the single responsibility and clean architecture.

Dependencies

Maybe this method it’s not a way to communicate between micro-services but it’s useful to standardized data across the cloud, you can have a project in you cloud that will work as a library use for every service, you can have a library named “Commons” and there you can have, I don’t know, general validator for strings and numbers or some DTOs shared across all the micro-services, in this way you can code without losing quality, no duplicate code and no losing data or incompatible entities.

Deploy and Production

Last but not least, how to deploy it?, how this works in production? Well, when I started I thought it was having ever micro-service in different servers and call them through internet hahaha, if you thought the same, give me five! but in reality we have some tools to make this posible.

Docker

I mentioned Docker in previous posts, and again, this post it’s not about Docker, so, let me give a big picture again. This software is use to create and handle containers, this containers are like lightweight virtual machines, you can run everything on them, included micro-services and one of the features of Docker it’s creating a digital network that connects every container so they will know each other and can make internal HTTP requests whenever you need. All this configuration could be in AWS, Google Cloud or Azure, so, just pick one.

Kubernetes

Other software I mentioned in previous posts, everything is connected, I’m an internal digital network AWESOME! haha, back to the real world, Kubernetes has a similar feature, internal networks between pods, maybe it’s just a decision related with prices and software size.

Maybe there are more options but I actually don’t know more of them haha they’re the only way I know and I used before.

That’s it for me! This is the first post about micro-service, I’ll post the other 2 later and I’ll update this post with the link so you can follow the process, I hope you like the information and see you later!

--

--