What is Azure Lease Blob?

user43286 picture user43286 · Jun 9, 2017 · Viewed 13.3k times · Source

What is Azure Lease Blob? Is it just a mechanism which provides an exclusive lock to the blob storage? What is special about it? Where can I use it? I tried to read Azure documents, but it's not clear to me yet.

Answer

Gaurav Mantri picture Gaurav Mantri · Jun 10, 2017

What is Azure Lease Blob? Is it just a mechanism which provides an exclusive lock to the blob storage?

Your understanding is correct. When you lease a blob, you acquire an exclusive lock on that blob. As long as the blob is leased, no one other than lease holder can modify or delete the blob. You can either acquire a temporary lease, duration of which could be anywhere between 15 to 60 seconds or an infinite lease.

Where can I use it?

Most commonly this functionality is used by Azure Virtual Machines. As you know Azure VMs are backed by Azure Page Blobs. So when a VM is created, an infinite lease is acquired on the blob holding the VHD for that Azure VM so that no other process can modify or delete that blob.

Yet another place where you would want to use lease blob functionality is when you want to implement Leader Election pattern.

For example, consider a scenario where you would want to process a file stored in blob storage through a background Worker Role. Further assume that there are multiple instances of that Worker Role running where each instance wakes up after 30 seconds and checks for blobs in a container and if any blob is found that instance processes it.

Since Azure Worker Roles are stateless in nature, without locking that blob each instance will process that blob (not something you would want). To avoid this situation what you could do is have each instance try to acquire a lease on that blob. Only one instance will succeed (and hence elected as leader). Other instances will fail to acquire the lock on that blob. The leader instance will process the blob.

Another example would be where you want to distribute work in Competing Consumers where you would want to distribute work among them. In one of our products, we are using this pattern with Leader Election pattern. Through blob lease functionality, we find a leader (consumer which was able to acquire blob lease) and then this leader distributes work amongst other consumers.