Need some clarification on .net connection object in c#, slow consumers & Flow Control #704
-
Question1) Ive been using NatsConnection object for opening connections & also for publish subscribe & even creating jetstream contexts. Does using NatsClient have any advantages over NatsConnection ? Question 2) I tried this example https://docs.nats.io/using-nats/developer/connecting/events/slow#detect-a-slow-consumer-and-check-for-dropped-messages, SubQn2) In my code for subscription, i do not provide any NatsSubChannelOpts, & i also use queue groups to make sure message is distributed only to one subscriber. till now ive not observed slow consumer message but do i need to take any precautions?
SubQn3) Lets assume slow consumer is detected, server canceling the subscription is quite scary, on client if we have the MessageDroppedCallback, what is recommended to ensure server does not cancel subscription? As per nats-io/nats.go#888 using MaxAckPending helps, wanted some more clarity on this. SubQn4) If a subscription of CoreNATS is consuming but no messages are flowing yet, will server treat this as inactive & close it? SubQn5 )Also As per https://stackoverflow.com/questions/70550060/performance-of-nats-jetstream to avoid overloading broker with publish calls, he states "You have two options to flow-control your JetStream async publications: specify a max number of in-flight asynchronous publication requests as an option when obtaining your JetStream context: i.e. js = nc.JetStream(nats.PublishAsyncMaxPending(100))" How do i do this in c# client?
Question 3) As per https://natsbyexample.com/examples/jetstream/pull-consumer/csharp/ & https://nats-io.github.io/nats.net/documentation/jetstream/consume.html ConsumeAsync is the most performant of the consume methods due to real time streaming like behaviour. So does this mean FetchAsync can be ignored & we can assume to improve throughputs of our system its better to use ConsumeAsync? Also the above documents show lot of flowcontrol techniques for FetchAsync, how to handle flow control in consumeAsync? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 6 replies
-
@mtmk can you pls help answer this |
Beta Was this translation helpful? Give feedback.
-
Only advantage is it comes with better defaults for serialization and message channel for ease of use.
It's a NATS Core concept.
If you are dropping messages when the subscription channel is full (NatsOpts setting) then you should also subscribe to the message dropped callback event.
If you see messages being dropped you should slowdown or stop other consumers and subscribers for some time to give the system some space.
No, connection would be kept alive with PINGs and the consumer also have heartbeats.
You can use the concurrent publish method on JetStream context.
You should use ConsumeAsync in general which keeps the messages flowing with minimal overhead. FetchAsync is more for potentially for use cases like application wakes up and needs to consume some data and goes back to sleep for example.
You can use the options to adjust thresholds and number of messages in batches for example. |
Beta Was this translation helpful? Give feedback.
-
@mtmk thank you very much for your response, much appreciated. I am unable to find any example of using PublishConcurrentAsync for flow control, can you pls help with this? |
Beta Was this translation helpful? Give feedback.
-
@mtmk i have added message drop handler on the connection to detect slow consumers, any action should we take after detecting slow consumer so that server does not close it? |
Beta Was this translation helpful? Give feedback.
-
@mtmk @Jarema if slow consumers is a feature of core nats only, i am still observing slow consumers in my k8s server & server is disconnecting the consumer completely where all consumers are jetstream consumers only. qn1) Slow Consumer Detected: WriteDeadline of 10s exceeded with 2 chunks of 939 total bytes. Now i understand that write deadline is configured to 10sec here, am i correct to assume because write deadline got exceeded the server closed this connection & thats why even though i am using only jetstream consumers i am facing this issue? Question2) In my code i follow a single broker connection per application pattern, so lets assume one application has 20 consumers on same connection & out of this only 1 consumer is detected to be slow, it does not make any sense to close the entire connection itself right? Why is this happening? I can see multiple ConnectionDropped & Reconnection handlers being hit in client? Requesting you to pls help me with this.... |
Beta Was this translation helpful? Give feedback.
no worries, I hope it helps. Let me dig it out. I think there is one in another discussion.
edit: #523 (comment)