Search code examples
spring-bootmicroservicesdistributed-database

Distributed database design style for microservice-oriented architecture


I am trying to convert one monolithic application into micro service oriented architecture style. Back end I am using spring , spring boot frameworks for development. Front-end I am using angular 2. And also using PostgreSQL as database.

Here my confusion is that, when I am designing my databases as distributed, according to functionalities it may contain 5 databases. Means I am designing according to vertical partition. Then I am thinking to implement inter-microservice communication services to achieve the entire functionality.

The other way I am thinking that to horizontally partition the current structure. So my domain is based on some educational university. So half of university go under one DB and remaining will go under another DB. And deploy services according to Two region (two for two set of university).

Currently I am decided to continue with the last mentioned approach. I am new to these types of tasks, since it referring some architecture task. Also I am beginner to this microservice and distributed database world. Would someone confirm that my approach will give solution to my issue? Can I continue with my second approach - horizontal partitioning of databases according to domain object?


Solution

  • Can I continue with my second approach - Horizontal partitioning of databases according to domain object?

    Temporarily yes, if based on that you are able to scale your current system to meet your needs.

    Now lets think about why on the first place you want to move to Microserices as a development style.

    1. Small Components - easier to manager
    2. Independently Deployable - Continous Delivery
    3. Multiple Languages
    4. The code is organized around business capabilities
    5. and .....

    When moving to Microservices, you should not have multiple services reading directly from each other databases, which will make them tightly coupled.

    One service should be completely ignorant on how the other service designed its internal structure.

    Now if you want to move towards microservices and take complete advantage of that, you should have vertical partition as you say and services talk to each other.

    Also while moving towards microservices your will get lots and lots of other problems. I tried compiling on how one should start on microservices on this link .

    How to separate services which are reading data from same table:

    Now lets first create a dummy example: we have three services Order , Shipping , Customer all are three different microservices.

    Following are the ways in which multiple services require data from same table:

    1. Service one needs to read data from other service for things like validation.

    Order and shipping service might need some data from customer service to complete their operation.

    Eg: While placing a order one will call Order Service API with customer id , now as Order Service might need to validate whether its a valid customer or not.

    One approach Database level exposure -- not recommened -- use the same customer table -- which binds order service to customer service Impl

    Another approach, Call another service to get data

    Variation - 1 Call Customer service to check whether customer exists and get some customer data like name , and save this in order service

    Variation - 2 do not validate while placing the order, on OrderPlaced event check in async from Customer Service and validate and update state of order if required

    I recommend Call another service to get data based on the consistency you want.

    1. In some use cases you want a single transaction between data from multiple services.

    For eg: Delete a customer. you might want that all order of the customer also should get deleted.

    In this case you need to deal with eventual consistency, service one will raise an event and then service 2 will react accordingly.

    Now if this answers your question than ok, else specify in what kind of scenario multiple service require to call another service.

    If still not solved, you could email me on [email protected], will answer you