Skip to content

Feature: Chained synchronization - #290

Open
gliljas wants to merge 1 commit into
madelson:masterfrom
gliljas:composite
Open

gliljas wants to merge 1 commit into
madelson:masterfrom
gliljas:composite

Conversation

@gliljas

@gliljas gliljas commented Oct 15, 2026 •

Copy link
Copy Markdown

I started this branch like two years ago, but other things got in the way. The idea is to enable chaining of two or more synchronization providers, so that locks and semaphores must be acquired by all included providers. They fail or succeed together. Since I started, the CompositeDistributedSynchronizationHandle was added, and I could remove a lot of my code and use that instead.

So, what's the use case? We used it (a separate wrapper that I ended up with) when doing a rolling migration from one Azure lock account to another, and we're using it again now to migrate from Azure to Redis. All seamless, since we can ensure that locks are acquired in both places during the migration.

The second use case I see is that it together with the In Process provider (#291 ) enables us to not consume resources on the shared state if the lock was already acquired by the same process. Simply add the in process provider as the first in the chain.

// HandleLostToken can be expensive to obtain on some providers, so we don't touch the underlying
// handles until we're asked for the token, and we only ever do so once. We track that with a
// separate flag rather than a null check because a null source is itself a valid memoized result
// (it means no underlying handle has a cancelable token), which we must not recompute on each read.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, this is optimizing away this code on the getter when HandleLost is not supported on all tokens:

var cancellableTokens = handles
    .Select(h => h.HandleLostToken)
    .Where(t => t.CanBeCanceled)
    .ToArray();

Is that correct? Feels unnecessary to me as it only optimizes the case where (a) none of the types in the chain have handle lost and yet (b) HandleLost is being accessed with some frequency. Am I missing something?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's correct. Indeed a narrow case, so I won't die on that hill. It was just case of "hmm...that can legitemately be null even when acquired".

@madelson

Copy link
Copy Markdown
Owner

Thanks for sharing @gliljas . Reading through, my reaction is we end up with a lot of API surface area for fairly narrow functionality.

Today, with composites our API supports 1 provider + N names but in theory the underlying CompositeDistributedSynchronizationHandle machinery supports N providers, N names.

My inclination is to keep things simple and just add an API like this:

public static class DistributedSynchronization
{
    public static ValueTask<IDistributedSynchronizationHandle?> TryAcquireAllAsync(IReadOnlyList<IDistributedLock> @locks, TimeSpan timeout = default, CancellationToken cancellationToken = default);
    public static ValueTask<IDistributedSynchronizationHandle> AcquireAllAsync(IReadOnlyList<IDistributedLock> @locks, TimeSpan? timeout = null, CancellationToken cancellationToken = default);

    // repeat for reader, writer, and semaphore. NOT upgrade!
}

This small change would expose everything needed to build targeted decorators in userland. It wouldn't stop us from adding them to the library in the future, but it feels like a no-regrets move.

As far as the in-process use-case, I feel like there's a bit more nuance around how you might want this to work. For example:

  • In a mutex/writer lock scenario, when releasing the distributed lock if we have other local waiters it might be desirable to hand off to them first instead of immediately giving up the remote lock. In a high-contention scenario this could greatly reduce the number of total lock/unlock operations on the remote. You'd need some kind of (configurable) timeout after which you revert to just giving up the lock in order to prevent one server from monopolizing
  • Similarly for a reader lock you can do this and really reduce overhead, e.g. N local readers could share the same underlying handle. Again you'd want a timeout for this so that you don't starve out remote writers forever
  • For semaphores it seems reasonable that you might want different max counts, e.g. N concurrent operations but only M from a given server

This makes me think that that scenario might be best supported by dedicated functionality directly focused on tiered synchronization rather than generic functionality which has multiple purposes.

@gliljas

gliljas commented Nov 13, 2026

Copy link
Copy Markdown
Author

My inclination is to keep things simple and just add an API like this:

I believe you may be correct. For my use case, I really needed it to be wrapped as a provider, but that could be easily built on top of extension methods.

This small change would expose everything needed to build targeted decorators in userland. It wouldn't stop us from adding them to the library in the future, but it feels like a no-regrets move.

Fair point

* In a mutex/writer lock scenario, when releasing the distributed lock if we have other local waiters it might be desirable to hand off to them first instead  of immediately giving up the remote lock. In a high-contention scenario this could greatly reduce the number of total lock/unlock operations on the remote. You'd need some kind of (configurable) timeout after which you revert to just giving up the lock in order to prevent one server from monopolizing

* Similarly for a reader lock you can do this and really reduce overhead, e.g. N local readers could share the same underlying handle. Again you'd want a timeout for this so that you don't starve out remote writers forever

Interesting idea!

* For semaphores it seems reasonable that you might want different max counts, e.g. N concurrent operations but only M from a given server

Yes! I've thought about that too. It would be somewhat similar to using two ConcurrencyRateLimiters inside a ChainedRateLimiter, which is a bit more complex with queues and stuff, but still.
The problem there is of course that Microsoft, despite initial comments indicating the opposite, built this without distributed architectures in mind. (I'm actually considering a DistributedRateLimiting library project).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants