DSemaphore never talks to Redis directly. It talks to a Transport, so you can back it with another store (Postgres, etcd, DynamoDB, ...) without changing any calling code.
interface Transport {
tryAcquire(key: string, id: string, limit: number, leaseMs: number): Promise<boolean>;
release(key: string, id: string): Promise<boolean>;
count(key: string): Promise<number>;
releaseAll(target: { key: string } | { prefix: string }): Promise<number>;
close(): Promise<void>;
}
tryAcquire is atomic. Concurrent callers must never push live holders above limit. Use a transaction, a script, or a conditional write.release loses its slot after leaseMs.count ignores expired holders, even ones not yet purged.release of an unknown id resolves false and does nothing.releaseAll force-frees every holder of the exact key, or of every key starting with the literal prefix (no wildcards), and resolves with the number of live holders freed. Holders' later release resolves false.close frees only what the transport created itself.The repository ships test/contract.ts, a shared suite that every transport must pass: limits, release, lease expiry, concurrent acquires, counting. Run it against your implementation:
import { transportContract } from '../contract';
transportContract('MyTransport', () => new MyTransport(/* ... */));
The built-in in-memory test transport and the Redis/Valkey transport run this same suite, so a new backend that passes it behaves identically to them.
const sem = new DSemaphore({ name: 'jobs', limit: 5, transport: new MyTransport() });