Cache
Cache goes where reads vastly outnumber writes and the answer can be a few minutes old. Redis with cache-aside and TTL, evicted on the write path. The database stays the source of truth.
Decisions and why
- Cache-aside with TTL
- If Redis is down, the app reads from the database and keeps working. The TTL bounds the damage of any forgotten eviction.
- Evict after commit
- Evicting before the commit lets another request put the old value back.
- Redis for shared state too
- Rate limits, distributed locks and sessions live in Redis so every instance sees them: that is what keeps the API stateless.
- Do not cache what always changes
- A cache with a low hit rate only adds latency and inconsistency.
requestresponse
Pick a scenario above to watch the message travel through the architecture.