nats-io / stan.go

NATS Streaming System
https://nats.io
Apache License 2.0
706 stars 117 forks source link

Question: What is the correct/idiomatic way to use durable group subscriptions on multiple subjects? #345

Closed ibraimgm closed 3 years ago

ibraimgm commented 3 years ago

I need to subscribe to 3 different subjects in the same application, all of the subscriptions should be durable.

Is it enough to just call QueueSubscriber with the same group (multiple replicas of the application) and same durable name? For example (note that only the subject and the handler changes):

sub1, _ := sc.QueueSubscribe("subject1", "my-app", qcb1, stan.DurableName("dur")) 
sub2, _ := sc.QueueSubscribe("subject2", "my-app", qcb2, stan.DurableName("dur"))
sub3, _ := sc.QueueSubscribe("subject3", "my-app", qcb3, stan.DurableName("dur"))

Is this correct? Or should the queue+durablename be unique for each subject?

Also, assuming I don't want to miss any messages, will I ever need to close those subscriptions? for what I understood, once the subscription is closed, everything is lost...

kozlovic commented 3 years ago

Depends what you want to do. A queue group allows to distribute the same message to 1 of the member of the group as opposed to all subscribers to a given subject. What you are doing here is create 3 different groups since the subject is different, so this application would receive messages on all 3 subjects. If there is another instance of the same application running, then a message on "subject1" would go to only one instance of the applications. If this is what you want, then yes, your use of queue subscription is correct. If you want all instances running the above code to receive a given message on a given subject, then you should not use QueueSubscribe, but just Subscribe.

You can (and should) call sub.Close() when your application is done receiving. This will not remove the durable interest in the server. To do so, you would need to call sub.Unsubscribe().

Here is more information on various type of subscriptions in NATS Streaming: https://docs.nats.io/nats-streaming-concepts/channels/subscriptions

ibraimgm commented 3 years ago

Thanks for the quick reply @kozlovic , now I understand it.