Redis β In-Memory Data Structure Store
Before you start: basic familiarity with a general-purpose database (what a key-value lookup is) helps but isn't required β Redis's data structures are explained from scratch below.
Redis stores data in RAM, making it 100-1000Γ faster than disk databases. It's the Swiss Army knife of backend infrastructure β used as a cache, message queue, session store, rate limiter, leaderboard, and pub/sub system.
Why This Exists (The Hook)
A disk-backed database like PostgreSQL is durable but has a physical floor on how fast it can respond β every read may mean a disk seek. Many real workloads (checking a user's session, incrementing a page-view counter, checking a rate limit) don't need that durability guarantee; they need an answer in microseconds and can tolerate losing that specific value if the server restarts. Redis exists to serve exactly that class of problem: it keeps everything in RAM, so read/write latency drops to sub-millisecond, at the cost of needing its own strategy (snapshots, replication) if you want that data to survive a crash.
Analogy β Think of Redis like the sticky notes on your desk versus the filing cabinet across the room. The filing cabinet (a disk-backed database) is where you keep everything permanently β safe, organized, but a walk away every time you need something. Sticky notes (Redis) are for the things you need constantly and right now β today's task list, a running tally β instantly available on your desk, but if the office burns down tonight, the sticky notes are gone while the fireproof cabinet's contents survive. You use both, for different kinds of data.
Try it (2 minutes) β Reason through why KEYS user:* is dangerous in production without running anything: Redis is single-threaded for command execution, meaning while it's doing one thing, it can't do anything else β including serving other clients' requests. KEYS has to scan every single key in the entire database to find matches. On a Redis instance holding 10 million keys, what happens to every other request trying to read a session or check a rate limit while that scan is running? (This is exactly why SCAN exists as a cursor-based alternative β it works in small increments instead of one blocking pass.)
What Redis Is Used For
| Use Case | Redis Feature | Example |
|---|
|---|---|---|
| **Caching** | String/Hash | Cache API responses, user sessions |
|---|---|---|
| Session Store | String + TTL | Web session storage |
| Rate Limiting | Incr + TTL | 100 requests/minute per user |
| Leaderboards | Sorted Set | Game rankings by score |
| Message Queue | List (BRPOP) | Job queues, task workers |
| Pub/Sub | Pub/Sub | Real-time notifications |
| Distributed Lock | SET NX PX | Prevent duplicate processing |
| Counting | Incr | Page views, analytics counters |

