Databases
Redis
Redis is an in-memory key-value database used for caching and lightning-fast (~1ms) data lookups.
Diagram
Request flow with Redis cache:
────────────────────────────────────────────
Request for user_42
│
▼
Redis.get("user:42")
│
┌────┴───────────────────┐
│ HIT? │ MISS?
▼ ▼
Return instantly (<1ms) Query Postgres (~20ms)
Redis.set("user:42", data, TTL=300)
Return dataIn Depth
Redis is an open-source, in-memory key-value database. Unlike disk databases (Postgres), Redis holds its dataset in RAM, enabling read and write operations in under a millisecond.
Code Example
Quick caching wrapper in Node.js
import { createClient } from 'redis';
const redis = createClient();
async function getCachedData(key, fetchFromDbFunc) {
const cached = await redis.get(key);
if (cached) return JSON.parse(cached); // Cache HIT!
const freshData = await fetchFromDbFunc();
await redis.setEx(key, 300, JSON.stringify(freshData)); // Cache with 5m TTL
return freshData;
}Related Terms