Distributed lock with Redis implementation for Node.js
npm install node-redissonnode-redlock project is not very easy to use. When using it, you need to pay great attention to the holding time of the lock, make predictions about the holding time of the lock in advance, and manually renew the lock. However, in actual business development, it is very difficult to make efficient predictions about the holding time of the lock.
Node Redisson has implemented the watchdog and unlock notification features, so that you don't have to worry about predicting the holding time of the lock anymore.
sh
pnpm add node-redisson ioredis
`
$3
`ts
import { Redisson } from 'node-redisson';
// 1. Create a Redisson instance
const redisson = new Redisson({
redis: {
options: {
host: '127.0.0.1',
port: 42800,
},
},
});
// 2. Get the lock
const lock = redisson.getLock('Example:LockName');
// 3. Lock
await lock.lock();
// 4. Unlock
await lock.unlock();
``