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).
If using Docker Compose:
docker compose exec redis redis-cliYou’ll see a prompt like:
127.0.0.1:6379>SET greeting "Hello Redis"
GET greeting
DEL greeting
EXISTS greetingSET stores data.GET retrieves.DEL deletes.EXISTS checks existence.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.
SET tempkey "expire soon"
EXPIRE tempkey 5
TTL tempkey
# wait 5s
GET tempkeyLPUSH tasks "task1"
LPUSH tasks "task2"
LRANGE tasks 0 -1
RPOP tasks
LRANGE tasks 0 -1LPUSH + RPOP = QueueLPUSH + LPOP = StackSADD users "sree"
SADD users "abhi"
SADD users "sree"
SMEMBERS users
SCARD users
SISMEMBER users "sree"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 WITHSCORESHSET user:100 name "Sree" plan "Gold" age 68
HGETALL user:100
HGET user:100 plan
HINCRBY user:100 age 1SET stockA 10
SET stockB 5
WATCH stockA stockB
GET stockA
MULTI
DECRBY stockA 3
INCRBY stockB 3
EXECIf another client changes stockA or stockB before EXEC, the transaction aborts safely.
MULTI
SET a 1
SET b 2
SET c 3
EXEC
MGET a b cExecutes multiple commands in one round trip for efficiency.
Open Terminal 1:
docker compose exec redis redis-cli
SUBSCRIBE alertsOpen Terminal 2:
docker compose exec redis redis-cli
PUBLISH alerts "Redis is awesome!"Messages appear instantly in Terminal 1 — no polling needed.
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.
SCAN 0 MATCH * COUNT 10
TYPE stockA
KEYS *SCAN is safer for production than KEYS * (non-blocking).INFO memory
INFO stats
INFO commandstats
MONITORMONITOR shows every command live (Ctrl+C to stop).Try two terminals editing same keys with WATCH to see optimistic locking fail and retry.
FLUSHALLClears entire Redis DB — use with caution!
Run this inside redis-cli:
SETEX rl:fixed:demo 10 0
INCR rl:fixed:demo
TTL rl:fixed:demo
GET rl:fixed:demoThis simulates the Fixed Window counter (INCR + EXPIRE).
redis-cli MONITORNow run your FastAPI requests — watch rate limiter keys (rl:fixed:..., rl:slide:...) appear and expire live.
| 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.