I have API GATEWAY, user, role, mailer, user-role microservices.
I have a question which type of development use more in production code:
- I should send event to createUser from gateway to userMicroservice, then if response OK, i will send another request to roleMicroservice to get role. Then if OK i will send request to userRole microservice to assign role to user. if ok i will send event to mailerMicroservice to send user mail:
API GATEWAY - Dummy Code:
user = await userMicroservice - create user
if (OK) await roleMicroservice - get role for user
if (OK) await userRoleMicroservice - assign role to user (create relation)
if (OK) await mailerMicroservice - send mail to user
OR
- Just send createUser event to user microservice and do all logic in user microservice:
API GATEWAY:
user = await userMicroservice - create user
return user
User Microservice:
- create user
- get role (inject role microservice)
- assign role to user (inject user role microservice)
- send mail (inject mailer service)
return response
I understand what it's depends on project/requirements (tried chatGPT, and searched on internet) but i don't have much experiance with it and maybe one approach it's totally shit. Question for good backend developers.
Thanks:)
Let one microservice to does complex thing make it very hard to maintenance later. Each microservice must independent. Processing another microservices in this one makes them tightly coupled. I think let them do their task as a asynchronous manner is the best choice.