π Redis Fundamentals
Understand what Redis is, when to use it, and get hands-on with the basics.

What is Redis?
Redis (Remote Dictionary Server) is an open-source, in-memory data structure store that can be used as a database, cache, message broker, and streaming engine.
In-Memory Data Store vs Traditional DB
| Aspect | Redis (In-Memory) | Traditional DB (Disk-Based) |
|---|---|---|
| Speed | Sub-millisecond latency | Milliseconds to seconds |
| Storage | RAM (limited by memory) | Disk (virtually unlimited) |
| Persistence | Optional (RDB/AOF) | Built-in & durable |
| Data Model | Key-Value + Rich Data Types | Tables, Documents, etc. |
Redis Use Cases
Store frequently accessed data to reduce DB load and improve response times.
Use Lists or Streams for async task processing and job queues.
Distributed session management for web applications.
Real-time rankings using Sorted Sets with O(log N) operations.
Redis Architecture
Single-Threaded Model (Myth vs Reality)
β οΈ Common Misconception
"Redis is single-threaded, so it can't be fast" β WRONG!
The Reality:
- Redis uses a single thread for command execution (atomic operations)
- Since Redis 6.0, I/O is handled by multiple threads
- Background threads handle persistence (RDB snapshots, AOF rewriting)
- No locks needed = No lock contention = Extremely fast
Event Loop & I/O Multiplexing
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β Redis Server β
βββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββ ββββββββββββββββββββββββ β
β β Clients βββββΆβ I/O Multiplexer β β
β β (1000s) β β (epoll/kqueue) β β
β βββββββββββββββ ββββββββββββ¬ββββββββββββ β
β β β
β ββββββββββββββΌβββββββββββββ β
β β Single-Threaded β β
β β Command Processor β β
β β (Main Event Loop) β β
β ββββββββββββββ¬βββββββββββββ β
β β β
β ββββββββββββββΌβββββββββββββ β
β β In-Memory Data Store β β
β βββββββββββββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββRedis Installation & Setup
Option 1: Docker-Based Redis (Recommended)
# Pull and run Redis containerdocker run -d--name redis-server -p6379:6379 redis:latest
# With persistencedocker run -d--name redis-server \-p6379:6379 \-v redis-data:/data \
redis:latest redis-server --appendonlyyes# Connect to Redis CLIdockerexec-it redis-server redis-cliOption 2: Local Installation
sudoapt update
sudoaptinstall redis-server
sudo systemctl start redisbrew install redis
brew services start redisVerify Installation
$ redis-cli ping
PONG
$ redis-cli info server |head-5# Server
redis_version:7.0.11
redis_git_sha1:00000000
redis_git_dirty:0Redis CLI Basics
Essential Commands
# SET & GET - Basic key-value operations127.0.0.1:6379> SET user:1:name "John Doe"
OK
127.0.0.1:6379> GET user:1:name
"John Doe"# EXISTS - Check if key exists127.0.0.1:6379> EXISTS user:1:name
(integer)1127.0.0.1:6379> EXISTS user:999:name
(integer)0# DEL - Delete keys127.0.0.1:6379> DEL user:1:name
(integer)1# KEYS - Find keys by pattern (β οΈ Avoid in production!)127.0.0.1:6379> KEYS user:*
1)"user:1:email"2)"user:2:name"TTL & EXPIRE - Key Expiration
# SET with expiration (EX = seconds, PX = milliseconds)127.0.0.1:6379> SET session:abc123 "user-data" EX 3600
OK
# Set expiration on existing key127.0.0.1:6379> EXPIRE cache:products 300(integer)1# Check remaining TTL127.0.0.1:6379> TTL session:abc123
(integer)3595# Remove expiration (make key persistent)127.0.0.1:6379> PERSIST session:abc123
(integer)1# SETEX shorthand (SET + EXPIRE in one command)127.0.0.1:6379> SETEX cache:homepage 60"<html>...</html>"
OKπ‘ Pro Tip
Use NX (only if Not eXists) and XX (only if eXists) for conditional sets:SET lock:order123 "locked" NX EX 30 β Sets only if key doesn't exist (distributed lock!)