Skip to content

Prefix option #1657

Closed
Closed

Description

@joacub

prefix option is not longer available, what is the suggestion for prefix the keys and not duplicate with other script in the same server.

Thanks in advance

Activity

  1. leibale commented on Sep 17, 2021

    @leibale
    Contributor

    something like:

    const REDIS_PREFIX = 'prefix';
    
    function redisKey(key) {
      return `${REDIS_PREFIX}:${key}`;
    }
  2. wmantly commented on Jan 3, 2022

    @wmantly

    @leibale The removal of the prefix option was a terrible idea. I am down grading because of it. Please rethink this and add it back.

  3. samypr100 commented on Jan 17, 2022

    @samypr100

    @leibale The prefix option was particularly useful when other libraries allowed you to provide a redis client as a substitute for their own (e.g bulljs). Thus, allowing you to prefix those libraries created keys. This implicitly allowed you to namespace their keys as well which normally would not be possible. It's something ioredis support out of the box

  4. leibale commented on Jan 17, 2022

    @leibale
    Contributor

    @samypr100
    We've decided to remove this feature because:

    const PREFIX = 'prefix:';
    
    const client = createClient({
      prefix: PREFIX
    });
    
    for (const key of client.scanIterator('*')) {
      // key is `prefix:${key}`
      await client.get(key); // will run `GET prefix:prefix:${key}` instead of `GET prefix:${key}`
      await client.get(key.substring(PREFIX.length)); // works, but...
    }

    Regarding bull - here is a quote from the PATTERNS.md file: "do not set a keyPrefix on the connection you create, use bull's built-in prefix feature if you need a key prefix"

    https://github.com/OptimalBits/bull/blob/16fdbe90a05ae2e2bcb134a550a97dc84dfe573e/lib/queue.js#L140

  5. samypr100 commented on Jan 17, 2022

    @samypr100

    Fair, thanks for the clarification. I'm lucky that most of my other dependencies take a prefix option at this time, so I'm okay for the time being :)

  6. gajus commented on May 12, 2025

    @gajus
    Contributor

    Migrating from ioredis to this is a major PITA, e.g. we used to use prefix option to namespace our databases for integration tests.

    const redis = new Redis(CAS_REDIS_DSN, {
      keyPrefix: 'contra-test:' + TEST_RUN_ID + ':',
    });

    Now we need to figure out odd hacks to make it work.

  7. gajus commented on May 12, 2025

    @gajus
    Contributor

    Had to come up with this monstrosity:

    /**
     * https://github.com/redis/node-redis/issues/1657#issuecomment-2873493460
     */
    const createRedisDatabaseAssigner = async ({ url }: { url: string }) => {
      const redisMain = (await createClient({
        // We use database 0 as a main database for test run id storage.
        database: 0,
        url,
      }).connect()) as RedisClientType;
    
      return {
        assignDatabase: async ({ testRunId }: { testRunId: string }) => {
          let assignedDatabase = (await redisMain.get(
            `assigned-database:${testRunId}`,
          ))
            ? Number(await redisMain.get(`assigned-database:${testRunId}`))
            : null;
    
          let newAssignedDatabase = false;
    
          if (!assignedDatabase) {
            const lastDatabase = (await redisMain.get('last-database'))
              ? Number(await redisMain.get('last-database'))
              : 0;
    
            // By default there are 16 databases (indexed from 0 to 15) and
            // you can navigate between them using select command.
            // Beware that this means that this concurrency control will break
            // if we run more than 16 tests concurrently.
            assignedDatabase = lastDatabase > 14 ? 1 : lastDatabase + 1;
    
            await redisMain.set('last-database', assignedDatabase);
            await redisMain.set(`assigned-database:${testRunId}`, assignedDatabase);
    
            newAssignedDatabase = true;
          }
    
          const redis = (await createClient({
            database: assignedDatabase,
            url,
          }).connect()) as RedisClientType;
    
          if (newAssignedDatabase) {
            await redis.flushDb();
          }
    
          return redis;
        },
      };
    };
  8. SamuelGaona commented on Jul 7, 2025

    @SamuelGaona

    +1

  9. magidandrew commented on Sep 10, 2025

    @magidandrew

    I switched off of node-redis for ioredis because of this

    Our use case required many processes to talk to the same db, each with isolated keys.

    Yes, we lost redis time series support on ioredis, but it solves the massive headache which is to keep all the processes (which don't know about each other) have unique keyspacing

  10. gajus commented on Jul 3, 2026

    @gajus
    Contributor

    Key prefixing is back.

  11. mifi commented on Aug 5, 2026

    @mifi
    Contributor

    yea the option is now called keyPrefix, just like in ioredis (but I think it gets ignored for sendCommand which is not the case in ioredis)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions