Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to design microservice database one to many?

i have a question about microservices and database design. Situation: I have a Service AircraftsService which handle all data about an aircraft, an aircraft type and so on. And i have a Service FlightsService which handle all data about flights. I have to link the aircraft to a flight. Normaly i would create a foreign key in the flights table that links the aircraft. But in microservice architecture i understand that i have domains and seperate databases. So my question is how can i represent this problem.

My first thought was that i have a database for all aircrafts and a database for all flights. the flights table becoms a 'indirect' foreign key to the aircraft. When i call the flight service the flight service call the aircraft service with the id from the flight table, then build the response. That promised that i do not call the aircraft database direct from the flight service.

Is my solution correct? What is the best practice in this use case?

After i thought about the problem, what is when i delete an aircraft i have to tell the flight service that an specified aircraft not exist any more and the flight service have to update the 'indirect' foreign key.

Can somebody help me?

Thanks

like image 715
Jan Kück Avatar asked Mar 21 '19 06:03

Jan Kück


People also ask

Can a microservice have multiple databases?

It means that we can use different database technologies for different microservices. So one service may use an SQL database and another one a NoSQL database. That's feature allows using the most efficient database depending on the service requirements and functionality.

How do I manage multiple databases in microservices?

Create a single database for different microservices is anti-pattern, then the correct way is to create a database for each microservice.

Can multiple microservices share the same database?

Even if microservices share a database, it is possible to configure a single database so that tables are separated by clearly defined, logical boundaries and owned by specific services. There are simple ways to enforce this separation, such as assigning database-specific roles and permissions to individual services.

Should each microservice have its own DB?

An important rule for microservices architecture is that each microservice must own its domain data and logic. Just as a full application owns its logic and data, so must each microservice own its logic and data under an autonomous lifecycle, with independent deployment per microservice.


1 Answers

There are various ways of best database practices when dealing in micro services , it may differ with respect to domain of the entities which are being used , and also the scope of your application use.

There are few best practices for database design in micor services , to start with listing few of them

1 - Private-tables-per-service – each service owns a set of tables that must only be accessed by that service
2 - Schema-per-service – each service has a database schema that’s private to that service
3 - Database-server-per-service – each service has it’s own database server.

You can mix and match these are per your data size and data count.

I would like you to refer and go through this page for a perfect example.

Microservices Database Best practices

Identify the load on each service that you will have. Load can be counted in 2 ways.

  • Data load (how much data will be retrived)
  • Service hit count (how many times service will be called)

Then you can decide the appraoch.

like image 119
Ashish Shetkar Avatar answered Oct 27 '22 01:10

Ashish Shetkar