4 – Moving to Windows Azure SQL Database


This chapter describes the third step in Adatum’s migration of the aExpense application to Windows Azure. For the initial migration of the aExpense application to the cloud, Adatum chose the IaaS model because it required the fewest modifications to the existing application. In the previous chapter, you saw why and how Adatum decided to move the aExpense web application to the PaaS deployment model by using Windows Azure Cloud Services instead of Windows Azure Virtual Machines. In this chapter you will see how Adatum reviewed its choice of data store, and moved from using SQL Server hosted in a Windows Azure virtual machine to using Windows Azure SQL Database.

The Premise

At present the aExpense application’s data store still uses the IaaS model in the form of SQL Server installed in a Windows Azure virtual machine. However, the Windows Azure platform also includes cloud services that offer both relational database capabilities and less structured storage capabilities using the PaaS model. Adatum wants to explore the capabilities and the consequent impact on their migration strategy for these alternative data stores.

Goals and Requirements

In this step along its migration path Adatum wants to evaluate whether it can reduce operating and hosting costs for the data storage functions of the aExpense application. The goals are to reduce the direct hosting costs if this is possible, as well as minimizing the amount of management and maintenance required by Adatum’s operators and administrators.

Adatum must first ensure that the data store they choose at this stage is suitable for use with the aExpense application, and it does not have limitations that may prevent the operation or future development of this or other related applications.

NoteBharath says:
Bharath When you evaluate the choice of data storage mechanism, remember that you are making a decision that could affect more than just the application you are currently migrating. If you plan to move additional on-premises applications to the cloud in the future, or create new applications that share the same data, you should consider how these will be affected by your choice of data storage mechanism.

Adatum would like to be able to change just the connection strings that define how the application connects to the data store, and continue to use the same SQL Server tools to manage and interact with its data. However, Adatum must consider how management tasks such as backing up the data can be accomplished when it moves away from using a hosted instance of SQL Server.

Finally, Adatum must ensure that the solution it chooses provides an acceptable level of performance, reliable connectivity, and sufficient data storage and throughput capabilities.

Overview of the Solution

This section of the chapter describes how Adatum evaluated the Windows Azure PaaS services for data storage, and then compares Windows Azure SQL Database to SQL Server. It also describes how Adatum explored the limitations of SQL Database in terms of its impact on the aExpense application.

PaaS Options for Data Storage

Windows Azure PaaS approach offers these two built-in mechanisms for storing data:

Adatum previously discounted using Windows Azure storage during the early steps of the migration due to the requirement to change the data access mechanism to work with the table and blob storage APIs. At this stage, Adatum wants to be able to continue using standard SQL Server tools, and change only the database connection strings. Therefore, the logical choice for this step is to use Windows Azure SQL Database.

Windows Azure SQL Database is SQL Server for the cloud. Therefore, Adatum should be able to move the aExpense data from SQL Server running in a Windows Azure Virtual Machine to SQL Database without requiring changes to the way that the application works.

NoteBharath says:
Bharath For the majority of applications, moving from SQL Server to SQL Database does not require any changes in either the database or the application code other than modifying the connection strings. However, there are some differences in the feature set supported by SQL Database as compared to SQL Server, which you must evaluate before making your decision.

Using Windows Azure SQL Database provides a compelling option for storing data because it removes the requirement to manage the operating system and database software. Developers can simply migrate their application data to the Windows Azure SQL Database hosted database service. They can use the Management Portal to configure the database, create and manage the tables, run scripts, and monitor the database. In addition they can use SQL Server Management Console, Visual Studio, scripts, or any other compatible tools to migrate and interact with the database.

NoteNote:
The MSDN topic. "Migrating Databases to Windows Azure SQL Database (formerly SQL Azure)" provides an overview of how to migrate a database to Windows Azure SQL Database. You can also use the Migration Wizard to help you to migrate your local SQL Server databases to Windows Azure SQL Database.

Comparing SQL Server and Windows Azure SQL Database

Given the similarities between SQL Server and Windows Azure SQL Database, why should Adatum choose one over the other? The key to this choice is to understand the advantages and limitations of the IaaS and PaaS models. In the cloud, SQL Server is available if you opt for the IaaS model while and SQL Database is available if you opt for the PaaS model.

NoteBharath says:
Bharath While SQL Server and Windows Azure SQL Database are fundamentally similar, there are some important differences that you must be aware of when making a decision on how to store your applications’ data.

If you choose to run SQL Server on a virtual machine in the cloud, you are responsible for managing both the operating system and the SQL Server installation; and ensuring that they are kept up to date with patches, that they are securely configured, and so forth. Windows Azure keeps copies of VMs for resilience and you can choose to have the storage account that holds these copies geo-replicated across different datacenters, but you do pay for the blob storage that holds these copies.

If you choose SQL Database, Microsoft handles all of this management for you. However, taking the PaaS route does mean that you give up some level of control over your environment.

In terms of cost, the situation is more complex because you have three basic choices for a SQL data store:

NotePoe says:
Poe See Chapter 6, "Evaluating Cloud Hosting Costs" for more detailed information about the relative costs of these options.

Limitations of Windows Azure SQL Database

Windows Azure SQL Database does have some limitations compared to SQL Server. Developers will not be able to use free text search, some XML handling capabilities, procedures that require common language runtime (CLR) programmability, scheduled tasks, and distributed queries that rely on the SQL Service Broker. In addition, some data types (including user-defined CLR types) are not supported.

However, Windows Azure SQL Database does support stored procedures and the majority of Transact-SQL functions and operators, and you may also be able to implement workarounds for some of the limitations, such as using a separate mechanism such as the Quartz scheduler in a Cloud Service role to drive scheduled tasks.

SQL Server may also provide better performance under certain high load conditions and complex query execution than Windows Azure SQL Database, which can be subject to throttling under very high load conditions. However, SQL Database supports a mechanism called federations that allows you to partition data by a specific key across multiple databases while still being able to interact with the data using standard queries.

NoteNote:
See “Federations in Windows Azure SQL Database” for more information about using SQL Database federations.

One other major difference is that Windows Azure SQL Database automatically replicates the data to multiple datacenters to provide resilience against failure. However, this is not a complete backup solution because it does not provide access to previous versions of the database. A database backup routine must be established for critical data when using both a hosted SQL Server and Windows Azure SQL Database. The section “Data Management” later in this chapter provides an example of a simple backup technique.

NoteNote:
For more details of these differences, see “Windows Azure SQL Database Overview” and “Guidelines and Limitations (Windows Azure SQL Database)”.

After considering all of the options available, and their impact on the migration of the aExpense and other applications, the developers decided that the simplicity offered by Windows Azure SQL Database was the most attractive. The few limitations it imposes will not prevent them from using it with the existing data and procedures in the aExpense application, and in other applications that Adatum intends to create in the future.

Database Management and Data Backup

Windows Azure SQL Database exposes remote management connectivity that is compatible with SQL Server management tools and other third party tools. Administrators, operators, developers, and testers can use SQL Server Management Console, Visual Studio, scripts, or any other compatible tools to connect to a SQL Database server instance in the cloud to migrate data, perform management tasks, and to and interact with the database and the data it holds. This familiar approach makes changing from SQL Server to SQL Database a relatively pain-free process.

Windows Azure SQL Database automatically maintains three replicas of database in different locations within the same datacenter to protect against hardware failure that may affect an individual node within the datacenter. However, it’s important to recognize that this is not a backup solution in the same sense that administrators and operators perform backup procedures for an on-premises or cloud-hosted SQL Server database.

For example, Adatum’s operators cannot access a specific replica of the data stored in SQL Database. The platform automatically maintains the replicas and uses the most current should a failure occur. If data is changed in error or lost in a database (for example, due to an operator error, code fault, or malicious action) that change to, or loss of data will also be applied to the database replicas.

NotePoe says:
Poe Windows Azure SQL Database automatically maintains copies of your database, but you cannot depend on this as a backup strategy. For example, if you accidently delete data in your current database, it will be removed from all the replica copies as well.

Therefore it is vital that Adatum adopts a backup strategy that works with SQL Database. For example, Adatum may choose to back up the data to an on-premises database or a separate cloud-hosted database by using replication or the Windows Azure database import and export feature; by using the SQL Server Management Studio to create a SQL Server Backup for the data; or by using third party tools to copy the data between Windows Azure SQL Database and blob storage. These tools can also be used for initializing the data in a database from a backup copy.

NoteNote:
See “Windows Azure SQL Database Backup and Restore” and “Business Continuity in Windows Azure SQL Database” for information about how you can back up and restore Windows Azure SQL Databases.

Database Connection Reliability

Adatum has noticed that the application running in the cloud very occasionally suffers from a failed connection to the database. While the application was running on premises these kinds of events were very rare, mainly due to the high speed physical connection between the application servers and the database servers within Adatum’s datacenter. When the application was moved to cloud, the data was deployed in a hosted SQL Server running in a Windows Azure Virtual Machine, and so connectivity between the application and the database was no longer over a dedicated network managed by Adatum.

While the connectivity within the Windows Azure datacenters is much faster and more reliable than is available on the Internet, Adatum can no longer manage traffic allocation and so occasional connection failures may occur. In addition, when Adatum moves from using SQL Server to the shared data storage mechanism that is Windows Azure SQL Database, there are additional factors to consider. For example, Windows Azure SQL Database will attempt to connect for thirty seconds, but may fail when the database encounters severe load conditions, or if the server is automatically recycled following a failure.

NotePoe says:
Poe In the cloud, the types of error that often disappear if you retry the operation are known as transient faults. They can occur for many reasons, including intermittent connectivity issues within the cloud or throttling behavior in the cloud service.

Adatum must address the issue of how to handle these kinds of transient faults in its applications’ data access code. If the timeout expires before establishing a connection, or if a connection to Windows Azure SQL Database drops while an application is using the connection, an error occurs; which the application must handle. How the application handles these errors depends on the specific circumstances, but possible strategies include immediately trying to re-establish the connection, keep retrying the connection until it succeeds, report the error to the user, or log the error and move on to another task.

If a database transaction was in progress at the time of the failure, the application should retry the transaction. It is possible for a connection to fail between sending a message to commit a transaction and receiving a message that reports the outcome of the transaction. In this circumstance, the application must have some way of checking whether the transaction completed successfully in order to determine whether to retry it.

Whereas Adatum wanted to avoid making unnecessary changes to the application code in previous migration steps, the developers have realized that they must resolve these issues now that Adatum has chosen to use Windows Azure SQL Database by implementing retry logic in the aExpense application.

Implementing Retry Logic for Database Connections

It is possible to create your own logic for retrying failed or dropped connections by writing a suitable delegate. The blog post "SaveChangesWithRetries and Batch Option" describes how to use the RetryPolicy delegate in the Microsoft.WindowsAzure.StorageClient namespace for this. The post describes using this delegate to retry saving changes to Windows Azure table storage, but you could adapt this approach to work with a context object in LINQ to SQL or ADO.NET Entity Framework.

However, a simpler option is to use the Transient Fault Handing Application Block that is part of the patterns & practices Enterprise Library. This application block exposes methods that allow you to execute code that connects to a wide range of Windows Azure services including SQL Database, Windows Azure Service Bus, Windows Azure Storage, and Windows Azure Caching Service.

You can configure a range of different retry policies, and write code to handle the success and failure events for the connection. You can easily add the Transient Fault Handling Application Block to an existing application; it requires very few code changes in your application, and you can customize its behavior to meet your specific requirements through configuration. Customization options include the choice of retry strategy to use in different circumstances; for example, you can choose between fixed interval, incremental interval, and exponential interval retry strategies.

This is the solution that the developers at Adatum chose to adopt.

NoteNote:
For more information see “The Transient Fault Handling Application Block.” The block is extensible, which enables you to add support other for other cloud services. You can find guidance on how to extend the block in “Extending and Modifying the Transient Fault Handling Application Block.”

Inside the Implementation

Now is a good time to walk through this step in the process of migrating aExpense into a cloud-based application in more detail. As you go through this section, you may want to download the Visual Studio solution from http://wag.codeplex.com/. This solution contains an implementation of the aExpense application (in the Azure-CloudService-WADB folder) after the migration step described in this chapter. If you are not interested in the mechanics, you should skip to the next section.

The Hands-on Labs that accompany this guide provide a step-by-step walkthrough of parts of the implementation tasks Adatum carried out on the aExpense application at this stage of the migration process.

Connecting to Windows Azure SQL Database

In all of the previous migration steps, Adatum used a hosted SQL Server database to store the data for the aExpense application. In this phase, the team moved the database to Windows Azure SQL Database and the data access code in the application remained unchanged. The only thing that needs to change is the connection string in the configuration file.

Connecting to Windows Azure SQL Database instead of SQL Server requires only a configuration change.

XML

<add name="aExpense" connectionString=
  "Data Source={Server Name};
   Initial Catalog=aExpense;
   UId={User Id};
   Pwd={User Password};
   Encrypt=True;
   TrustServerCertificate=False;
   Connection Timeout=30;"
  providerName="System.Data.SqlClient" />

NoteNote:
The values of Server Name, User Id, and User Password are specific to your Windows Azure SQL Database account.

There are two things to notice about the connection string. First, notice that, because Windows Azure SQL Database does not support Windows Authentication, the credentials for your Windows Azure SQL Database account are stored in plain text. You should consider encrypting this section of the configuration file as described in Chapter 3, “Moving to Windows Azure Cloud Services.”

The second thing to notice about the connection string is that it specifies that all communications with Windows Azure SQL Database are encrypted. Even though your application may reside on a computer in the same data center as Windows Azure SQL Database, you have to treat that connection as if it was using the internet.

Any traffic within the data center is considered to be “Internet” and should be encrypted.


NoteBharath says:
Bharath You can also add protection to your Windows Azure SQL Database instance by configuring the firewall in the Management Portal. You can use the firewall to specify the IP addresses of the computers that are permitted to connect to your Windows Azure SQL Database server and control if other Windows Azure services can connect to your SQL Database.

Handling Transient Connection Failures

When you try to connect to Windows Azure SQL Database, you can specify a connection timeout value in your data access connection strings. Windows Azure SQL Database will attempt to connect for thirty seconds, and so you should set the connection timeout in your code to at least this value. Adatum set the connection timeout to thirty seconds in the aExpense application.

NoteNote:
Windows Azure SQL Database removes idle connections after thirty minutes, and also imposes throttling to protect all users of the service from unnecessary delays. For a detailed explanation of how this works, and how you can optimize your data access connections, see “Windows Azure SQL Database Connection Management.”

In addition, Adatum decided to use the Enterprise Library Transient Fault Handling Application Block to implement a consistent and configurable retry strategy in the aExpense application. The recommended retry interval for Windows Azure SQL Database is ten seconds.

Adatum added a configuration section for the Transient Fault Handling Application Block to the Web.config file for the aExpense.Azure project. This configuration defines several retry strategies that the application can use, and identifies the "Fixed Interval Retry Strategy" as the default.

XML

<configuration>
  <configSections>
    <section name="RetryPolicyConfiguration"
      type="Microsoft....RetryPolicyConfigurationSettings,
            Microsoft.Practices...TransientFaultHandling,
            Version=5.0.1118.0, Culture=neutral,
            PublicKeyToken=31bf3856ad364e35"
            requirePermission="true"/>
    ...
  </configSections>
  <RetryPolicyConfiguration
      defaultRetryStrategy="Fixed Interval Retry Strategy">
    <incremental name="Incremental Retry Strategy"/>
    <fixedInterval name="Fixed Interval Retry Strategy"/>
    <exponentialBackoff name="Exponential Backoff Retry Strategy"
                        maxRetryCount="3"/>
  </RetryPolicyConfiguration>
  <typeRegistrationProvidersConfiguration>
    ...
    <add sectionName="RetryPolicyConfiguration"
         name="RetryPolicyConfiguration"/>
  </typeRegistrationProvidersConfiguration>
  ...
</configuration>

For more information about how to configure these strategies, see “Specifying Retry Strategies in the Configuration” on MSDN.

NotePoe says:
Poe Adatum chose to define their retry strategies in a configuration file rather than in code to make it easier to modify them in the future.

The following code sample from the ExpenseRepository class shows how Adatum uses a retry strategy when the application tries to save an expense item to the SQL Database database. The application uses the default retry policy when it invokes the SubmitChanges method.

C#

using Microsoft.Practices.EnterpriseLibrary.WindowsAzure
                                    .TransientFaultHandling;
using Microsoft.Practices.TransientFaultHandling;
using Model;

...

private readonly RetryPolicy sqlCommandRetryPolicy;

...

public void SaveExpense(Model.Expense expense)
{
    using (var db = new DataAccessLayer.ExpensesDataContext
        (this.expenseDatabaseConnectionString))
    {
        var entity = expense.ToEntity();
        db.Expenses.InsertOnSubmit(entity);

        foreach (var detail in expense.Details)
        {
            var detailEntity = detail.ToEntity(expense);
            db.ExpenseDetails.InsertOnSubmit(detailEntity);
        }
                
        this.sqlCommandRetryPolicy.ExecuteAction(
            () => db.SubmitChanges());
    }
}

Setup, Deployment, Management, and Monitoring

This section discusses the way that Adatum manages the setup and deployment of the Windows Azure SQL Database server and the databases it contains, and how administrators and operators at Adatum manage and monitor the database.

Data for Development and Testing

It is possible to create separate databases within your SQL Database server instance for testing and for use during development. Alternatively, as when using SQL Server hosted in a virtual machine, you can use separate server instances to minimize the chances of corrupting live data. Adatum has a separate Windows Azure subscription that is uses for development and testing, as described in Chapter 3 of this guide. The keys for accessing and deploying to the production subscription are not available to testers and developers.

Data Migration

To migrate an existing database schema to Windows Azure SQL Database, you can use SQL Server Management Studio to export the schema as a Transact-SQL script, and then run the script against Windows Azure SQL Database. To move data to Windows Azure SQL Database, you can use SQL Server Integration Service. The SQL Server Migration Wizard is also very useful for migrating both schema and data.

One issue that Adatum faced is that the aspnet_regsql.exe tool used to create the tables for the ASP.NET profile provider will fail to run against a Windows Azure SQL Database. For information about this incompatibility and how to work around it, see “Updated ASP.net scripts for use with Microsoft SQL Azure.”

NoteMarkus says:
Markus The ASP.NET providers are not designed to retry operations when they encounter a transient fault. Adatum is investigating alternative solutions to storing the profile data for the aExpense application.

When the team at Adatum migrated the live application they created scripts that create the required tables in SQL Database, and transfer data from the on-premises version of SQL Server to Windows Azure SQL Database.

NoteNote:
For more information about data migration and the tools that are available to help, see “Data Migration to Windows Azure SQL Database Tools and Techniques” and “Choosing Tools to Migrate a Database to Windows Azure SQL Database.”

Data Management

To enable backup and restore functionality, Adatum plans to create a copy of the aExpense database daily using the following Transact-SQL command and maintain three rolling copies of the previous three days.

T-SQL

CREATE DATABASE aExpense_copy_[date] AS COPY OF [source_server_name].aExpense

This technique creates a copy of the aExpense database that includes all of the transactions that committed up to the time when the copy operation completed. Adatum can restore to a previous version of the database by renaming the copy, such as aExpense_copy_080612 to aExpense. However, Adatum will be charged for the storage required to maintain multiple copies of the database

NotePoe says:
Poe This technique does not allow for point-in-time restore operations as you can do with SQL Server by running the RESTORE command.

Adatum also plans to investigate using the SQL Database Import/Export Service to create a backup of the aExpense database in a different data center from the one where it hosts the aExpense application. This will ensure that Adatum has a copy of the aExpense data in the event of a disaster that leads to the data center where the aExpense application is hosted becoming unavailable.

Database Monitoring

SQL Database enables a subset of the dynamic management views that operators can use to diagnose performance problems in the database. Adatum plans to use these views in the aExpense database.

For more information, see “Monitoring Windows Azure SQL Database Using Dynamic Management Views” on MSDN.

More Information

For information about Windows Azure SQL Database, see “Windows Azure SQL Database Overview.”

TechNet describes the differences between SQL Server and Windows Azure SQL Database.

The MSDN topic “Migrating Databases to Windows Azure SQL Database (formerly SQL Azure)” provides an overview of how to migrate a database to Windows Azure SQL Database. You can also use the Migration Wizard at to help you to migrate your local SQL Server databases to Windows Azure SQL Database.

See “Windows Azure SQL Database Backup and Restore” and “Business Continuity in Windows Azure SQL Database” for information about how you can back up Windows Azure SQL Databases.

To encrypt your SQL connection string in the Web.config file, you can use the Pkcs12 Protected Configuration Provider that you can download from. The sections “Best Practices on Writing Secure Connection Strings for SQL Database” and “Create and deploy an ASP.NET application with Security in mind” in the post “Windows Azure SQL Database Connection Security” discuss using this provider.

For more information about retrying database connections by using Enterprise Library, see “The Transient Fault Handling Application Block.”

Overview of Options for Migrating Data and Schema to Windows Azure SQL Database” contains useful information about migrating data to Windows Azure SQL Database.