Skip to content

Bug: Memory is not released from in-memory cache even after TTL expires #805

Open
@yomapi

Description

@yomapi

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When using @nestjs/cache-manager (which internally relies on cache-manager v6+), in-memory caching uses Keyv as the underlying storage engine. However, Keyv employs a lazy eviction mechanism, meaning that expired entries are not automatically removed from memory unless a .get() call is explicitly made for that key.

This results in expired cache entries lingering in memory indefinitely, especially if .get() is never invoked again. Over time, this leads to increased memory usage and can potentially cause memory leaks or OOM (Out Of Memory) issues in long-running services.

Minimum reproduction code

https://github.com/yomapi/nestjs-in-memory-leak

Steps to reproduce

npm install
npm run start:memory
./test-cache.sh

Expected behavior

When a TTL is set on a cache entry, the item should be automatically removed from memory after the TTL expires — even if .get() is never called again.

This ensures proper memory usage and aligns with typical expectations of TTL-based caching, where expired items are evicted in the background rather than remaining indefinitely in memory.

Package version

@nestjs/[email protected]

NestJS version

11.1.3

Node.js version

22.11.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions