Teach Redis basics
2025-11-172 turns5,206 charsgpt-5-1
Summary
The user wanted to learn the basics of Redis.
Messages
teach me redis
Alright — here’s the clean, fast, *“teach me Redis”* intro you actually need. No fluff, no tutorial-soup, just the mental model + the commands + the patterns so you can use Redis **as a developer**, **as an architect**, and **as someone building AI/agent systems**.
---
# 🚀 What Redis *Really* Is (in your head)
Forget the marketing. Redis is:
**A super-fast, in-memory, single-threaded key–value database with data structures.**
It’s basically a **data-structure server** with persistence options.
Think of it like:
- A hash map on steroids
- That lives on a server
- That you can query over a network
- With atomic operations
- And microsecond-level performance
If you understand this, everything else is details.
---
# 🌱 Step 1 — The Redis Mental Model
Redis stores data like:
```
<key> -> <data structure>
```
And the structures can be:
- `STRING` — raw bytes (yes, includes JSON)
- `LIST` — linked list (LPUSH/RPUSH)
- `SET` — unique values
- `HASH` — dictionary
- `ZSET` — sorted set
- `STREAM` — append-only log (like Kafka-lite)
- `BITMAP` — bit-level operations
- `GEO` — geospatial
- `HLL` — hyperloglog for cardinality
Redis is **single-threaded**: commands execute one at a time, which means:
- No race conditions within one Redis instance
- Atomicity = free
- Pipes and transactions become predictable
---
# 🧪 Step 2 — Actually Using Redis (Basics)
Start Redis:
```
redis-server
```
Connect:
```
redis-cli
```
### 📌 Strings
```
SET name "Daniel"
GET name
```
### 📌 Hashes (like Python dict)
```
HSET user:1 name "Daniel" age 32
HGET user:1 name
HGETALL user:1
```
### 📌 Lists (queues, stacks)
```
LPUSH jobs "task1"
RPUSH jobs "task2"
LPOP jobs
RPOP jobs
```
### 📌 Sets (unique values, no duplicates)
```
SADD tags "ai"
SADD tags "llm"
SMEMBERS tags
```
### 📌 Sorted Sets (leaderboards, priorities)
```
ZADD leaderboard 130 "daniel"
ZADD leaderboard 200 "alice"
ZRANGE leaderboard 0 -1 WITHSCORES
```
---
# 🧱 Step 3 — Persistence (What People Get Wrong)
Redis is memory-first. Disk is optional.
You choose how it persists:
### **1. RDB snapshots**
Saves the DB every X minutes. Fast to load, cheap to store.
### **2. AOF (Append Only File)**
Every write is appended. Safe but larger.
### **3. Hybrid**
Most common: RDB + AOF.
---
# 🚦 Step 4 — Common Real Use Cases
Here’s the list you will see **in real systems**:
## 1. **Caching Layer**
```
SETEX page:/index 60 "<html>...</html>"
```
## 2. **Rate Limiting**
```
INCR user:123:requests
EXPIRE user:123:requests 60
```
## 3. **Job Queues**
Use `LIST` or Streams:
```
XADD tasks * type=embedding text="..."
```
## 4. **Session Storage**
Store Django/Flask session tokens.
## 5. **Leaderboards**
ZSET was *made* for this.
## 6. **Pub/Sub**
Lightweight messaging.
```
PUBLISH events "hello"
SUBSCRIBE events
```
## 7. **Feature Flags**
```
HSET flags enable_new_ui 1
```
---
# 🧠 Step 5 — Redis for AI / Agents / RAG (your domain)
You can use Redis for:
### **1. Agent memory**
Hash structure per agent:
```
HSET agent:4:last_action "search_query: llama"
```
### **2. RAG chunk caching**
Avoid recomputing embeddings:
```
SET embedding:<hash> <vector>
```
Redis 7+ supports vector similarity via RedisSearch:
```
FT.CREATE idx ON JSON ... VECTOR ...
```
### **3. GraphRAG or entity linking caches**
Store nodes as hashes:
```
HSET graph:node:42 type "person" name "Chris"
```
### **4. Conversation context windows**
Store rolling windows using LIST:
```
RPUSH convo:user42 "message blob"
LTRIM convo:user42 -20 -1
```
### **5. Distributed agent coordination**
Use Pub/Sub or Streams to broadcast tasks.
---
# 🛠️ Step 6 — How Redis Scales
Redis Cluster uses **sharding**:
keys are partitioned across nodes using hash slots.
You *must* design keys with hashing in mind:
```
user:{42}:profile
user:{42}:settings
```
Everything inside `{}` hashes to the same slot.
---
# 📐 Step 7 — How to Think in Redis
Redis is not SQL. The trick is to use the right structure.
Here’s the quick cheat sheet:
| Need | Redis Structure |
|------|-----------------|
| Key-value cache | STRING |
| JSON objects | STRING or JSON module |
| User profiles | HASH |
| Tags | SET |
| Queues | LIST |
| Streams of events | STREAM |
| Leaderboards | ZSET |
| Pub/Sub | PUBSUB |
| Vector search | RedisSearch |
---
# 🎓 If You Want to Go Deeper
I can teach you:
- How Redis implements its internal architecture
- How to build microservices around Redis
- The “Redis patterns” that matter (queues, locks, dedupe, rate limiters)
- Redis Streams vs Kafka
- Redis as an event backbone for agents
- Redis performance tuning
- Designing key schemas
- Using Redis in Python / Django / FastAPI
- Redis graph queries (module)
- Redis vector search best practices
---
# Ready for the next step?
Tell me **which direction you want to go next**:
1. **Redis for backend dev**
2. **Redis for algorithms & data structures**
3. **Redis for AI/agents/RAG**
4. **Redis as a distributed system** (cluster, scaling, failover)
5. **Redis applied to your projects** (GraphRAG, personas, agent orchestration)
Pick a number — I’ll take you deeper.
gpt-5-1