Perfect — let’s make this practical and complete. Here’s a Redis command-line crash course that will let you test every major feature — from atomic counters to Lua scripting — right inside your terminal. You can run these inside the API container or Redis container itself (docker compose exec redis redis-cli).


🧠 Redis Command-Line Deep Test

⚙️ Step 1: Enter Redis CLI

If using Docker Compose:

docker compose exec redis redis-cli

You’ll see a prompt like:

127.0.0.1:6379>

🔹 1. Basic Key-Value Commands

SET greeting "Hello Redis"
GET greeting
DEL greeting
EXISTS greeting

🔹 2. Atomic Counters

INCR page_views
INCRBY page_views 5
GET page_views
DECR page_views

✅ Atomic Guarantee: Even under heavy concurrency, no two clients will race. You can test by running the same INCR in multiple terminals.


🔹 3. Expiry / TTL (Time to Live)

SET tempkey "expire soon"
EXPIRE tempkey 5
TTL tempkey
# wait 5s
GET tempkey

🔹 4. Lists (Queues / Stacks)

LPUSH tasks "task1"
LPUSH tasks "task2"
LRANGE tasks 0 -1
RPOP tasks
LRANGE tasks 0 -1

🔹 5. Sets (Unique Members)

SADD users "sree"
SADD users "abhi"
SADD users "sree"
SMEMBERS users
SCARD users
SISMEMBER users "sree"

🔹 6. Sorted Sets (ZSET) — Sliding Window Demo

ZADD visits 1001 "req1"
ZADD visits 1002 "req2"
ZADD visits 1003 "req3"
ZRANGE visits 0 -1 WITHSCORES
ZREMRANGEBYSCORE visits 0 1002
ZRANGE visits 0 -1 WITHSCORES

🔹 7. Hashes (Structured Objects)

HSET user:100 name "Sree" plan "Gold" age 68
HGETALL user:100
HGET user:100 plan
HINCRBY user:100 age 1

🔹 8. Transactions (WATCH / MULTI / EXEC)

Example: Transfer between stockA and stockB

SET stockA 10
SET stockB 5
WATCH stockA stockB
GET stockA
MULTI
DECRBY stockA 3
INCRBY stockB 3
EXEC

If another client changes stockA or stockB before EXEC, the transaction aborts safely.


🔹 9. Pipelining (Batch Commands)

MULTI
SET a 1
SET b 2
SET c 3
EXEC
MGET a b c

Executes multiple commands in one round trip for efficiency.


🔹 10. Pub/Sub (Real-time Messaging)

Open Terminal 1:

docker compose exec redis redis-cli
SUBSCRIBE alerts

Open Terminal 2:

docker compose exec redis redis-cli
PUBLISH alerts "Redis is awesome!"

Messages appear instantly in Terminal 1 — no polling needed.


🔹 11. Lua Scripting (Atomic Server-Side Logic)

Example: Atomic wallet debit

SET balance 100
EVAL "local bal=tonumber(redis.call('GET',KEYS[1]));if bal>=tonumber(ARGV[1]) then redis.call('DECRBY',KEYS[1],ARGV[1]);return bal-ARGV[1];else return -1;end" 1 balance 30
GET balance

✅ Entire logic runs atomically on Redis server.


🔹 12. Scan / Keys Exploration

SCAN 0 MATCH * COUNT 10
TYPE stockA
KEYS *

🔹 13. Memory & Performance Insights

INFO memory
INFO stats
INFO commandstats
MONITOR

🔹 14. Transactions with Retry (manually)

Try two terminals editing same keys with WATCH to see optimistic locking fail and retry.


🔹 15. Flush / Reset (for testing)

FLUSHALL

Clears entire Redis DB — use with caution!


🧪 Bonus: Simulate a Rate Limit Key Behavior

Run this inside redis-cli:

SETEX rl:fixed:demo 10 0
INCR rl:fixed:demo
TTL rl:fixed:demo
GET rl:fixed:demo

This simulates the Fixed Window counter (INCR + EXPIRE).


💡 Pro Tip: Watch live changes

redis-cli MONITOR

Now run your FastAPI requests — watch rate limiter keys (rl:fixed:..., rl:slide:...) appear and expire live.


✅ Summary

Feature Command Use
Atomic Counter INCR, DECR Simple atomic increments
TTL EXPIRE, TTL Auto key expiry
Hash HSET, HGET Structured user/session data
Sorted Set ZADD, ZCARD Time-based window counting
Lua EVAL Atomic server-side logic
Transactions WATCH/MULTI/EXEC Multi-key atomic ops
Pub/Sub PUBLISH/SUBSCRIBE Real-time updates
Introspection INFO, MONITOR Performance & live ops

Would you like me to make this into a PDF or PPTX cheat sheet (“Redis CLI Master Commands – Live Test Guide”) with categories, expected outputs, and use cases? It would make a great quick-reference for your workshops or interns.