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-pyfor 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
CacheProtocolabstraction (can swap for Memcached, etc.) - Connection pooling via
redis-py
Windows Alternatives
| Option | License | Notes |
|---|---|---|
| Memurai | Commercial (free tier) | Full Redis compatibility |
| Garnet | MIT (Microsoft) | Redis-compatible, .NET-based |
| Docker Redis | Open Source | Works via WSL2/Docker Desktop |
Alternatives Considered
| Option | Pros | Cons |
|---|---|---|
| Chosen: Redis | Standard, rich features, ecosystem | Windows complexity |
| Memcached | Simple, fast | No data structures, no TTL on read |
| NATS KV | Single infrastructure | Limited features, new |
| DragonflyDB | Redis-compatible, faster | Less mature |
| Local Memory | No dependencies | Not distributed, lost on restart |