4
votes

I have two django REST API projects which I have decoupled them into micro services architecture, one of the services is an (SSO) that handles authentication (I'm using JWT token based authentication) and manage Users info and the other is a payroll service.

The problem is the user has a relation to some model in payroll service. To be specific I have an Employee class in payroll service which has a user_id field. This is where I will add a user UUID which I will get from querying the SSO service.

How do I share database across micro-services taking into consideration each service has its own database.

1
If you definitely need to access both databases from one service, Django does support configuration for multiple databases; is this not appropriate for your use case? Though it does seem to me that as you're using microservices it might be more appropriate to get whatever user data you need from an endpoint on the auth service rather than subverting the architecture by going straight to the DB. - kungphu
Sharing DB between services is generally considered as not a good option. But finally, it depends on the programmer. Following 2 articles gives a broad explanation of both the patterns. This would help you decide. Finally, if you want to share DB, Django supports configurations to multiple databases as mentioned by @kungphu microservices.io/patterns/data/database-per-service.html microservices.io/patterns/data/shared-database.html - Roopak A Nelliat

1 Answers

4
votes

Sharing database across bounded contexts is not advisable as each microservice should have the capability to make changes on how it persist data. Allowing multiple microservices to manage databases would lead you to a death star pitfall pattern

However, you might want to send a copy/updates of user data on the authentication context towards your payroll service. In this way, you can have independent data persistence strategies. One way to do this is to implement event emission strategy on your authentication context, this event emission strategy would be in-charge of broadcasting data changes made on the authentication context that subscribers residing from another bounded context can listen so that they can store a copy of your user data on their own persistence layers.