Handling Errors in Microservice Architecture and Asynchronous Message Queue

apache-kafkamessage-queuemicroservices

When inter-communicating through http requests between Microservices it is quite easy to reply with error messages to users, you can just catch the error of a different Service and forward it to the user.
But how does Error handling work when communicating through a message queue?
For example, a user makes a request to an Auth Service, that Auth Service publishes a message indicating that a new user needs to be created. The User service consumes this message and tries to create a new User. But what if the User Service throws an error, maybe the Email is already taken. How do we notify the user that his request failed?

My guess would be that it is not possible to respond to the user with this error because the initial request to the Auth Server is already closed after publishing the message to the queue. Might this be a better case for synchronous communication?

Example Flow:

client -> Auth Service                              User Service
                   |                                       ↑  |
                   | create User               create User |  | Fails to create User
                   |                                       |  |
                   ↓                                       |  ↓
           ----------------------Async Queue----------------------

Best Answer

In general, you have two options:

The first is sending a message back. The user service knows that the auth service sent the request, so will complete the loop by reporting the error (or success).

The second option is having a common DB next to the message queue. That common DB stores info about jobs including timing, status, and error information.

The first option scales better and tends to be more fault tolerant, but is harder to debug, visualize, and coordinate.

Related Topic