Coordinating concurrent access to shared resources across multiple service instances is a foundational challenge in microservice architectures. MASA Framework’s distributed lock abstraction exposes a singleDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/masastack/MASA.Framework/llms.txt
Use this file to discover all available pages before exploring further.
IDistributedLock interface and lets you swap between an in-process lock for single-instance deployments and robust, externally-coordinated locks backed by MySQL, Azure Blob Storage, or the file system — all without changing your application code.
Available Packages
Choose the backend that matches your deployment topology:| Package | Backend | Best For |
|---|---|---|
Masa.Contrib.Data.DistributedLock.Local | SemaphoreSlim (in-process) | Single-instance / unit-testing |
Masa.Contrib.Data.DistributedLock.Medallion.MySql | MySQL advisory locks | Multi-instance, relational DB stack |
Masa.Contrib.Data.DistributedLock.Medallion.SqlServer | SQL Server app locks | Multi-instance, SQL Server stack |
Masa.Contrib.Data.DistributedLock.Medallion.Azure | Azure Blob leases | Multi-instance, Azure-hosted workloads |
Masa.Contrib.Data.DistributedLock.Medallion.FileSystem | File-system locks | Multi-process on a single shared volume |
Core Abstractions
IDistributedLock
The primary interface your application code depends on. It provides a single async method to acquire a named lock and returns a disposable handle:
null return value indicates that the lock could not be acquired within the specified timeout — your code must handle this case explicitly.
DefaultLocalDistributedLock
The local implementation wraps a SemaphoreSlim keyed by resource name. It is safe for concurrent access within a single process and is ideal for integration tests or single-replica deployments where external coordination is unnecessary.
MedallionBuilderExtensions
Extension methods that configure the Medallion.Threading provider for each supported backend. Medallion.Threading is a battle-tested .NET distributed locking library; MASA wraps it behind IDistributedLock so you can switch backends via DI registration only.
DisposeAction
An internal helper used by all implementations to release the underlying lock when the caller disposes the handle. The await using pattern ensures release even in the presence of exceptions.
Registration
Local lock (single process)
MySQL distributed lock
SQL Server distributed lock
Azure Blob distributed lock
File-system distributed lock
Usage Pattern
InjectIDistributedLock into any service and acquire a named lock before entering a critical section. Use await using to guarantee the lock is released:
Name your lock resources with a meaningful prefix (e.g.
inventory:, order:) to avoid accidental collisions between unrelated parts of your application. Resource names are arbitrary strings — choose a consistent naming convention across your team.Choosing the Right Backend
IDistributedLock interface, you can start with UseLocal() during local development and switch to a Medallion backend for staging and production by changing only the DI registration — no application code changes required.
Timeout and Cancellation
AllAcquireAsync calls accept an optional timeout and CancellationToken. If neither is provided, the call blocks indefinitely until the lock is available: