Can I keep a copy of a table of one database in another database in a microservice architecture?

Viewed 379

I am currently new to microservice architecture so thanks in advance.

I have two different services a User Service and a Footballer Service each having their individual databases.(User database and Footballer database). The Footballer service has a database with a single table storing footballer informations. The User service has a database which stores User details along with other user related data.

Now a User can add footballers to their team by querying the Footballer service and I need to store them somewhere in order to be displayed later. Currently I'm storing the footballers for each user in a table in the User database whereby I make a call to the Footballer service to give me the details of a specific footballer by ID and save them in the USer database by mapping against the USer ID.

So is this a good idea to do that and by any chance does it mean im replicating data between two services and if it is than what other ways can i achieve the same functionality ?

2 Answers

Currently I'm storing the footballers for each user in a table in the User database whereby I make a call to the Footballer service to give me the details of a specific footballer by ID and save them in the USer database by mapping against the USer ID.

"Caching" is a fairly common pattern. From the perspective of the User microservice, the data from Footballer is just another input which you might save or not. If you are caching, you'll usually want to have some sort of timestamp/version on the cached data.

Caching identifiers is pretty normal - we often need some kind of correlation identifier to connect data in two different places.

If you find yourself using Footballer data in your User domain logic (that is to say, the way that User changes depends on the Footballer data available)... that's more suspicious, and may indicate that your boundaries are incorrectly drawn / some of your capabilities are in the wrong place.

If you are expecting the User Service to be autonomous - that is to say, to be able to continue serving its purpose even when Footballer is out of service, then your code needs to be able to work from cached copies of the data from Footballer and/or be able to suspend some parts of its work until fresh copies of that data are available.

People usually follow DDD (Domain driven design) in case of micro-services : So here in your case there are two domains i.e. 2 services :

  1. Users
  2. Footballers

So, user service should only do user specific tasks, it should not be concerned about footballer's data.

Hence, according to DDD, the footballers that are linked to the user should be stored in football service.

Replicating the ID wouldn't be considered replication in case of microservices architecture.

Related