Microservices Database – Database Per Service Pattern Explained

databasedatabase-designmicroservicesrest

I'm trying to understand the database per service pattern in microservices architecture.

Say we have a restaurant reservation application composed of the following microservices:

  • User
  • Search
  • Restaurant
  • Reservation
  • Search
  • Notification

If a user wants to create a reservation, the UI would perform a POST request to /api/reservation with the data in the request body.

Would the Reservation database be required to store redundant data from the User database (user_id, username, first name, last name)?

Or would it only store a user_id reference owned the User database?

Best Answer

Reservation service has no reasons to know the first or last name of the user, nor his age, sex or e-mail address or phone number. What the reservation service needs to know is the link to a given person reserving the table, and that would be the user's unique identifier.

Later, when a reservation needs to be displayed, with the associated person's first and last name and probably the phone number (to be able to contact the person in a case of a problem), the application displaying this information will do two calls to two services: reservation service, and, from there, the user service (specifying the identifier of the user).

Microservices architecture is not an excuse to duplicate data. If you do that, you'll quickly create inconsistencies, which are, given the distributed nature, very difficult to get rid of. So store references, not duplicated data.

Related Topic