How to define a retry policy for transient errors when publishing messages to RabbitMQ?

1.4k Views Asked by At

I'm using the RabbitMQ.Client nuget package to publish messages to rabbitmq from a .NET core 3.1 application. We are using the 5.1.0 version of the library.

We want to improve the resiliency of our application, so we are exploring the possibility to define a retry policy to be used when we send messages via the IModel.BasicPublish method. We are going to employ the Polly nuget package to define the retry policy.

Thw whole point of retry policies is retrying a failed operation when a failure deemed to be transient occurs. What I'm trying to understand is how to identify a transient error in this context.

Based on my understanding, all the exceptions thrown by the RabbitMQ.Client derives from the RabbitMQClientException custom exception. The point is that there are several exception types defined by the library which derives from RabbitMQClientException, see here for the full list.

I didn't find any specific documentation on that, but by reading the code on github it seems that the only custom exception thrown by the library when a message is published is AlreadyClosedException, this happens when the connection used to publish the message is actually closed. I don't think that retrying in this case makes sense: the connection is already closed, so there is no way to overcome the error by simply retrying the operation.

So my question is: what exception types should I handle in my Polly retry policy which I want to use to execute the IModel.BasicPublish call ? Put another way, which are the exception types representing transient errors thrown by IModel.BasicPublish?

0

There are 0 best solutions below