Skip to content

perf: implement Redis caching layer for gist queries #25

Description

@snowrugar-beep

Problem Statement

Redis infrastructure is defined in Terraform (ElastiCache parameter group with redis7 and allkeys-lru eviction policy) but no caching logic is implemented in the application. Every gist query — including repeated spatial queries within the same radius — hits PostgreSQL directly, creating unnecessary read load.

Evidence

  • infrastructure/terraform/redis-parameters.tf — Redis parameter group defined
  • No Redis client or caching logic in Backend source code
  • Every findNearby, findOne call hits PostgreSQL

Impact

High read load on primary database. No read load distribution. Slower response times for repeated queries.

Proposed Solution

  1. Install ioredis and @nestjs/bull (or @liaoliaots/nestjs-redis) for NestJS Redis integration
  2. Configure Redis connection from environment variables
  3. Cache findNearby results with TTL based on query parameters (lat, lon, radius, limit)
  4. Cache findOne results by gist ID with TTL
  5. Invalidate cache on new gist creation
  6. Add cache hit/miss metrics

Technical Requirements

  • Cache key format: gist:nearby:{lat}:{lon}:{radius}:{limit}
  • Cache TTL: 60 seconds for nearby, 300 seconds for single gist
  • Must handle cache invalidation on new gist creation
  • Must gracefully degrade if Redis is unavailable
  • Must not cache paginated results incorrectly

Acceptance Criteria

  1. First query misses cache, hits DB, populates cache
  2. Second identical query returns from cache (no DB hit)
  3. New gist creation invalidates nearby cache for that area
  4. Redis unavailability does not crash the application
  5. Cache hit rate is logged/metrics available
  6. All existing tests pass

File Inventory

  • Backend/src/gists/gists.service.ts
  • Backend/src/gists/gist.repository.ts
  • Backend/src/app.module.ts
  • Backend/package.json

Dependencies

None.

Testing Strategy

  • Unit test: service layer with mocked Redis
  • Integration test: running Redis, verify cache behavior
  • Performance test: compare response times with and without cache

Security Considerations

Cache should not store sensitive data. Redis should require authentication in production.

Definition of Done

  • Redis client configured
  • Caching logic implemented
  • Cache invalidation working
  • Graceful degradation on Redis failure
  • Tests passing

Metadata

Metadata

Assignees

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