Skip to content

feat(backend): Add Redis Caching Layer for API Responses #263

Description

@devJaja

Summary

Integrate Redis (or in-memory LRU cache) for caching frequently accessed API responses like agent listings, stats, and health checks.

Motivation

Every API request hits the SQLite database. Caching hot data reduces database load and improves response times for repeated queries.

Acceptance Criteria

  • Cache middleware that stores API responses with configurable TTL
  • Cache invalidation on data mutations (agent registration, task creation)
  • Cache keys follow pattern: api:{method}:{path}:{query_hash}
  • Cache-aside pattern: check cache first, fallback to DB, update cache
  • Cache hit/miss metrics logged via Pino
  • Configurable TTL per endpoint (agents: 60s, stats: 30s, health: 10s)
  • Cache bypass option via X-Cache-Bypass header
  • Unit tests for cache hit, miss, invalidation, and TTL expiry

Files to Modify

File Action
backend/src/api/middleware/cache.ts New: cache middleware
backend/src/cache/index.ts New: cache client (LRU or Redis)
backend/src/cache/invalidation.ts New: cache invalidation logic
backend/src/api/routes/agents.ts Add cache middleware to GET endpoints
backend/src/api/routes/stats.ts Add cache middleware
backend/src/api/routes/health.ts Add cache middleware
backend/src/config/index.ts Add cache configuration

How to Contribute

  1. Create branch feat/api-response-caching
  2. Implement LRU cache client (or Redis integration)
  3. Build cache middleware with TTL support
  4. Add cache invalidation to mutation endpoints
  5. Write unit tests for cache behavior
  6. Run npm run test && npm run lint before submitting PR

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions