https://github.com/youscan/go-azuremutex
Golang implementation of distributed mutex on Azure lease blobs
https://github.com/youscan/go-azuremutex
azure distributed-computing golang golang-package leader-election mutex
Last synced: 5 months ago
JSON representation
Golang implementation of distributed mutex on Azure lease blobs
- Host: GitHub
- URL: https://github.com/youscan/go-azuremutex
- Owner: youscan
- License: mit
- Created: 2021-09-12T20:44:01.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2025-12-23T07:29:53.000Z (6 months ago)
- Last Synced: 2025-12-24T21:59:27.049Z (6 months ago)
- Topics: azure, distributed-computing, golang, golang-package, leader-election, mutex
- Language: Go
- Homepage:
- Size: 110 KB
- Stars: 16
- Watchers: 10
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Distributed Mutex on Azure Lease Blobs
[](https://github.com/youscan/azure-mutex/actions/workflows/ci.yaml) [](https://pkg.go.dev/github.com/youscan/go-azuremutex) [](https://goreportcard.com/report/github.com/youscan/go-azuremutex)
This package implements distributed lock available for multiple processes. Possible use-cases include exclusive access to shared data, leader election, etc.
Azure Storage supports the feature that guarantees exclusive access to the blobs using basic lease functionality: it's possible to acquire a fixed or infinite duration lease, renew and then release it. Thus it's possible to implement a basic distributed mutex on top of blobs.
## Basic Abstractions
`AzureMutex`
A wrapper on top of Azure blobs API encapsulates API interactions and provides three primary functions: Acquire, Renew, Release.
`Locker`
Top-level abstractions that support background renewal of the acquired lease.
## Locker Usage
Installation
```
go get -u github.com/youscan/go-azuremutex
```
Configuration
```
var options = azmutex.MutexOptions{
AccountName: "mystorageaccount",
AccountKey: "******",
ContainerName: "locks",
}
```
Use `Locker` with automated renewal
```
locker := azmutex.NewLocker(options, "migration")
err = locker.Lock()
...
// As soon the lease was acquired we can safely do exclusive job
RunDatabaseMigration()
...
err = locker.Unlock()
```
Use `AzureMutex` with lease that will expire in 60 seconds
```
mutex := azmutex.NewMutex(options)
err = mutex.Acquire("migration", 60)
...
// This code is only safely to run within 60 seconds window
RunDatabaseMigration()
...
err = mutex.Release("migration")
```
## Reference
- [Azure / Architecture / Cloud Design Patterns / Leader Election pattern](https://docs.microsoft.com/en-us/azure/architecture/patterns/leader-election)
- [Microsoft Azure Development Cookbook Second Edition: Leasing a blob and implementing distributed locks](https://www.oreilly.com/library/view/microsoft-azure-development/9781782170327/ch03s16.html)