NestJS cache.set works but Redis shows no keys
cache.set returns without error but redis-cli KEYS * is emptyShort 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.
Fix it fast
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*'2Match what you see, then jump to its fix
- 1
The key never appears in Redis, and no connection error is loggedThe Redis store is not in the stores listFix - 2
Keys exist for a moment, or get() works for one request and then returns undefinedThe ttl is in milliseconds, not secondsFix - 3
redis-cli GET probe returns nil, yet the application reads the value backA namespace prefix hides the key from a plain lookupFix
- 1
What does the error look like?
cache.set returns without error but redis-cli KEYS * is emptycache.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" src2Fix
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
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" src2Fix
Multiply by 1000, in the module default and in each set call.
await this.cache.set('user:1', data, 60 * 1000); // 60 seconds3Verify
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
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::probe3Verify
The prefixed key is found.
redis-cli --scan --pattern 'my-app::*'Confirmed by keyv.org
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
Databases
node-redis throws The client is closed when you send a command
ClientClosedError: The client is closed
node-redis throws ClientClosedError when the client's socket is not open.
3 causes, each with a fixBack end
TypeScript cannot find module '@nestjs/core' or its type declarations
error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.
TS2307 means TypeScript searched node_modules and found neither the package nor its types.
3 causes, each with a fixBack end
Celery in a Django project does not connect to Redis or ignores the broker URL
kombu.exceptions.InconsistencyError: ... _kombu.binding.celery
Celery talks to Redis through a client library and reads its broker URL from your Django settings.
4 causes, each with a fixBack end
Node.js in Docker Compose cannot reach MySQL on 127.0.0.1:3306
Error: connect ECONNREFUSED 127.0.0.1:3306
Inside a container, 127.0.0.1 is that container, not MySQL.
3 causes, each with a fixBack 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 fixBack 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