NoSQL: Key-Value & Caching with Redis
Key-value stores are the simplest database model: you store a value under a key and retrieve it by that key. Redis is the dominant in-memory key-value store — it lives entirely in RAM, which is why operations complete in sub-millisecond time.
Redis supports more than simple strings. Hashes store structured objects, sorted sets power leaderboards and rate limiters, lists work as queues, and pub/sub enables real-time messaging. The most common pattern is a cache in front of a slower database: check Redis first, fall through to Postgres on a miss, then write the result back to Redis with a TTL so stale data eventually expires.
Exercise
Implement cache-aside for a getById: check Redis, on miss query the DB, SETEX with a TTL, return the value. Add a note on what happens if the DB row is updated while the key is still cached.