Book Image

Microsoft Azure Development Cookbook Second Edition

Book Image

Microsoft Azure Development Cookbook Second Edition

Overview of this book

Table of Contents (15 chapters)
Microsoft Azure Development Cookbook Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Leasing a blob and implementing distributed locks


As the same services of Azure consume the Blob storage as a service, it needed some features to be enabled for complex scenarios. One of these is to allow an Azure virtual machine to exclusively access a blob (a VHD, for example), preventing other clients to access it in any way. To provide this feature, Blob Storage has the lease function: a lock-like status in which a blob cannot be written by anybody except the one who owns the lease.

A blob can be leased for 15 seconds up to 60 seconds (or infinite), and it can be renewed as many times as needed (while it is still locked). This is particularly useful with page blobs as they can be updated with random writes. Indeed, the Azure virtual machines use leases to ensure that only one instance at a time can mount a VHD page blob as a disk.

There are five blob-leasing actions:

  • Acquire

  • Renew

  • Change

  • Release

  • Break

The Acquire action is invoked on a blob to acquire a lease on it for a specified...