Skip to content

feat(apis): Redis-backed cache for market and position data #177

@IbrahimIjai

Description

@IbrahimIjai

Context

The current market/position cache is in-process (Arc<RwLock>). This means:

  • Cache is lost on every server restart (cold boot hits Stellar RPC with N markets x M calls)
  • Running multiple API replicas behind a load balancer means each has a stale independent cache

Replacing with Redis gives persistence across restarts and a shared cache across replicas.

Implementation

Add a Redis client (using deadpool-redis) as an AppState dependency.

Cache keys and TTLs:

  • markets_list -> 30s
  • market:{address} -> 15s
  • positions:{account} -> 10s
  • orders:{account} -> 10s
  • stats -> 60s

Fallback: if Redis is unreachable on startup, fall back to in-memory cache and log a warning (degraded mode, not a hard failure).

Env vars: REDIS_URL (default redis://localhost:6379), REDIS_CACHE_ENABLED=true|false.

Acceptance criteria

  • All cached endpoints read from and write to Redis
  • TTLs enforced by Redis EXPIRE, not application-level timers
  • Graceful degradation to in-memory cache when Redis is unavailable
  • docker-compose.yml already includes Redis service — wire it into apis startup
  • Integration test: populate Redis cache, restart server, verify cache hit
  • REDIS_CACHE_ENABLED=false disables Redis entirely (useful for local dev without Docker)

Metadata

Metadata

Assignees

No one assigned

    Labels

    apisAxum API server crateenhancementNew feature or requestinfrastructureCI, deploy scripts, tooling

    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