Skip to content

Support Multi-Key Distributed Locking for Composite Operations #236

@moeen

Description

@moeen

Description

We need a feature to acquire distributed locks on multiple keys simultaneously. The solution must:

  • Acquire locks in a deterministic order (e.g., alphabetical) to prevent deadlocks.
  • Return a composite lock that implements IDistributedLock so it can be used in a using block.
  • Roll back all acquired locks if any single lock cannot be acquired within a timeout.

Real-World Use Case

Imagine we have three data banks (A, B, C) and users want to extract records from them. Each bank uses a cursor to mark the last retrieved record so that subsequent extractions don’t return duplicates. Users might request a combination—say, N records from Bank A, M from Bank B, and O from Bank C—in a single operation. To ensure data consistency and proper cursor advancement without overlap, we need to atomically lock the selected banks during the extraction.

Example Code Use Case

// Operation: extract records from Banks A, B, and C simultaneously.
var keysToLock = new[] { "BankA", "BankB", "BankC" };
var compositeLock = await distributedLockProvider.TryAcquireMultiLockAsync(keysToLock, TimeSpan.FromSeconds(2), cancellationToken);

I’m ready to implement this feature. Please let me know if you have any feedback or additional requirements.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions