UNPKG

node-redlock

Version:

A distributed locking algorithm used to manage distributed resources in a system.

107 lines (79 loc) 4.24 kB
Introduction and Overview --------------------------- This TypeScript/JavaScript code is a simple example of how to use the `ioredis` and `redlock` libraries for managing distributed locks in a Node.js application. The code demonstrates how to create clients for Redis, a popular key-value store, using both the `ioredis` library and the `redis-sentinel` package. It then shows how to use these clients with the `redlock` library to acquire and release locks on a resource. System Architecture --------------------------- The architecture of this system is based on the client-server model, where the Redis server acts as the central data store and the Node.js application acts as the client that interacts with the Redis server. The `redlock` library is used to manage distributed locks across multiple Redis clients, ensuring that only one client can acquire a lock at a time. Data Design --------------------------- The code uses Redis as the data store, which stores key-value pairs. In this example, the key is a string representing a resource, and the value is a boolean indicating whether the lock is currently held. Interface Design --------------------------- There are no user interfaces in this code, as it is focused on the internal workings of the application rather than its interaction with users. Component Design --------------------------- The main components in this code are: * `Redis`: a class that represents a Redis client created using the `ioredis` library. * `IoredisClient`: an interface that defines the methods for interacting with a Redis client. * `RedisJsClient`: a class that implements the `IoredisClient` interface and provides additional functionality for managing distributed locks using `redlock`. * `Redlock`: a class that manages distributed locks across multiple Redis clients. User Interface Design --------------------------- There is no user interface in this code, as it is focused on the internal workings of the application rather than its interaction with users. Assumptions and Dependencies ------------------------------- * The `ioredis` and `redis-sentinel` libraries are installed and properly configured. * The Redis server is running and accessible from the Node.js application. * The `redlock` library is installed and properly configured. Glossary of Terms --------------------------- * Redis: a popular key-value store that is used as the data store in this example. * Distributed lock: a mechanism for ensuring that only one client can access a resource at a time, even when multiple clients are running concurrently. * Redis client: an object that represents a connection to a Redis server and provides methods for interacting with the server. * `IoredisClient`: an interface that defines the methods for interacting with a Redis client. * `RedisJsClient`: a class that implements the `IoredisClient` interface and provides additional functionality for managing distributed locks using `redlock`. * `Redlock`: a class that manages distributed locks across multiple Redis clients. Class Diagram (in Mermaid syntax) --------------------------------- ```mermaid class Redis { host: string; port: number; constructor(options: object) { this.host = options.host; this.port = options.port; } } interface IRedisClient { get(key: string): Promise<string | null>; set(key: string, value: boolean): Promise<void>; quit(): Promise<void>; } class RedisJsClient implements IRedisClient { url: string; constructor(options: object) { this.url = options.url; } async get(key: string): Promise<string | null> { // Implementation of the get method } async set(key: string, value: boolean): Promise<void> { // Implementation of the set method } async quit(): Promise<void> { // Implementation of the quit method } } class Redlock { clients: IRedisClient[]; constructor(clients: IRedisClient[]) { this.clients = clients; } async acquireLock(resource: string, ttl: number): Promise<boolean | null> { // Implementation of the acquireLock method } async releaseLock(resource: string, lockValue: boolean | null): Promise<void> { // Implementation of the releaseLock method } } ```