nats-io / nats-architecture-and-design

Architecture and Design Docs
Apache License 2.0
177 stars 20 forks source link

Receive responses from multiple subscribers for a single request #215

Closed scottf closed 6 months ago

scottf commented 1 year ago

One of the most common questions we get is how to do request-reply but get response from all subscribers, not just the first one. Currently the way to do it is publish a message with a reply-to and at the same time to subscribe to the reply-to address.

Since this is such a common ask, we should consider how to add it to the clients.

ripienaar commented 11 months ago

Cli has --replies for this purpose, so will unassign myself.

aricart commented 11 months ago

The javascript client handles this via the requestMany() apis.

tbeets commented 10 months ago

Agree @scottf. Although easy for developer to hand-roll, scatter-gather comes up often enough that a library-provided facility like @aricart describes makes sense.

I think the usual push back is useful options for "get response from all subscribers", i.e. timer based, count based (with expiry), etc. What tactic used for requestMany() @aricart ?

wallyqs commented 10 months ago

Also need to consider using the single resp mux to keep track of responses as an alternative to creating an inbox subscription for the batch of requests.

ColinSullivan1 commented 10 months ago

Adding a convenience API would be nice - # of responses to expect, timeout, and return an array/list/slice of messages. This is a very common ask...

aricart commented 10 months ago

@tbeets you have several strategies:

The jitter is the secret sauce, when you specify that, as long as messages are arriving, the jitter is reset (100ms or so). So this means you make a request with a 2s timeout, but as soon as you get one response, the jitter timer takes over and is reset with each new message. If no messages arrive within the jitter, then the request is done. With jitter most count/expected type things work correctly without having an absolute stop timer

Sentinel, is empty payload

The API works with both mux and no mux inboxes

aricart commented 10 months ago

https://nats-io.github.io/nats.deno/interfaces/NatsConnection.html#requestMany

scottf commented 6 months ago

Closing as being addressed with ADR