Skip to main content

Azure Design patterns: Self-Healing & Transient fault handling

I will start a series of posts about the core design patterns that you need to take into consideration when you start to work with Microsoft Azure and the cloud in general.
These principles are important in a cloud application, even if most of them are known from classical on-premises development. Even if most of them are known, we don’t apply them for on-premises system all the time.
In a cloud environment, it is possible for example to have for 10ms a connectivity problem with another service that you are using. It means that you would need a retry policy in-place that would automatically try to reconnect to the service.
Cases like this need to be covered by your application that is running in the cloud. Take into consideration that you need to take all of this into consideration for lift and shift cases, too.

Self-Healing & Transient fault handling
The today post it is dedicated to self-healing topic. This is not SF, and you don’t need to be Netflix to have a self-healing mechanism. You can do small things at the application level that can improve drastically the capability of your system to recover after failures, especially when you work with a distributed system.  
When you run on top of Azure, there are many external services and dependencies that are used by your system. If one of them has a connectivity issue or is down for a few seconds, you need a mechanism that can recover from this failure and minimize data loss.

There are three things that you need to check to be able to have a self-healing system

  • Detect: You need to have a mechanism that can detect an issue automatically
  • Respond: The system needs to be able to take specific action when a problem is detected
  • Audit: The issue needs to be logged together will all relevant information

When you think about the response, try to think as simple as possible. The most important action that you need to do is to fail gracefully with a minimum impact on the rest of the system. On top of this, you can have more complex responses where you can switch to a secondary replica or persist the action to a temporary location until the service is up and running.

Common causes of transient faults can be:

  • Temporary unavailability
  • Network loss
  • Timeouts
  • Switch from one node to another

All these faults can be only temporary and basic mechanism like a retry mechanism can solve this issue. Even so, there are many on-premises systems that don’t have a retry policy when they communicate with storage or with an external service.

The challenge of a detecting mechanism for transient failures is to know if the failure lasts for a long period or is transient. This is why most retries mechanism includes a counter and different ways to calculate the waiting time between retries.
In most of the cases, client libraries are providing a built-in mechanism for retry policies. Let’s take as example Azure Blob Storage library that has multiple retry policies build-in. Developers can decide what policy they want to use. Remember that not for all errors a retry policy will solve the issue. For example, if you get a 401 Unauthorized, there is no sense to try to reconnect to the Azure Blob Storage. It is pretty clear from the error code that you have the right credentials or you don’t have access to that specific resource. The retry policy will not help in this case.

There are multiple strategies when using a retry mechanism from exponential back-off to random value for sleep time. Depending on your scenarios and what you want to achieve you will need to test different mechanism and identify the ideal values for retry time interval and the number of retries.

The most used retry mechanisms are using:

  • Immediate retry (can be dangerous)
  • Fix Interval
  • Incremental Interval
  • Exponential Interval
  • Random Interval

The most common mechanism is based on Exponential Interval, that it is supported by most of the libraries. If you have strict SLAs than you need to be careful with the time interval values that you decide to use. You might be forced to use a fixed interval combined to be able to meet a specific SLA.

When you're application has multiple layers be sure that you don’t have multiple retry mechanism that overlap and each retry policy will stop automatically. Testing the retry policy is also an important step that needs to be done especially when you don’t use a retry policy from the library SDK.
For complex systems, you need a mechanism that can detect and mark a system as down. For example, if you have a web application that is using Azure Redis Cache, you don’t want each client to try to access cache once it is down. The system shall be able to be aware of this and reject any other calls to the cache system as long as the service is down. For this scenarios, a Circuit Breaker can be used to avoid retries policies when are not necessary. Circuit Breaker is giving you the capability to monitor channel and block the communication when a failure is detected. In most of the implementations, some requests are allowed to be executed and are used to monitor the channel and mark the channel up again if the communication succeeds. 
There are many other mechanisms that can be used for self-healing. A part of them can be found below:

  • Retry-policy
  • Circuit Breaker
  • Bulkhead
  • Queues
  • Fail-over
  • Intermediate check-points
  • Leader Election
  • Block bad clients (actors)
  • Clients throttle 
  • Load Balancer

Before jumping to the next topic, I want to prepare a post that describes how you can combine multiple services that are available inside Azure to have a self-healing system.

Comments

Popular posts from this blog

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine:
threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration:

TeamCity.NET 4.51EF 6.0.2VS2013
It seems that there …

Entity Framework (EF) TransactionScope vs Database.BeginTransaction

In today blog post we will talk a little about a new feature that is available on EF6+ related to Transactions.
Until now, when we had to use transaction we used ‘TransactionScope’. It works great and I would say that is something that is now in our blood.
using (var scope = new TransactionScope(TransactionScopeOption.Required)) { using (SqlConnection conn = new SqlConnection("...")) { conn.Open(); SqlCommand sqlCommand = new SqlCommand(); sqlCommand.Connection = conn; sqlCommand.CommandText = ... sqlCommand.ExecuteNonQuery(); ... } scope.Complete(); } Starting with EF6.0 we have a new way to work with transactions. The new approach is based on Database.BeginTransaction(), Database.Rollback(), Database.Commit(). Yes, no more TransactionScope.
In the followi…

GET call of REST API that contains '/'-slash character in the value of a parameter

Let’s assume that we have the following scenario: I have a public HTTP endpoint and I need to post some content using GET command. One of the parameters contains special characters like “\” and “/”. If the endpoint is an ApiController than you may have problems if you encode the parameter using the http encoder.
using (var httpClient = new HttpClient()) { httpClient.BaseAddress = baseUrl; Task<HttpResponseMessage> response = httpClient.GetAsync(string.Format("api/foo/{0}", "qwert/qwerqwer"))); response.Wait(); response.Result.EnsureSuccessStatusCode(); } One possible solution would be to encode the query parameter using UrlTokenEncode method of HttpServerUtility class and GetBytes method ofUTF8. In this way you would get the array of bytes of the parameter and encode them as a url token.
The following code show to you how you could write the encode and decode methods.
publ…