π° SECTION 0 β Redis Fundamentals (Before Any Commands)¶


π§ What Redis really is (important)¶
Redis is:
- A single-threaded event loop
- Holding data entirely in RAM
- Persisting to disk optionally
- Optimized for very fast reads/writes
Redis is fast because it avoids locks and disk I/O
π§© Internal mental model¶
KEY (string)
β
Value (string | hash | list | set | zset | stream)
β
Stored in RAM
β
Optional persistence to disk
π Start & Connect (once)¶
Test:
Redis replies:
Meaning:
Redis event loop is alive and accepting commands
1οΈβ£ STRING β The Atomic Unit of Redis¶
π§ Why strings exist¶
Strings are:
- The fastest Redis type
- Atomic (safe for counters)
- Used in 90% of Redis use cases
Real-world uses¶
- Cache values
- Session tokens
- Feature flags
- Counters
- Locks
π§© How Redis stores a string¶
Internally:
- Key is always a string
- Value is a byte array
- TTL stored separately
π§ͺ Step-by-step¶
Step 1 β Store value¶
Redis does:
- Allocates memory
- Stores key hash
- Stores value bytes
Step 2 β Read value¶
Redis:
- Hashes key
- Direct memory lookup
- Returns value
Step 3 β Add TTL (CRITICAL)¶
π What happens internally:
- Redis adds expiry metadata
- Key auto-deletes after TTL
Why TTL matters:
Redis will crash if you forget TTLs in cache-heavy systems
Step 4 β Atomic counter¶
Why INCR is special:
- No race condition
- Single-threaded atomic op
Used for:
- Rate limiting
- Analytics
- Quotas
π« DONβT
- Store large JSON blobs
- Use strings without TTL for cache
2οΈβ£ HASH β Structured Data Without JSON¶
π§ Why hashes exist¶
Hashes solve:
- Storing structured data
- Updating single fields
- Avoiding JSON parsing
Real-world uses¶
- User profiles
- Product metadata
- Configuration
π§© Internal model¶
Redis stores fields compactly β memory efficient.
π§ͺ Step-by-step¶
Step 1 β Create hash¶
Redis:
- Creates hash table
- Stores fields separately
Step 2 β Read data¶
Why hashes are fast:
- Field lookup is O(1)
Step 3 β Update one field¶
Only role changes β no full rewrite.
Step 4 β Expiry on hash¶
β TTL applies to entire hash, not fields.
π« DONβT
- Store nested JSON in hashes
- Use hashes without TTL in cache
3οΈβ£ LIST β Ordered Queue / Stack¶
π§ Why lists exist¶
Lists solve:
- Queues
- Background jobs
- Message buffering
Real-world uses¶
- Job queues
- Email sending
- Event buffering
π§© Internal model¶
Linked list internally β fast push/pop.
π§ͺ Step-by-step¶
Step 1 β Push jobs¶
Step 2 β View list¶
Step 3 β Pop job (worker)¶
FIFO achieved by:
Step 4 β Blocking pop (IMPORTANT)¶
Meaning:
- Worker waits
- No CPU waste
- Production-safe
π« DONβT
- Use lists for durable queues
- Use lists without monitoring length
4οΈβ£ SET β Fast Membership & Uniqueness¶
π§ Why sets exist¶
Sets solve:
- Uniqueness
- Membership checks
- Grouping
Real-world uses¶
- Online users
- Feature flags
- Permissions
π§© Internal model¶
Unordered hash set:
π§ͺ Step-by-step¶
Membership check is O(1) β extremely fast.
Set math (powerful)¶
Used for:
- Access control
- A/B testing
π« DONβT
- Expect ordering
- Store huge sets without memory limits
5οΈβ£ SORTED SET β Ranking & Time-Based Logic¶
π§ Why sorted sets exist¶
Sorted sets solve:
- Ranking
- Priority
- Time ordering
Real-world uses¶
- Leaderboards
- Rate limiting
- Scheduling
π§© Internal model¶
Score determines order.
π§ͺ Step-by-step¶
Update score¶
Used for:
- Sliding window rate limits
- Priority queues
π« DONβT
- Use lists for ranking
- Forget score precision
6οΈβ£ TTL & Expiry β Redis Safety Valve¶
π§ Why expiry is critical¶
Redis memory is finite. TTL prevents:
- OOM crashes
- Memory leaks
π§ͺ Commands¶
Redis deletes keys:
- Lazily on access
- Actively via background cleanup
7οΈβ£ Pub/Sub β Fire-and-Forget Messaging¶
π§ Why Pub/Sub exists¶
- Real-time fan-out
- Low latency
- No persistence
π§ͺ Demo¶
Terminal 1:
Terminal 2:
β Message lost if no subscriber.
8οΈβ£ Streams β Durable Messaging (Advanced)¶
π§ Why streams exist¶
Streams solve:
- Reliable messaging
- Event sourcing
- Replayability
π§ͺ Demo¶
Consumer group:
Used instead of:
- Pub/Sub
- Lists (for jobs)
9οΈβ£ Persistence β Data Survival¶
π§ Redis is RAM-first¶
Persistence is backup, not primary storage.
π§ͺ Check config¶
Enable AOF:
π 10οΈβ£ Security Basics¶
Never expose Redis publicly.
π FINAL TRUTH ABOUT REDIS¶
Redis is not a database replacement Redis is a performance accelerator