Lab / 03 · Caching
A cache-aside pattern checks Redis before touching the database. The first request takes 70+ms to query PostgreSQL; subsequent requests hit in-memory Redis in <2ms with an instant X-Cache: HIT.
On the live site, these requests hit real Spring Boot endpoints querying Redis with atomic TTLs and PostgreSQL fallback.
Read requests check in-memory Redis first. On a cache miss, data is queried from PostgreSQL and written back to Redis with an atomic TTL.
Cache-aside keeps application logic in control of fallback behavior, retry logic, and fallback serialization if Redis is temporarily unreachable.
A 30s TTL guarantees data eventually becomes consistent even if an invalidation message is dropped during network partitions.
When high-frequency keys expire, hundreds of concurrent threads can hit the DB simultaneously. Mutex locking or probabilistic early recomputation (XFetch) protects hot keys.