Redis Command-Line—Comprehensive Explanation


1. Basic Key-Value Commands


2. Atomic Counters

Atomicity guarantee: Redis ensures operations like INCR/DECR are indivisible; even under massive concurrency, the counter values are perfectly reliable (crucial for rate limiting, page views, etc).[9]


3. Expiration / Time to Live (TTL)


4. Lists (Queues / Stacks)

Use LPUSH + RPOP for a queue (FIFO: first in, first out). Use LPUSH + LPOP for a stack (LIFO: last in, first out).[5][9]


5. Sets (Unique Members)

Use cases: unique tags, IDs, feature flags, etc.[9]


6. Sorted Sets (ZSET) — Sliding Window Demo

What’s a score? Each member in a sorted set is paired with a number called its score. Redis automatically organizes the set so the member with the smallest score comes first and the largest last. The set is always sorted by score—not the value itself. Members are unique; scores can repeat, but if you ZADD a member with a new score, its score is updated.

Why use scores? The score represents rank, priority, or timestamp.

Use case: leaderboards, logs sorted by time, rate-limiting with timestamps.[9]


7. Hashes (Structured Objects)

Use case: storing user/session profiles, structured data with named properties.[5][9]


8. Transactions (WATCH / MULTI / EXEC)

Practical example: Deduct stock from one product, add to another; if someone else modifies stock in the middle, your transfer fails safely.


9. Pipelining (Batch Commands)


10. Pub/Sub (Publish/Subscribe Messaging)


11. Lua Scripting (Atomic Server-Side Logic)

E.g.: Atomically debit an account if it has enough funds, else return -1—prevents race conditions.


12. Scan / Key Exploration


13. Memory & Performance Insights


14. Manual Transaction Retry

Try editing the same keys from two terminals using WATCH. If one changes before EXEC, the transaction aborts—forcing you to retry and thus implementing optimistic locking (ensures correctness without always locking the entire key set).


15. Flush / Reset


16. Rate Limiting Demo (Fixed Window)

You now have a sliding window rate limiter: keys "decay" as windows expire.


Summary Table

Feature Command(s) Use Case/Goal
Atomic Counter INCR, DECR Page views, counters, rate limiting
TTL EXPIRE, TTL Auto-expiring cache/session
Hash HSET, HGET, HGETALL Structured objects—users, sessions
Sorted Set ZADD, ZRANGE Ordered logs, leaderboards, sliding windows
Lua EVAL Custom atomic workflows (logic)
Transactions WATCH/MULTI/EXEC Safe multi-key updates
Pub/Sub PUBLISH/SUBSCRIBE Real-time messaging, notifications
Introspection INFO, MONITOR Debugging, monitoring
Flush FLUSHALL Reset for test/dev

*