Thu, 29 Apr 2010

Update Your Windows Azure Website in Just Seconds by Syncing with Blob Storage

One of the coolest uses I’ve found for my Windows Azure Hosted Web Core Worker Role is to sync my website with blob storage, letting me change files at will and immediately see the results in the cloud. You can grab the code over on Code Gallery, or a prebuilt package that you can deploy right away.

Here’s a 30-second video showing it in action:

How It Works

In a typical Windows Azure web role, IIS is configured to host the website contained in your application package. As you may know, this content is in a read-only directory on the virtual machine. That means that to change anything in your website, you need to redeploy your application.

With the Hosted Web Core Worker Role, I can point Hosted Web Core (HWC) at any local directory by just changing applicationHost.config. This means I can have a writable local directory (using local storage resources), and I can populate that directory with the website content I fetch from blob storage. Then it’s just a matter of continually syncing those blobs as they change.

The class that does all the work is called OneWayBlobSync. It’s used in WorkerRole.cs like this in OnStart():


sync = new OneWayBlobSync(
    container,
    RoleEnvironment.GetLocalResource("Websites").RootPath,
    TimeSpan.FromSeconds(int.Parse(
        RoleEnvironment.GetConfigurationSettingValue("SyncIntervalInSeconds"))));

and then in Run():

sync.Start();

Under the hood, this class does the following:

  1. List all the blobs in the source container.
  2. Delete any local blobs that are no longer in the source container.
  3. Compare the blobs’ ETags with the corresponding local files, and update any changed blobs.
  4. Repeat.

That’s it!

Drawbacks

There are a few drawbacks to using this approach:

  1. Updating the website happens at roughly the same time on all instances. No rolling upgrade means potential downtime for your application.
  2. Copying over files that are in use might have unintended consequences (or might not work at all). For most ASP.NET content (including web.config), this seems to work fine, but I wouldn’t be surprised to learn that some files can’t be modified this way. My code doesn’t handle any sort of concurrency errors.
  3. There are no atomic updates. During a sync, some files might be the old versions and others the new versions. There’s no way with this method to make sure everything changes at once.

The bottom line is that I’d hesitate to use this in production. But it’s awesome as a development tool.

Get the Code

You can download the full source code or a prebuilt package over at the Windows Azure Hosted Web Core Worker Role on Code Gallery. With the prebuilt package, you can simply update the configuration file (ServiceConfiguration.cscfg) and deploy. Then you can copy the website to blob storage whenever you’re ready.