Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

NestJS cache.set works but Redis shows no keys

Errorcache.set returns without error but redis-cli KEYS * is empty

Short answer

Check three things. The Redis store must be passed in the stores list; otherwise nothing talks to Redis. The ttl is in milliseconds, so ttl 60 lasts 60 ms. And a namespace adds a prefix, so search with a pattern. Add an error listener on the store to see connection failures.

  • Applies to NestJS 11, @nestjs/cache-manager, @keyv/redis
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Set a key with a long ttl and look for it in Redis with a pattern.

    // in a service: await this.cache.set('probe', 'ok', 600000);
    redis-cli --scan --pattern '*probe*'
  2. 2Match what you see, then jump to its fix

    1. 1The key never appears in Redis, and no connection error is loggedThe Redis store is not in the stores listFix
    2. 2Keys exist for a moment, or get() works for one request and then returns undefinedThe ttl is in milliseconds, not secondsFix
    3. 3redis-cli GET probe returns nil, yet the application reads the value backA namespace prefix hides the key from a plain lookupFix

What does the error look like?

  • cache.set returns without error but redis-cli KEYS * is empty
  • cache.get returns undefined right after set

Cause 1 of 3

The Redis store is not in the stores list

1Check

The Redis adapter is only used when you create it with createKeyv and give it to CacheModule in stores. The first entry in stores is the default.

grep -rn "CacheModule" src

2Fix

Register the module with the Redis store, and attach an error listener.

import { createKeyv } from '@keyv/redis';

CacheModule.registerAsync({
  isGlobal: true,
  useFactory: async () => {
    const redis = createKeyv('redis://localhost:6379');
    redis.on('error', (err) => console.error('Redis error:', err));
    return { stores: [redis] };
  },
})

3Verify

Set a key and find it.

redis-cli --scan --pattern '*probe*'

Confirmed by docs.nestjs.com

Did this fix it?

Cause 2 of 3

The ttl is in milliseconds, not seconds

1Check

A value such as 60 or 3600 is read as milliseconds, so the key expires after 60 ms or 3.6 seconds.

grep -rn "cache.set\|ttl" src

2Fix

Multiply by 1000, in the module default and in each set call.

await this.cache.set('user:1', data, 60 * 1000); // 60 seconds

3Verify

The key is still there after a few seconds, and Redis reports a TTL.

redis-cli --scan --pattern 'user:1*' | xargs -I{} redis-cli pttl {}

Confirmed by docs.nestjs.com

Did this fix it?

Cause 3 of 3

A namespace prefix hides the key from a plain lookup

1Check

When a namespace is set on the Keyv instance, stored keys look like namespace::key, with :: as the default separator.

redis-cli --scan --pattern '*::*'

2Fix

Search with the prefix, or set the namespace yourself so it is the one you expect.

const redis = createKeyv('redis://localhost:6379', { namespace: 'my-app' });
// stored as my-app::probe

3Verify

The prefixed key is found.

redis-cli --scan --pattern 'my-app::*'

Confirmed by keyv.org

Did this fix it?

How do I stop it coming back?

  • Always attach an error listener to the Redis store, as the Nest docs say.
  • Write ttl values as seconds * 1000 so the unit is visible.
  • Store plain data; values are serialised to JSON, so class instances come back as plain objects.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

req.session is undefined in an Express route

TypeError: Cannot set properties of undefined (setting 'user')

express-session only attaches req.session when its middleware runs first and can reach its store.

3 causes, each with a fix

Back end

NestJS GraphQL cannot work out the output type of a field or query

Cannot determine a GraphQL output type for the "<name>". Make sure your class is decorated with an appropriate decorator.

In code-first mode Nest reads TypeScript types through decorators, and arrays, promises and unions disappear at runtime.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.