Search code examples
persistencemessage-queuenats-streaming-server

How to make a published queue automatically durable?


I am experimenting with Nats streaming server and it looks quite promising so far. However it appears queues can only be durable after a durable subscription has been created for it. This certainly makes sense however how does it work in practice in a microservices architecture?

For instance assume you are publishing services and Service1 is pumping messages out to a queue that is not yet durable and has no listeners. Some time later that corresponding service starts and makes that queue durable. Do you just deal with this hopefully short loss or ensure the later service is started first?


Solution

  • Sorry for the delay. In NATS Streaming, any message published to a channel are stored, regardless of subscription interest. You can experiment and publish say 3 messages on "foo". Then, you can start a subscription (even non durable) and replay those messages. It is just a matter of specifying the starting point of the subscription. For instance, there is an option to have deliver "all available". Using the Go nats samples, it would be:

    $ go run examples/stan-pub/main.go foo msg1
    Published [foo] : 'msg1'
    $ go run examples/stan-pub/main.go foo msg2
    Published [foo] : 'msg2'
    $ go run examples/stan-pub/main.go foo msg3
    Published [foo] : 'msg3'
    
    $ go run examples/stan-sub/main.go -id "me" -all foo
    Connected to nats://127.0.0.1:4222 clusterID: [test-cluster] clientID: [me]
    Listening on [foo], clientID=[me], qgroup=[] durable=[]
    [#1] Received: sequence:1 subject:"foo" data:"msg1" timestamp:1583947471103854000 
    [#2] Received: sequence:2 subject:"foo" data:"msg2" timestamp:1583947472684693000 
    [#3] Received: sequence:3 subject:"foo" data:"msg3" timestamp:1583947473990567000