Using geo-redundancy in your HA apps with RA-GRS Storage

This sample shows how to use geo-redundancy with read-access geo-redundant storage (RA-GRS) to switch your high-availability application to secondary storage when there is a problem with primary storage, and then switch back when primary storage becomes available again. For more information, please see Designing HA Apps with RA-GRS storage.

If you don't have a Microsoft Azure subscription, you can get a FREE trial account here.

How it works

This application uploads a blob by creating a Stream object, then uploads the stream to a container for testing. It then enters a loop with a prompt to download the blob, reading from primary storage. If there is a retryable error reading from the primary region, a retry of the read request is performed against secondary storage.

To exit the loop and clean up resources, press the Esc key at the prompt.

How to run the sample

  1. If you don't already have it installed, install Fiddler. In this application, we'll use Fiddler to intercept and modify a response from the service to indicate a failure, so it triggers a failover to the secondary region.

  2. Run Fiddler.

  3. Start the application in Visual Studio. It displays a console window showing the count of requests made against the storage service to download the file, and tells whether you are accessing the primary or secondary endpoint. You can also see this in the Fiddler trace.

  4. At the prompt to download the blob, press any key to see that request is made made successfully against the primary region endpoint.

  5. Before downloading the blob again, go to Fiddler and select Rules > Customize Rules. Search for the OnBeforeResponse function and insert the following code. (An example of the OnBeforeResponse method is included in the project in the Fiddler_script_v12.txt file.)

if (oSession.hostname == "YOURSTORAGEACCOUNTNAME.blob.core.windows.net")
{
   oSession.responseCode = 503;  
}

Replace YOURSTORAGEACCOUNTNAME with the name of your storage account and make sure the above code is uncommented. Save your changes to the script. Sometimes, you may need to restart Fiddler in order for the script changes to be picked up by the app.

  1. Return to your application. At the prompt, press any key to resume the application and download the blob. In the output, you will see the request is sent to the primary region. This request will fail because Fiddler has modified the response to return a 503. The retry request is sent to the secondary region and will likely succeed. You can continue to download the blob at the prompt to test various scenarios.

  2. When you are finished testing, go back into Fiddler, comment out the code, and save the script. At the prompt, press any key to download the blob. The output shows that the request to the primary region is successful again.

  3. To exit the application and delete the newly created container, press the Esc key at the prompt.

If you run this application multiple times, make sure the added code in the script is commented out before you start the application.

More information