I am planning to build a project using micro-services architecture. I was curious to know which design would be better in terms of database?
If I keep separate database for each service, how do we decide where to keep the mapping tables?
For example, we have Customers service (separate project talking to DB 1) and Products service (separate project talking to DB 2), where should I store customer and product mapping (products bought by a customer)?
How about reporting in the longer run where I would need joins over a number of tables (that are in different databases due to Architecture mentioned in point 1)?
You can use a single shared database with tables that owned by different microservices if your data is heavily related. Also in case you have strong requirements on data consistency and availiblty of a service. There is pros and cons.
Pros
Reliability
Maintenance:
Features:
Cons
Reliability:
Maintenance:
Discipline:
Performance:
Hate:
Other thoughts:
In my personal expirience single database is a good choice if you're not a Netflix and your application can not tolerate any form of data loss.
The 2nd approach is an "Shared database" anti-pattern in the Microservices architecture.
In a micro-service architecture, it is preferable to use the Database per service pattern.
There are also advantages and disadvantages of these approaches in the links above at the end of the pages.
In response to your question about where to store the products purchased by the customer, you need to create a new microservice that will store the products purchased by the customer with its own database.
For Analytics and statistics, you need to send data from all databases to the reporting module. In other words, you need to build an etl process. A message broker, such as Apache Kafka, is usually used for this purpose. This page describes this approach well.
Building a micro service architecture is a very complex and extensive task, and it is associated with many problems and design patterns that allow you to minimize these problems. I recommend reading a book "Microservices patterns" that looks at various patterns of micro-service architecture design.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With