https://chatgpt.com/share/e/69eafa82-6f24-8010-b6f0-43b9061e449d
take a look at live-mutex https://github.com/oresoftware/live-mutex how could we improve the code to lock not on composite keys but on two keys if we have user1 and writeFavoritesForUser1 we maye want to lock on user1 and writeFavoritesForUser1 - we could do this: lockKey = ${user1}:${writeFavoritesForUser1} but locks the intersection not the union we could do this: await getLock(${user1}); await geLock(${writeFavoritesForUser1}); but that's slower and more http reqs - instead we want: getMultiLock([user1, writeFavoritesForUser1]); and the lock/mutex broker handles it is this possible?
Yes — getMultiLock([user1, writeFavoritesForUser1]) is possible, and it’s the right abstraction.
Composite key:
"user1:writeFavoritesForUser1"
locks only that exact intersection. It won’t conflict with a lock on user1 alone.
The safer design is broker-side atomic multi-key acquisition:
const lock = await client.acquireMany(['user1', 'writeFavoritesForUser1']);
try {
// critical section
} finally {
await lock.release();
}
Implementation idea:
- Add client command type, e.g.
type: 'lock-many', with keys: string[].
- Broker canonicalizes keys: dedupe + sort.
- Broker grants only if all keys are currently available.
- If any key is held, enqueue the request as one waiter object attached to all relevant keys.
- On unlock of any key, broker reevaluates queued multi-lock requests.
- Unlock releases all keys using one
lockUuid.
Sorting is important even though acquisition is broker-atomic; it gives deterministic fairness and avoids deadlock if fallback logic ever acquires per-key. Live-Mutex already has the right architecture for this because the broker is the single source of truth for locks, and the current broker stores lock state by key with wait queues/lockholders. ([GitHub]1)
I would not implement it as:
await getLock(user1);
await getLock(writeFavoritesForUser1);
unless the client library wraps it with deterministic ordering + rollback, because two clients can deadlock:
A locks user1, waits for writeFavoritesForUser1
B locks writeFavoritesForUser1, waits for user1
Recommended API shape:
await client.acquireMany(keys, opts);
await client.releaseMany(keys, { id });
or better:
const unlock = await client.acquireMany(keys);
await unlock();
Return value:
{
acquired: true,
keys: ['user1', 'writeFavoritesForUser1'],
id,
unlock,
release: unlock
}
So: yes, possible, and it should be done in the broker, not by inventing composite keys and not by sequential client-side locks.
https://chatgpt.com/share/e/69eafa82-6f24-8010-b6f0-43b9061e449d
take a look at live-mutex https://github.com/oresoftware/live-mutex how could we improve the code to lock not on composite keys but on two keys if we have user1 and writeFavoritesForUser1 we maye want to lock on user1 and writeFavoritesForUser1 - we could do this: lockKey = ${user1}:${writeFavoritesForUser1} but locks the intersection not the union we could do this: await getLock(${user1}); await geLock(${writeFavoritesForUser1}); but that's slower and more http reqs - instead we want: getMultiLock([user1, writeFavoritesForUser1]); and the lock/mutex broker handles it is this possible?
Yes —
getMultiLock([user1, writeFavoritesForUser1])is possible, and it’s the right abstraction.Composite key:
"user1:writeFavoritesForUser1"locks only that exact intersection. It won’t conflict with a lock on
user1alone.The safer design is broker-side atomic multi-key acquisition:
Implementation idea:
type: 'lock-many', withkeys: string[].lockUuid.Sorting is important even though acquisition is broker-atomic; it gives deterministic fairness and avoids deadlock if fallback logic ever acquires per-key. Live-Mutex already has the right architecture for this because the broker is the single source of truth for locks, and the current broker stores lock state by key with wait queues/lockholders. ([GitHub]1)
I would not implement it as:
unless the client library wraps it with deterministic ordering + rollback, because two clients can deadlock:
Recommended API shape:
or better:
Return value:
So: yes, possible, and it should be done in the broker, not by inventing composite keys and not by sequential client-side locks.