microservice-template icon indicating copy to clipboard operation
microservice-template copied to clipboard

Add separate databases for reading and writing.

Open AdamOleszko opened this issue 4 years ago • 0 comments

Feature Request

Describe the solution you'd like

Add separate databases for reading and writing. In the template, both databases can use PostgreSQL (it can be changed in a particular microservice, depending on specific needs). To do:

  1. Add a new Postgres instance to docker-compose
  2. Create two separate repositories and entities (one for reading and one for writing)
  3. Differ data in these databases (just to show the example)
  4. Keep databases synchronized using events

Teachability, Documentation, Adoption, Migration Strategy

Database for commands should as it is now. The new database for queries should be used for all "GET" operations. In addition, it is important to keep this DB synchronized with the commands database. For this purpose, we need to handle proper events and update this database asynchronously, in the background.

What is the motivation / use case for changing the behavior?

In many cases, we will need to get complicated data from many microservices in a single query. To avoid making "in memory joins" in the API gateway that are inefficient we decided to use CQRS pattern. Separate databases are one of the main points of this pattern. Thanks to that we will store data similar as much as possible to these ones fetched by clients.

AdamOleszko avatar Feb 22 '21 23:02 AdamOleszko