Skip to content

Backend: Expand Redis caching beyond rate limiting (campaign list, stats, config) #304

@CelestinaBeing

Description

@CelestinaBeing

Summary

Redis is integrated only for the rate limiter store. The current short-cache for campaign lists is an in-memory Map that resets on restart and does not share state across multiple backend instances. For horizontal scaling, the shared Redis cache must serve campaign list, stats, and config responses.

Problem

In backend/src/index.js:

const shortCache = new Map();  // in-memory, single-instance only
  • Every new process starts with a cold cache
  • Multi-instance deployments (Docker Compose scale, Fly.io, Kubernetes) each have independent caches
  • Cache stampede risk during Redis-unavailable period (no fallback TTL logic)

Acceptance Criteria

  • Create backend/src/cache.js that exports a unified cache interface: get(key), set(key, value, ttlMs), del(key), flush(prefix)
  • Implementation: uses Redis when REDIS_URL is set, falls back to in-memory Map otherwise (same as rate limiter pattern)
  • Replace all shortCache.get/set/clear call sites in index.js with the new cache interface
  • Cache GET /api/v1/campaigns responses with key that includes query params hash (TTL: SHORT_CACHE_TTL_MS)
  • Cache GET /api/v1/config (TTL: 5 minutes — rarely changes)
  • Invalidate all campaigns:* cache keys on POST/PUT/DELETE campaign operations
  • Add a GET /api/v1/cache/stats endpoint (admin-only) showing hit/miss counts and key count
  • Add unit tests for cache get/set/invalidation with both backends (mock Redis + in-memory)

References

  • backend/src/index.jsshortCache usage
  • backend/src/middleware/rateLimit.js — Redis integration pattern
  • backend/.env.exampleREDIS_URL

Metadata

Metadata

Assignees

Labels

Stellar WaveIssues in the Stellar wave program

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