Skip to content

[Backend] Cache invalidation: replace JSON.stringify equality with stable hash comparison #327

@Baskarayelu

Description

@Baskarayelu

Description

invalidateCache in src/cache/invalidation.ts detects stale reads using JSON.stringify(staleCheck) !== JSON.stringify(freshData). This is fragile: key ordering, undefined vs missing keys, and BigInt/Date serialization produce false positive or false negative stale detections, and the default verifyFn therefore mis-reports recordStaleCacheRead. We should compare via a stable, type-aware hash (reuse src/utils/hash.ts) so stale-read metrics are trustworthy.

Requirements and context

  • Replace JSON.stringify comparison in src/cache/invalidation.ts with a canonical hash from src/utils/hash.ts.
  • Handle BigInt, Date, and key-order normalization in the canonicalizer.
  • Keep the custom verifyFn override path working.
  • Ensure recordStaleCacheRead only fires on genuine divergence.

Suggested execution

Create branch feature/stable-cache-stale-detection.

  • Add a canonical serializer to src/utils/hash.ts.
  • Modify src/cache/invalidation.ts.
  • Add src/cache/invalidation.test.ts for ordering/BigInt/Date cases.
  • Update docs/CACHE_INVALIDATION.md.

Test and commit

Run npm run test. Edge cases: reordered keys equal, BigInt fields, nested objects, null vs undefined. Security: no sensitive data logged in stale warnings.

Example commit message

feat: use stable canonical hash for cache stale-read detection

Guidelines

  • Minimum 95% test coverage
  • Clear documentation in docs/CACHE_INVALIDATION.md
  • Timeframe: 96 hours

Metadata

Metadata

Assignees

Labels

Stellar WaveIssues in the Stellar wave programbackendBackend services and APIsobservabilityMetrics, logging, tracingperformancePerformance

Type

No fields configured for Task.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions