Search code examples
microservices

Monolithic to Microservice


I have a monolothic application which I am trying to convert to micro service based apps.

Approach 1:-

  1. Database still remain same, but there will be multiple micro services connecting to the same db

Approach 2:-

Different micro service for different module and each micro service will have its own database. But, each module need user data( Table -> user).

Say, We are breaking into 4 modules. Admin module, Billing module, Appointment module, Inventory module

Here, For Ex:- Admin module, Appointment module & Billing module needs user data. For appointment module, certain appointment queries needs to be joined with user table which is there in other DB.

How to go with this?


Solution

  • First of all, you should consider if you really need a microservices architecture, because you are only going to get the benefits of this type of architecture if the microservices are very independent, I mean if there are very few connections between them.

    Said this, the first approach is totally inadvisable, because the key of a microservices architecture is that the microservices should be totally decoupled and if they share the same database + schemas + tables the changes needed for one of them are going to trigger changes for the rest, so you are going to have a distributed monolith (that have the worst of both worlds).

    There are two possible solutions. One of them is to call the user service every time you need user information, the bad part of it is that every time you need that information you will have to make a call so you are depending on the other service and the latency will grow.

    The other solution is to replicate the user table (only the part needed) on the other services that need this information and update it asynchronously using messages, For example, having a kafka or some other message queue and when there is any update on the users table the user service will publish a message in a specific topic and the other services will be subscribed to it, so they can update their user information accordingly.

    The problem here is the eventual consistency, I mean, the updates will not be immediately visible for the other services.

    As you can see, the two options have some drawbacks and both of them are because the communication between services, so the fewer communication between them the best for a microservices architecture