Peter Morris
Peter Morris

Reputation: 23224

Microservice cross-db referencial integrity

We have a database that manages codes, such as a list of valid currencies, a list of country codes, etc (hereinafter known as CodesDB).

We also have multiple microservices that in a monolithic app + database would have foreign key constraints to rows in tables in the CodesDB.

When a microservice receives a request to modify data, what are my options for ensuring the codes passed in the request are valid?

I am currently leaning towards having the CodesDB microservice post an event onto a service bus announcing when a code is added or modified - and then each other microservice interested in that type of code (country / currency / etc) can then issue an API request to the CodeDB microservice to grab the state it needs and reflect the changes in its own local DB. That way we get referential integrity within each microservice DB.

Is this the correct approach? Are there any other recommended approaches?

Upvotes: 0

Views: 95

Answers (1)

asolanki
asolanki

Reputation: 1373

Asynchronous event based notification is a pattern commonly used in micro services world for ensuring eventual consistency. Depending on how strict your consistency requirement are you may have to ensure additional checks.

Another possible approach could be to use

Read only data stores using materialized view. This is a form of CQRS pattern where data from multiple services is stored in a de-normalized form in read only data store. The data gets updated asynchronously using the approach mentioned above. The consumers gets fast access to data without having to query multiple services

Caching - You could also possibly use distributed or replicated depending on your performance or consistency requirements.

Upvotes: 2

Related Questions