4

I'm trying to make a service architecture which includes two Node.js apps which shares the same database. The overall service architecture looks like below (simplified version)

enter image description here

I'm planning to use Sequelize as an ORM to access the database. As far as I know, if a service uses Sequelize, it needs model to get the structure of data tables. In my case, api and service will access the same database, which means they should share the same Sequelize model.

So here is the question: where should I locate the common Sequelize relevant files? It seems I have two choices:

  1. put them on the upper common location (assuming the project structure is monorepo) so that each apps can use the single same files
  2. maintain copies of files in each apps' project folders. In this case, each apps will be independent(Let's say I want to dockerize each apps) but in case the Sequelize files modified, the same action should be done for the other.

I'm not sure how I understood is correct. Is my question valid? If so, what is the better choice and practice? I appreciate for your answers in advance.

2
  • 1
    Here I am suggesting you check the communication between microservices, Also don't go with your second method (It will kill your developing time in future). Commented Jul 28, 2020 at 7:08
  • 1
    Totally agree with @BINFAS K Maybe you should give more details and put an answer. Commented Jul 28, 2020 at 9:18

1 Answer 1

2

There is no correct answer, it depends on the specific situation, but sharing a database between multiple microservices is a bad design.

Sharing a database means tight coupling at the data level. The direct consequence is that when a service modifies the database table structure, such as deleting the name field of the user table, it may break the APIs of other services and all use the sequelize user model. All services need to update the model definition and modify the implementation code of the API.

If all of your services are maintained by a team, I suggest you choose the first solution, which costs less and is easier to maintain. If your services are maintained by different teams, the two solutions are actually similar, because as long as the table structure is modified, the application layer model needs to be modified or verified whether it still works well.

Therefore, I recommend following the best practices of microservice architecture, first splitting the database vertically according to the business model, and building application APIs on top of it.

Core principles of microservices:

  • loose coupling
  • high cohesion
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.