0
votes

As I heard most of the time that in micro services architecture, for every single micro service we have to create individual database.

But if I have to maintain foreign key constraint across the different databases which is not possible. Like I have a user table in authentication micro service and I want to use it in my catalog service(userid column from user table)

So how can it be resolve.

Thanks in Advance

2

2 Answers

1
votes

You can maintain a shadow copy (with only useful information for eg. just the userid column) of user table in catalog service via event sourcing(for e.g. you can use rabbit MQ or apache kafka for async messaging).

Catalog service will use the user information in read only mode. This solution is however effective only when user information doesn't change frequently. Otherwise async communication can be inefficient and costly. In that case you can implement API calls from catalog service to user service for any validations to be done on user data.

0
votes

Use the Saga Pattern to maintain data consistency across services.

A saga is a sequence of local transactions. Each local transaction updates the database and publishes a message or event to trigger the next local transaction in the saga. If a local transaction fails because it violates a business rule then the saga executes a series of compensating transactions that undo the changes that were made by the preceding local transactions.