DEV Community

Cover image for Redis in NestJS: The RedisX Solution You Didn't Know You Needed
Suren Krmoian
Suren Krmoian

Posted on

Redis in NestJS: The RedisX Solution You Didn't Know You Needed

Redis in NestJS: The RedisX Solution You Didn't Know You Needed

Tired of wrangling with multiple libraries for Redis operations in your NestJS app? Meet NestJS RedisX. It’s the all-in-one toolkit for all your Redis needs — caching, locking, rate limiting, and more — without the hassle of multiple connections.

Why RedisX?

If you've been struggling with managing separate Redis connections for different functions, RedisX is your new best friend. It offers a modular approach where everything runs off a single Redis connection. That means less complexity and centralized configuration.

Plugin Power

RedisX is all about plugins. Install just what you need and keep things lean:

  • @nestjs-redisx/cache: L1+L2 caching with some nifty features like stampede protection.
  • @nestjs-redisx/locks: Distributed locks with auto-renewal.
  • @nestjs-redisx/rate-limit: Various strategies for rate limiting.
  • @nestjs-redisx/idempotency: Keep those requests idempotent.
  • @nestjs-redisx/streams: For those who love event-driven architectures.
  • @nestjs-redisx/metrics: Prometheus metrics, anyone?
  • @nestjs-redisx/tracing: OpenTelemetry tracing support.

Get Up and Running

Ready to dive in? Install the core package and any plugins you fancy:

npm install @nestjs-redisx/core @nestjs-redisx/cache @nestjs-redisx/locks
Enter fullscreen mode Exit fullscreen mode

Then, wire it up in your AppModule:

import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { RedisModule } from '@nestjs-redisx/core';
import { CachePlugin } from '@nestjs-redisx/cache';
import { LocksPlugin } from '@nestjs-redisx/locks';

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    RedisModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      plugins: [
        CachePlugin.registerAsync({
          imports: [ConfigModule],
          inject: [ConfigService],
          useFactory: (config: ConfigService) => ({
            l1: { enabled: true, maxSize: 1000, ttl: 60 },
            defaultTtl: config.get('CACHE_TTL', 300),
          }),
        }),
        new LocksPlugin({ retryAttempts: 3, retryDelay: 200 }),
      ],
      useFactory: (config: ConfigService) => ({
        clients: {
          type: 'single',
          host: config.get('REDIS_HOST', 'localhost'),
          port: config.get('REDIS_PORT', 6379),
        },
      }),
    }),
  ],
})
export class AppModule {}
Enter fullscreen mode Exit fullscreen mode

Wrapping It Up

RedisX is the toolkit that makes Redis integration a breeze in NestJS applications. No more juggling — just a clean, unified approach to Redis. Focus more on what you love: building cool features. For more insights, check out the official documentation and GitHub repository.

Top comments (0)