How does data denormalization work with the Microservice Pattern?

前端 未结 4 461
后悔当初
后悔当初 2021-01-29 19:16

I just read an article on Microservices and PaaS Architecture. In that article, about a third of the way down, the author states (under Denormalize like Crazy):

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-29 19:29

    It is indeed one of key problems in microservices which is quite conviniently omitted in most of articles. Fortunatelly there are solutions for this. As a basis for discussion let's have tables which you have provided in the question. Image above shows how tables will look like in monolith. Just few tables with joins.


    To refactor this to microservices we can use few strategies:

    Api Join

    In this strategy foreign keys between microservices are broken and microservice exposes an endpoint which mimics this key. For example: Product microservice will expose findProductById endpoint. Order microservice can use this endpoint instead of join.

    It has an obvious downside. It is slower.

    Read only views

    In the second solution you can create copy of the table in the second database. Copy is read only. Each microservice can use mutable operations on its read/write tables. When it comes to read only tables which are copied from other databases they can (obviously) use only reads

    High performance read

    It is possible to achieve high performance read by introducing solutions such as redis/memcached on top of read only view solution. Both sides of join should be copied to flat structure optimized for reading. You can introduce completely new stateless microservice which can be used for reading from this storage. While it seems like a lot of hassle it is worth to note that it will have higher performance than monolithic solution on top of relational database.


    There are few possible solutions. Ones which are simplest in implementation have lowest performance. High performance solutions will take few weeks to implement.

提交回复
热议问题