Stop 11 · CachingTopics · 2/8

Redis for Caching

Part of the Backend Developer Roadmap

In short

Your app runs on four servers. A cache inside each process would give you four separate, mismatched copies. Redis is one shared, in-memory store that every server can read from.

Using Redis's speed and TTL (time-to-live) support to cache database query results, computed values, or API responses — the most common caching backend for Node.js apps.

The blob character sticks notes with little timers on a fridge door as one old note falls off.

Think of it like this

Sticky notes with timers

Imagine a fridge door where the whole family sticks quick notes, each with a tiny kitchen timer. Anyone can read them. When a timer rings, that note falls off by itself. Redis is that fridge door for your servers: shared notes, with expiry built in.

Watch it happen

Miss, fill, then hit

API
Redis
Postgres

GET product:42

1/6The API asks Redis for the cached product first.

The big ideas

What you need to know

In memory, and shared

Redis keeps data in RAM on its own server, so all your app instances see the same cache.

Expiry is built in

SET key value EX 300 makes Redis delete the key after 300 seconds. No cleanup code needed.

Name keys clearly

Use patterns like product:42 or user:7:orders, so you can find and delete related keys later.

Set a memory limit

Configure maxmemory and an eviction policy like allkeys-lru, so Redis drops old keys instead of refusing writes.

See it in code

Walk through the code

ts
1import { createClient } from "redis";2 3const redis = createClient({ url: process.env.REDIS_URL });4await redis.connect();5 6export async function getProduct(id: number) {7  const key = `product:${id}`;8  const hit = await redis.get(key);9  if (hit) return JSON.parse(hit);10 11  const product = await db.products.find(id);12  if (product) await redis.set(key, JSON.stringify(product), { EX: 300 });13  return product;14}

1/3Create one client and connect once at startup, not on every request.

Run Redis locally with docker run -p 6379:6379 redis, then watch commands arrive with redis-cli MONITOR.

Save yourself the pain

Common mistakes

Oops

Storing cache entries with no expiry at all.

Do this instead

They pile up and go stale forever. Give every cache key a TTL, even a long one.

Oops

Letting the whole app fail when Redis is down.

Do this instead

A cache is an optimisation. On Redis errors, log it and fall back to the database.

Your mission

Cache a slow endpoint in Redis

~30 min

Quick check

What does SET product:42 '{...}' EX 300 do?

Go deeper

  1. 1Cache a slow database query's result in Redis with a sensible TTL.
  2. 2Measure the latency difference between hitting the cache vs the database.
  3. 3Learn cache key naming conventions to avoid collisions across different data types.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge