3

I'm developing microservices for a project and we're experimenting with pub-sub communication using AWS SNS+SQS. We're unsure how to signal to services whether or not other services have successfully completed tasks or not.
For example, if service A emits an SNS Event and service D, E, and F all are listening to the subscribed SQS Queue, how does service A know if the activities kicked off by service A inside of service D, E, and F were successful?

I'll give a more concrete example: A new user registers for a website. This network call first reaches the user service in the backend. If the user was successful, it sends off an event saying a new user was created. That triggers the email service to send an email to for the user to confirm his registration. What happens if it fails to send an email? Has the user service:

1) Already responded to the frontend saying it was successful

2) Or is it waiting for a confirmation? What is a good pub-sub pattern for confirmations?

I know we could have just done a synchronous call, but this example is simplified for brevity's sake.

NateW
  • 2,856
  • 5
  • 26
  • 46
  • 1
    If you've started with events, why not continue with them? Have the services publish confirmation events that the original service listens for. – dbugger Apr 25 '16 at 16:01
  • @dbugger if it's waiting for the queue for everything to be a success, does that mean that `service A` is just waiting on the same thread and the frontend is also waiting for a response from `service A`? – NateW Apr 25 '16 at 16:47
  • 2
    that's up to you, but my inclination would be not leave the front end waiting. Respond to the front end that the request has been received. Have service A wait for its responses. When it receives its confirmations it can send confirmation to the front end. If it fails to receive them within a timeout period, it could a) retry, or b) tell the front end that there was an error. – dbugger Apr 25 '16 at 17:05
  • Gotcha. Concerning b), if the frontend user has already received a message that it was a success (even though that isn't certain), and the user moves to a different screen, how does the frontend listen for a possible message that the email failed to send? Using SNS or some push message service? – NateW Apr 25 '16 at 20:04

1 Answers1

2

As @dbugger says in the comments, you can have the subscribers send an ack or something back the publisher.

However, it is the publisher's responsibility to ensure the events were received by the subscribers?

Kind of the point of publishing events is that the publisher doesn't (and shouldn't) need to know about the state of consumers subscribing to the event, whether the subscribers ignored the message, or even if there are no subscribers.

If the publisher does need to know, then rather than an event, the publisher should be sending a command direct to the consumer in a request-response pattern, rather than an event.

This is because a command message assumes knowledge of the recipient whereas an event message assumes no knowledge.

In terms of knowing from a top-down perspective when an event has arrived: well, you should be using a durable message transport which can guarantee delivery of your event, but even with durability you can still drop messages.

The only real way of doing it is to implement some kind of instrumentation which allows you to track the "conversations" which are encoded in the events being published from place to place. There are tools available for this (I have only used one, for NServicebus called ServicePulse).

tom redfern
  • 30,562
  • 14
  • 91
  • 126
  • 1
    Actually, it would be ServicePulse that provides the monitoring to see if a message has failed :) – Udi Dahan Apr 26 '16 at 09:57
  • @TomRedfern are you actually able to send an ack back to the publisher or just to the message broker software like RabbitMQ? – NateW May 02 '16 at 20:39
  • I now understand that you delete a message from the queue if its successful and that's the way to ensure a message was successfully processed, but I'm still curious if it's possible to confirm to the publisher that the message was successful. – NateW May 02 '16 at 20:40
  • 1
    @NateW - it's not always possible - some messaging protocols eg amqp and mtqp support duplex communication patterns. IIRC rabbit is just an implementaiton of amqp written in erlang. Many messaging platforms don't support either protocol, and are just dumb pipes. For such platforms, this capability would need to be built on top. – tom redfern May 02 '16 at 21:19