Redis Basics

Lesson 2 of 5

The Ephemeral Lead

Concept:

Strings are Redis's simplest data type: one key, one value. But with SETEX (SET + EXpiry), they become powerful for real-world use cases. Every website that remembers your login uses something like this — session tokens stored with a TTL that auto-expire. Rate limiters count requests per IP with expiring keys. Caches store expensive database query results for a few minutes. The pattern is always the same: SET a key, let Redis auto-delete it after N seconds.
Detective Indecks: Indecks.
REDIS: Got a new lead for you, detective. A shadowy figure, `user789`. We need to track them, but only when they're active. We can't waste resources tailing them when they go dark.
Detective Indecks: So I need a way to put a temporary tag on them. What's the tool?
REDIS: The simplest tool in the box. A basic key-value pair. You give me a key, like `online:user789`, and a value, like 'active'. My `SET` command handles it. If you need to check the status, you just ask me with `GET online:user789`.
Detective Indecks: But you said temporary. What if I forget to remove the tag? The lead goes cold, but my board still says they're active. Sloppy work.
REDIS: That's why you don't use `SET`. You use `SETEX`—SET with Expiry. You give me the key, the number of seconds to keep the lead alive, and the value. For example: `SETEX online:user789 300 "active"`. In 5 minutes (300 seconds), that record vanishes. No loose ends. No sloppy work.
Detective Indecks: Clean. I like it. Let's put it to the test.
Example Code:
SETEX online:user789 300 "active"

Your Assignment

A new person of interest, `user456`, just came online. Mark their status as 'online' but make sure the lead goes cold (expires) in exactly 2 minutes (120 seconds).

Redis Console
redis>