Microservices – Common Data in Microservice Architecture

microservices

We are trying to develop a microservices based application. The idea is to come up with a good pattern and would ideally like to develop it so that each service is loosely coupled with its own database. But how would we handle data that is common across multiple services.

Let's consider an example of country and state information required across multiple services.

We came up with a few options here:

  1. Replicate the tables in each service, with a country management service separately for managing country data, and each other service subscribing for any updates (but here the data is replicated across multiple services)
  2. A Common database for country data (but that could potentially become a single point of failure)
  3. Single country management service and other services accessing the country data through this

Which one would be the ideal choice to implement this? Or is there a better way of handling this scenario.

Best Answer

Data is not a service. Access to data may be a service. Processing data definitely is. You should ask yourself: what does my system do with that data? What is the service interface that my domain components require to access in order to use the country data? And then:

  • Do all my components agree on the syntax of that interface?
  • Do all my components agree on a particular implementation of that interface? Most trivial example: if your interface offers country name resolution, does every component agree that the country with the numerical ISO code 840 is spelled "United States of America", or do some components require "USA"?
  • Do all my components agree on the semantics of that interface? When countries 278 and 280 merge to form 276, do all your components agree with that? Or do some ignore 276 and require all former 278 addresses to be listed in 280 now?
  • Do all my components agree on the lifecycle of the underlying data? For example, does the name resolution represent the current name, subject to change when country 807 changes its name from "F.Y.R.M." to "North Macedonia" or do some disagree, because "addresses are free text anyway, so don't mess with them"

There are more questions like that, and they all boil down to: Are your countries true entities, or do they exist mostly in the imagination of all the components of your system?

The more questions you answer with yes, the more you should lean towards 3, the more questions you answer with no, the more you should lean towards 1.

Related Topic