We have two microservices a person service and a resource service. The person services stores the general person details, name, address contact information etc. The resource service would store details about the person if they have a role of Teacher, eg. what subjects they can teach, along with their name.
When creating a person you could create them as a teacher and therefore the person service will store the main person details and the resource service will store the teacher information. We're thinking this should be a Saga then as we'd be storing information about the person in two different services? Would this be correct?
Secondly, if that is correct, when the saga completes what message or messages should go on the message bus to say Person has been created and they have teacher details too? Should there be one message that says PersonCreated with all the details of the person and the teacher? Or two messages one of PersonCreated with those details and another TeacherCreated which is the resources view of the Teacher?
Before answering, it is necessary to understand two things:
For your first question, yes! Saga may be used in this case. Now, regarding your second question, it is important to understand the event creation structure. A triggered event should contain enough information for its listeners to interpret it according to their needs.
In practice, the
Personmicroservice is the entry point for creating a new person. So, you have two options for communicating with theResourcemicroservice:Personmicroservice will emit thePersonCreatedevent containing all the necessary information for theResourcemicroservice to interpret (such as job role, job metadata, etc.), as well as for all other listeners (person ID, name, address, etc.). TheResourcemicroservice will read this event and determine what needs to be done based on the payload;Resourcemicroservice.POST /person/:id/resources) without working with events.⚠️ It is also important to note that Sagas have a dependency relationship. In other words, if the
Resourcemicroservice fails to process thePersonCreatedevent, it should emit an event to have the previously created Person removed.