Skip to content

Redis

Overview

@modularityjs/redis provides a shared ioredis client used by all Redis-backed drivers (cache-redis, lock-redis, queue-redis). It manages the connection lifecycle — connecting during boot and disconnecting on shutdown.

Setup

typescript
import { RedisModule } from '@modularityjs/redis';

const modules = [
  RedisModule.forRoot({
    host: 'localhost',
    port: 6379,
  }),
  // ... Redis drivers
];

Configuration

OptionDefaultDescription
host'localhost'Redis server hostname
port6379Redis server port
password-Authentication password
db0Database index
keyPrefix'modularityjs:'Prefix for all keys (used by drivers, not ioredis)
lazyConnectfalseDefer connection until first command

Key Prefixing

All Redis drivers build keys using the pattern:

{RedisConfig.keyPrefix}{driver namespace}{key}

For example, with default config:

  • Cache key user:1 becomes modularityjs:cache:user:1
  • Lock key payment:123 becomes modularityjs:lock:payment:123
  • Queue stream orders becomes modularityjs:queue:stream:orders

The prefix is not passed to ioredis's keyPrefix option (which breaks Lua scripts). Instead, each driver builds the full key manually for consistency across all Redis commands including eval.

Lifecycle

  • onInit — connects to Redis. If the connection fails, boot fails immediately.
  • onShutdown — gracefully disconnects via QUIT.