toeverything/AFFiNE · critical

Invalid database index: ${db}, must be between 0 and 11

Error message

Invalid database index: ${db}, must be between 0 and 11

What it means

Thrown by Redis.assertValidDBIndex() when a Redis database index greater than 15 is requested. The comment notes Redis allows [0..16) by default, and the app separates different Redis uses (cache, session, socket.io, queue) by offsetting the base db index by 0–4. A db value of 12 or higher would push these offsets past Redis's 16-database limit, so indices above 15 are rejected as invalid.

Source

Thrown at packages/backend/server/src/base/redis/instances.ts:45

  }

  async onModuleDestroy() {
    try {
      await this.quit();
    } catch {
      this.disconnect();
    }
  }

  override duplicate(override?: Partial<RedisOptions>): IORedis {
    const client = super.duplicate(override);
    client.on('error', this.errorHandler);
    return client;
  }

  assertValidDBIndex(db: number) {
    if (db && db > 15) {
      throw new Error(
        // Redis allows [0..16) by default
        // we separate the db for different usages by `this.options.db + [0..4]`
        `Invalid database index: ${db}, must be between 0 and 11`
      );
    }
  }
}

@Injectable()
export class CacheRedis extends Redis {
  constructor(config: Config) {
    super(redisOptions({ ...config.redis, ...config.redis.ioredis }));
  }
}

@Injectable()
export class SessionRedis extends Redis {
  constructor(config: Config) {

View on GitHub (pinned to 26c515e050)

Solutions

  1. Set config.redis.db to a low value (0–11) so that db + max_offset(4) stays under 16.
  2. Check the REDIS_DB environment variable and reduce it.
  3. If you need more database separation, configure separate Redis instances instead of high db indices.

Example fix

// before (.env)
REDIS_DB=13 // QueueRedis uses db 13+4=17 -> throws

// after
REDIS_DB=0 // QueueRedis uses db 0+4=4, all within [0..16)
Defensive patterns

Strategy: validation

Validate before calling

function validateRedisDb(db: number): void {
  if (db && db > 11) {
    throw new Error(`Redis db must be between 0 and 11 (got ${db}). Max offset of +4 must stay under 16.`);
  }
}
validateRedisDb(config.redis.db ?? 0);

Type guard

const isValidRedisDb = (db: number): boolean => db >= 0 && db <= 11;

Prevention

When it happens

Trigger: Constructing a Redis instance (CacheRedis, SessionRedis, SocketIoRedis, QueueRedis) or calling assertValidDBIndex(db) with db > 15. The base config.redis.db value combined with the per-class offset (+2, +3, +4) must stay under 16.

Common situations: Setting config.redis.db (or the REDIS_DB env var) to a high value like 13, which when offset by +4 for QueueRedis becomes 17, exceeding the limit. Using a Redis provider that assigns high database indices. Copying config from an environment with different Redis limits.

Related errors


AI-assisted analysis of toeverything/AFFiNE@26c515e050 (2026-08-12). Data as JSON: /api/errors/76afc6313e4a1666. Report an issue: GitHub.