Sandeep
Sandeep

Reputation: 776

GO lang NATS Queueing with multiple Queue Subscribe

I am creating NATS go lang Queue Subscriber client as follows,

nc.QueueSubscribe("foo", "my_queue", func(msg *nats.Msg) {
        log.Printf("Message :%s", string(msg.Data))
})

So whenever i publish any message to "foo" subject then some time it is receiving and some time not.

e.g let say i sent 10 messages to above "foo" subject then it will receive 2 or 3 max.

My requirement is as follows,

Any help appreciated.

Upvotes: 2

Views: 2520

Answers (1)

I. Kozlovic
I. Kozlovic

Reputation: 862

If you start multiple queue subscribers with the same name (in your example my_queue), then a message published on "foo" goes to only one of those queue subscribers.

I am not sure from your statement if you imply that the queue subscriber sometimes misses messages or not. Keep in mind one thing: there is no persistence in NATS (there is in NATS Streaming). So if you publish messages before the subscriber is created, and if there is no other subscriber on that subject, the messages will be lost.

If you were experimenting and starting the queue subscriber from one connection and then in the same application sending messages from another connection, it is possible that the server did not register the queue subscription before it started to receive messages (again, if you were using 2 connections). If that is the case, you would need to flush the connection after creating the subscription and before starting sending: nc.Flush().

Finally, there is nothing special to use queue subscribers in concurrent mode. This is what they are for: load balancing processing of messages on the same subject for subscribers belonging to the same group. The only thing you have to be careful of if you are creating multiple queue subscribers in the same application is either to not share the message handler or if you do, you need to use locking since the message handler would be concurrently invoked if messages arrive fast enough.

Upvotes: 6

Related Questions