Skip to main content

ADR-0004: Use Redis for Caching

  • Status: Accepted
  • Date: 2025-12-29
  • Deciders: @anshpansuriya14
  • Supersedes: N/A
  • Superseded by: N/A

Context

Framework M needs a caching layer for:

  • Session data (if stateful auth is used)
  • Query result caching
  • Rate limiting counters
  • Temporary data storage
  • Distributed locks

Forces at play:

  • Performance: Sub-millisecond reads
  • TTL Support: Automatic expiration
  • Cross-Platform: Must work on Windows (development)
  • Data Structures: Need more than key-value (counters, sets, sorted sets)
  • Industry Standard: Developers expect Redis

Decision

We will use Redis via redis-py for caching.

For Windows development, we recommend Memurai (commercial) or Garnet (Microsoft open-source).

from redis.asyncio import Redis

cache = Redis.from_url("redis://localhost:6379")

# CacheProtocol implementation
await cache.set("user:123:profile", json.dumps(profile), ex=3600)
profile = json.loads(await cache.get("user:123:profile"))

Consequences

Positive

  • Industry Standard: Most developers know Redis
  • Rich Data Structures: Strings, hashes, lists, sets, sorted sets, streams
  • TTL Native: Built-in expiration, no cleanup jobs needed
  • Ecosystem: Great tooling, monitoring, managed services (ElastiCache, Upstash)
  • Atomic Operations: INCR, DECR, SETNX for counters and locks

Negative

  • Separate System: Not same as NATS (jobs/events)
  • Windows Caveat: Native Redis doesn't support Windows; alternatives needed
  • Memory Bound: Data must fit in RAM

Neutral

  • Uses CacheProtocol abstraction (can swap for Memcached, etc.)
  • Connection pooling via redis-py

Windows Alternatives

OptionLicenseNotes
MemuraiCommercial (free tier)Full Redis compatibility
GarnetMIT (Microsoft)Redis-compatible, .NET-based
Docker RedisOpen SourceWorks via WSL2/Docker Desktop

Alternatives Considered

OptionProsCons
Chosen: RedisStandard, rich features, ecosystemWindows complexity
MemcachedSimple, fastNo data structures, no TTL on read
NATS KVSingle infrastructureLimited features, new
DragonflyDBRedis-compatible, fasterLess mature
Local MemoryNo dependenciesNot distributed, lost on restart

References