Fault Tolerance (handle fault exception)
Hi,
Today I was jsut exploing on the implementation of Fault Tolerance in C# code as industry standard and here is my finding.
Some things are just out of your control. Sometimes the network is unreliable, the database is slow or someone else’s code fails. These errors are frustrating, and not easy to resolve. How can you retry a request to a remote service that is unreliable? What if you need to re-authenticate before retrying? You’ve probably seen homemade solutions like loops, try catches and if-else, but I’m here to show you there’s a better way.
To build resilient application and high availability - Make sure we have fault tolerance handled and also in terms of server level there should be multi zone hosted multiple server on load balancer. So here we will talk about fault tolerance.
I found two nu-get packages are available to handle this. One is Polly framework and the other one is Microsoft transient fault handling application block.
Polly is a resilience framework for .NET available as a .NET Standard Library so it can run on your web services, desktop apps, mobile apps and inside your containers—anywhere .NET can run. This allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner.
Retry : Lets you retry a failed request due to an exception or an unexpected or bad result returned from the called code. It doesn’t wait before retrying, so be careful.
Wait and Retry : The Wait and Retry policy lets you pause before retrying, a great feature for scenarios where all you need is a little time for the problem to resolve.
Fallback : The Fallback policy lets you return some default or perform an action like call other service. So like you want when let say ther are 3 failed count then instead of again retry, call other service or same some log or nofify user etc.
Circuit Breaker : Might be you would be interested to stop making requests to it for a moment because either of ("when a defined number of consecutive faults occur" or "when a threshold of faults occur within a time period"). The Circuit Breaker policy lets you do this.
Below are two links which having great explanation and examples as well.
https://blog.duijzer.com/posts/polly-refit/
https://www.pluralsight.com/blog/software-development/intro-to-polly
Categories/Tags: Fault Tolerance C#