C#CommerceSitecoreSoftware DesignWeb API

Transient fault handling in Sitecore Commerce

Transient fault handling can be cumbersome and lead developers to write repetitive code. There could be many situations when you want your code to be retried in case of failures/exceptions etc. While working with a few external system integrations, we had to implement a retry mechanism for certain transactional requests. Payment integrations are one of the examples that I am using here.

Polly is a library that allows developers to express resilience and transient fault handling policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner.

Almost all the payment providers we integrated to Sitecore Commerce project e.g. Braintree/DiectBank/Openpay recommend to retry the transactional requests in case of timeouts or internet issues.

For example when we request Braintree payment gateway to settle the payment if the request times out you have to retry the same request 3 times before you treat it as a failure.

Retry

Since the retry could be handled as cross-cutting, we decided to write some code to do automatic retries for. However, with a little research, we came across this API called Polly. Polly can help you manage transient faults like a charm.

We installed Polly nuget package in our project and started using it. Polly has a Policy class which can be used to configure the retry policy e.g. Retry should only occur for HttpRequestException etc. Since Policy class has static functions to work, we decided to create a wrapper around it to make it injectable. We called this wrapper RetryPayment and it implements IRetryPayment.

 

While the implementation looks like

And yes, that easy it is. 5 * retryAttempt is interesting here. First retry will be made with a delay of 5secs wait while 2nd with 10secs and so on.

IRetryPayment is injectable

Then we injected this in SettlePaymentBlock

And while executing the request

Happy coding!!!!!

Leave a Reply

Your email address will not be published. Required fields are marked *