1

Short story: how do I relate two entities in different databases? In microservices each service has its own database, how are the entities in different databases related to each other in a microservice architecture?

Long story: I am writing two separate but related applications, lets say a management system for used vehicle sellers and an accounts and finance system. I want my applications to work with each other and independently as well.

Now lets suppose a vehicle deal is finalized in the management system then from the finalized deal data I would generate an invoice in the finance system. Similarly if the deal is getting deleted then I would have to delete the corresponding invoice as well but to delete the corresponding invoice I want to somehow relate the deal and the invoice but I cant simply make a relation between the entities because I want to my applications to be completely independent which means different databases as well. Or is there a way to make a relation between two tables in different databases? Is it even a good idea to make relation two tables of different databases?

Furthermore, I want two way communication, for example if a deal is edited then its related invoice gets edited as well and if an invoice is edited then its related deal gets edited as well also just like microservices I want both the systems to be able to run independently and alongside each other as well.

Possible solution: Only solution I can think of right now is to have a single database and two separate application layers but this design is no good itself as I have used this approach before and it has a lot of its own drawbacks.

Technology Stack: My technology stack is .Net and MSSQL

I am looking for answers from people who have designed this kind of a system or have faced similar problems before

Fakhar Ahmad Rasul
  • 1,595
  • 1
  • 19
  • 37

1 Answers1

1

Use uuids for identifying your entities and reference those as a simple Strings.

See How can I generate UUID in C#

For example:

{
  "Name" : "MyDeal",
  "Id" : "6a0e5244-2a85-4944-9be4-04c8cfbf3e45"
}

{
  "Name" : "MyInvoice",
  "Id" : "efed8b94-0065-4004-adcc-9f2d219fb6eb",
  "DealReference" : "6a0e5244-2a85-4944-9be4-04c8cfbf3e45"
}

Do not use common persistence, such as a common database. See https://stackoverflow.com/a/43618284/7803650.

Oswin Noetzelmann
  • 9,166
  • 1
  • 33
  • 46
  • Thank you for the answer, any suggestions on how I can achieve two way communication between the applications? – Fakhar Ahmad Rasul Jan 23 '19 at 18:03
  • Create a service API for the functionality that is required to expose, for example a REST API. When you say two-way you'd have to be careful to cut your domain problem correctly. Most of the times it is possible to get away with one way communication if you cut the domain problem right. If invoice and deal are that tightly related maybe they belong to the same service. An alternative could be to use an event driven approach, where you have both services listen to events from each other on a common event queue, e.g. implemented with Kafka. That way your API is really the event definitions. – Oswin Noetzelmann Jan 23 '19 at 21:15
  • I want two way communication because I want to design this in such a way that I am able to implement the systems independently and with each other as well. Maybe a customer only wants the management system, maybe another customer wants both the systems so I want both my systems to be flexible enough to work with each other and without each other as well. – Fakhar Ahmad Rasul Jan 25 '19 at 07:27
  • Yes I am thinking about an event driven design myself and looking into RabbitMQ and Kafka. Thanks for the help – Fakhar Ahmad Rasul Jan 25 '19 at 07:28