Skip to content

Add periodic expired session cleanup job to prevent Redis memory growth #885

Description

@RUKAYAT-CODER

Overview

src/session/session.service.ts stores sessions in Redis, but if sessions use Redis TTL for expiry, Redis handles cleanup automatically. However, any secondary indexes (e.g. user:sessions:{userId} sorted sets) are not cleaned up when session keys expire via TTL, causing the index sets to grow indefinitely with references to non-existent session keys.

Specifications

Features:

  • Periodically reconcile session index sorted sets against actual session key existence.
  • Remove stale members from user session indexes.

Tasks:

  • Create SessionCleanupTask scheduled every hour.
  • For each user:sessions:{userId} sorted set, scan members and check if the session key exists.
  • Remove members whose session keys no longer exist.
  • Add a session_cleanup_removed_total Prometheus counter.

Impacted Files:

  • src/session/session.service.ts
  • New src/session/tasks/session-cleanup.task.ts

Acceptance Criteria

  • Stale session index members are removed within 1 hour of session expiry.
  • Cleanup task handles Redis failures gracefully (logs error, does not crash).
  • Prometheus counter increments with each removed stale member.

Metadata

Metadata

Assignees

Labels

Stellar WaveIssues in the Stellar wave programbugSomething isn't workingperformance

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions