Search code examples
pythondjangodatabasedjango-rest-frameworkmicroservices

Sharing database relations across micro-services with Django rest framework


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.


Solution

  • 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.