Best practices for retries and error handling #244
-
We are migrating some of the older code to the newer version of the ws client apis, and while looking at the older implementation, I started wondering if all of the boilerplate we have around retries and redundancies still makes sense. Are there any best practices for graceful error handling, and how much of it is actually handled by the client. I wonder if you could shed some light on these topics, if you have time. I read through the examples for ws and deno, but didn't see anything specific that would answer my questions, and I also ran into this reply #193 (comment), which seems to briefly touch on the subject, but doesn't fully answer it. For context, we have a web app that relies on real-time data from NATS, and we want it to always be active: the ultimate question is when should we throw the towel, and tell the user that something gone horribly wrong.
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
@hypeJunction really it depends on the context on how you view the connection. For a service - you would want the connection to always retry to heal itself if there's a disconnect (network outages are expected) - this is how NATS connections in general work. On a web application possibly the same idea but only while a "session" is active. Now - a When a client For request reply operations, all pending requests will be rejected (you were disconnected, so you are very unlikely to get a response within the reconnect window - if you have requests that take a long time to resolve, likely you need to figure out a different way of staging a response, and then checking later for the results). An error for a subscription is terminal - these are typically permissions errors - you can subscribe today to JetStream is a different matter - here you are talking to a server holding messages for a consumer. If your consumer exists, you can resume processing messages once you reconnect - the consume apis will retry - and fetch/next will work when you next call them and you are connected. |
Beta Was this translation helpful? Give feedback.
-
@aricart Thank you so much for clarifying this. It is starting to make sense, and I see the beauty of this design. One last clarification: does JetStream consumer subscription behave similarly to a NATS subscription when it comes to errors? Is there any scenario where you want to explicitly resubscribe? |
Beta Was this translation helpful? Give feedback.
-
So it is possible that there could be an error on creating a subscription for jetstream, that error will telegraph. However, in the case of JetStream, the attitude for |
Beta Was this translation helpful? Give feedback.
@hypeJunction really it depends on the context on how you view the connection.
For a service - you would want the connection to always retry to heal itself if there's a disconnect (network outages are expected) - this is how NATS connections in general work. On a web application possibly the same idea but only while a "session" is active.
Now - a
closed
connection means that the only way to connect again is to call theconnect()
function - Any API usage on a closed connection will fail with an error. Note the difference - a disconnected state simply means that there's no connection now, but the client is actively retrying as per its connect configuration.When a client
disconnects
- the s…