Stop 07 · NoSQL DatabasesTopics · 7/12

Redis Basics

Part of the Backend Developer Roadmap

In short

Redis keeps data in memory, so reads and writes often finish in under a millisecond. Backends lean on it for caching, sessions, rate limits and simple queues.

An extremely fast in-memory key-value store, commonly used for caching, session storage, rate limiting and pub/sub — not typically your primary database, but a critical supporting one.

The blob grabs popular items from a small counter tray, with a big storeroom far behind.

Think of it like this

The paan shop's front counter

The paan wala keeps his fastest sellers on the counter, within arm's reach. The big sacks sit in the godown at the back. Redis is that counter: small, very fast memory for things you need again and again, while your main database is the godown.

Watch it happen

Cache miss, then cache hit

API
Redis
Postgres

GET menu:today

1/6The API checks Redis first for today's menu.

The big ideas

What you need to know

Lives in memory

Data sits in RAM, which is why it's fast. It can save to disk, but treat it as a helper, not your main store.

Expiry built in

Give any key a time to live with EX. Redis deletes it when the time is up.

More than strings

Lists, hashes, sets, sorted sets and streams. Sorted sets make leaderboards almost trivial.

One command at a time

Redis runs commands one after another, so INCR is atomic. Perfect for counters and rate limits.

See it in code

Walk through the code

ts
1import { createClient } from "redis";2 3const redis = createClient({ url: "redis://localhost:6379" });4await redis.connect();5 6export async function getMenu() {7  const cached = await redis.get("menu:today");8  if (cached) return JSON.parse(cached);9 10  const menu = await db.query("SELECT name, price FROM dishes WHERE available");11  await redis.set("menu:today", JSON.stringify(menu.rows), { EX: 60 });12  return menu.rows;13}

1/4Connect to Redis on its default port, 6379.

This is the cache-aside pattern. db is a pg Pool; run Redis locally with docker run -p 6379:6379 redis.

Save yourself the pain

Common mistakes

Oops

Caching without an expiry and serving week-old prices.

Do this instead

Always set EX, and delete the key when the underlying data changes.

Oops

Keeping the only copy of important data in Redis.

Do this instead

Memory is limited and persistence settings vary. Keep the truth in your main database.

Your mission

Cache a slow endpoint

~25 min

Quick check

Why is INCR safe for a page-view counter even with thousands of requests at once?

Go deeper

  1. 1Install Redis locally and practice `SET`/`GET`/`EXPIRE` in the CLI.
  2. 2Store a computed value in Redis with a TTL to see automatic expiration.
  3. 3Learn why Redis being in-memory makes it fast but requires care around persistence.

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