Lesson 23 of 49 intermediate

Redis — The Speed Demon

Super-Fast Temporary Storage

Open interactive version (quiz + challenge)

Real-world analogy

Your database (MongoDB) is like a filing cabinet — organized, reliable, but takes a few seconds to find things. Redis is like a sticky note on your desk — INSTANTLY readable! You put frequently needed info on the sticky note so you don't keep opening the cabinet.

What is it?

Redis is an open-source, in-memory data store used as a cache, message broker, and database. It stores data in RAM (not disk), making it incredibly fast but limited in size. Think of it as a turbo-charged, temporary storage layer.

Real-world relevance

Twitter uses Redis to cache timelines. GitHub uses it for job queues. Stack Overflow uses it for caching. Almost every high-traffic app has Redis somewhere in its stack.

Key points

Code example

// Without Redis — database gets hammered 😵
// 100 users visit the homepage in 1 minute:
// → 100 database queries (each takes 200ms)
// → Database: "I'm TIRED! 😫"

// With Redis — database chills 😎
// Request 1:
User → Backend → Redis (miss!) → Database (200ms)
              → Store in Redis (cache it!)

// Requests 2-100:
User → Backend → Redis (HIT! 2ms) → "Here's your data!"
              → Database: *sipping coffee* ☕

// Redis in your docker-compose.yml:
services:
  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"

// Using Redis with NestJS:
import { CACHE_MANAGER } from '@nestjs/cache-manager';

@Injectable()
export class CacheService {
  constructor(@Inject(CACHE_MANAGER) private cache: Cache) {}

  async getProperty(id: string) {
    // Check Redis first
    const cached = await this.cache.get(`property:${id}`);
    if (cached) return cached; // Cache HIT! ⚡

    // Cache miss — ask database
    const property = await this.prisma.property.findUnique({
      where: { id },
    });

    // Store in Redis for next time (expire in 5 min = 300,000ms)
    await this.cache.set(`property:${id}`, property, 300_000);
    return property;
  }
}

Line-by-line walkthrough

  1. 1. Without Redis — database gets hammered 😵
  2. 2. 100 users visit the homepage in 1 minute:
  3. 3. → 100 database queries (each takes 200ms)
  4. 4. → Database: "I'm TIRED! 😫"
  5. 5.
  6. 6. With Redis — database chills 😎
  7. 7. Request 1:
  8. 8.
  9. 9.
  10. 10.
  11. 11. Requests 2-100:
  12. 12.
  13. 13.
  14. 14.
  15. 15. Redis in your docker-compose.yml:
  16. 16. Docker Compose section definition
  17. 17.
  18. 18.
  19. 19.
  20. 20.
  21. 21.
  22. 22. Using Redis with NestJS:
  23. 23. Importing required dependencies
  24. 24.
  25. 25. Decorator that adds metadata or behavior
  26. 26. Exporting for use in other files
  27. 27.
  28. 28.
  29. 29.
  30. 30. Check Redis first
  31. 31. Declaring a variable
  32. 32. Conditional check
  33. 33.
  34. 34. Cache miss — ask database
  35. 35. Declaring a variable
  36. 36.
  37. 37.
  38. 38.
  39. 39. Store in Redis for next time (expire in 5 min)
  40. 40. Waiting for an async operation to complete
  41. 41. Returning a value
  42. 42. Closing block
  43. 43. Closing block

Spot the bug

async getUser(id: string) {
  const cached = await this.cache.get("user:" + id);
  if (cached) return cached;
  const user = await this.prisma.user.findUnique({ where: { id } });
  await this.cache.set("user:" + id, user);
  return user;
}
Need a hint?
What happens when the cached data gets stale?
Show answer
The cache has no expiration (TTL), so data stays forever even if the user updates their profile. Fix: add a TTL: await this.cache.set('user:' + id, user, 300_000) to expire after 5 minutes (NestJS cache-manager uses milliseconds).

Explain like I'm 5

You have a book report due. Reading the book from the library (database) takes 30 minutes. But if you write important notes on a sticky note (Redis), next time you can read your notes in 5 seconds! Redis is that super-fast sticky note for your app.

Fun fact

Redis can handle over 100,000 operations PER SECOND on a single server. That's like a librarian who can find and return 100K books every second! 📚💨

Hands-on challenge

Build a rate limiter using Redis: track how many API requests each user makes per minute. Use INCR to count requests and EXPIRE to reset the counter after 60 seconds. If a user exceeds 100 requests, return a 429 Too Many Requests response. Bonus: use a Redis sorted set to build a real-time leaderboard!

More resources

Open interactive version (quiz + challenge) ← Back to course: Full-Stack Playbook