@parloa/ts-redis
A NestJS Redis client module with built-in support for Azure Managed Identity authentication and automatic token refresh.
Features
- NestJS Integration: Drop-in module for NestJS applications
- Azure Managed Identity: Seamless authentication with Azure Redis Cache using managed identities
- Automatic Token Refresh: Handles token expiration and refresh automatically
- Connection Resiliency: Built-in retry logic and reconnection strategies
- TypeScript: Full type safety with TypeScript support
Installation
pnpm install @parloa/ts-redis
Basic Usage
1. Import the module in your NestJS application
import { Module } from '@nestjs/common'
import { ConfigModule } from '@nestjs/config'
import { makeRedisModuleMetadata, REDIS_CLIENT } from '@parloa/ts-redis'
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
}),
],
...makeRedisModuleMetadata(REDIS_CLIENT),
})
export class AppModule {}
2. Configure environment variables
REDIS_URL=redis://localhost:6379
REDIS_MAX_RETRIES_PER_REQUEST=3
REDIS_COMMAND_TIMEOUT=5000
REDIS_KEEP_ALIVE=30000
REDIS_SOCKET_TIMEOUT=0
REDIS_MAX_RETRY_DELAY=3000
REDIS_USE_AZURE_IDENTITY_AUTH=false
3. Inject and use the Redis client
import { Injectable, Inject } from '@nestjs/common'
import { REDIS_CLIENT } from '@parloa/ts-redis'
import Redis from 'ioredis'
@Injectable()
export class MyService {
constructor(@Inject(REDIS_CLIENT) private readonly redis: Redis) {}
async setValue(key: string, value: string): Promise<void> {
await this.redis.set(key, value)
}
async getValue(key: string): Promise<string | null> {
return this.redis.get(key)
}
}
Advanced Usage
Custom Injection Token
You can use a custom injection token instead of the default REDIS_CLIENT:
const MY_REDIS = Symbol('MY_REDIS')
@Module({
...makeRedisModuleMetadata(MY_REDIS),
})
export class AppModule {}
Multiple Redis Instances
To use multiple Redis instances with different configurations, use custom injection tokens and environment variable prefixes:
const INSTANCE_1_REDIS = Symbol('INSTANCE_1_REDIS')
const INSTANCE_2_REDIS = Symbol('INSTANCE_2_REDIS')
@Module({
...makeRedisModuleMetadata(INSTANCE_1_REDIS, 'INSTANCE_1_REDIS'),
...makeRedisModuleMetadata(INSTANCE_2_REDIS, 'INSTANCE_2_REDIS'),
})
export class AppModule {}
Then configure separate environment variables:
INSTANCE_1_REDIS_URL=redis://localhost:6379
INSTANCE_2_REDIS_URL=redis://localhost:6380
Azure Managed Identity
The library supports Azure Managed Identity authentication for Azure Redis Cache, which eliminates the need for managing passwords.
Configuration
REDIS_URL=rediss://your-redis.redis.cache.windows.net:6380
REDIS_USE_AZURE_IDENTITY_AUTH=true
REDIS_TOKEN_REFRESH_BUFFER_MS=300000
How it works
- The library automatically obtains and refreshes Azure access tokens
- Tokens are refreshed before expiration (default: 5 minutes before expiry)
- On authentication errors, the library automatically attempts to re-authenticate
- All token management is handled transparently
Ownership
This library is maintained by the Conversation Platform team. For questions or support, please reach out to the team.