At what level should i check for record existence in Microservices?

Viewed 99

Imagine I have two services A and B, and one of the entities of A has a relation with some entity in B. Where should I place the checking logic?

I considered two options

  1. Use Ocelot API Gateway to check for Record existence before inserting
  2. Use async messaging where I insert the record and add a consistency flag at service A. Service A will publish an event: CheckExistanceEvent and listen to the event: RecordExistEvent published by B

I think both of my solutions are bad and introduce coupling. I have looked for solutions but nothing was clear to me.

Any suggestion on where I should check for record existence?

1 Answers

The approach that will minimize coupling is for service B to publish events on creation (and deletion, if that exists) of its entities. Then any service (such as A) which needs to know whether a given entity owned by B exists can maintain its own view of which entities exist, regardless of whether B is up or not.

Related