Redis
Redis explores an in-memory data structure store used as a database, cache, and message broker, prized for its blazing-fast performance since it keeps data primarily in RAM rather than on disk. It covers core concepts like key-value storage, data structures such as strings, hashes, lists, and sets, along with features like expiration policies and pub/sub messaging that support real-time applications. This technology blends speed with versatility, showing how developers rely on Redis for use cases like session storage, caching, leaderboards, and real-time analytics where microsecond response times matter. At its core, Redis asks how we can make data access nearly instantaneous by rethinking where and how information is stored. Where speed becomes the foundation of real-time systems.

Introduction To Redis
- Chapter 1: What is Redis?
- Chapter 2: Redis Core Concepts
- Chapter 3: How to Install Redis on Windows, macOS, and Linux
- Chapter 4: Redis CLI and Server Basics
- Chapter 5: Basic Commands and Operations
- Chapter 6: Redis Persistence
- Chapter 7: Intermediate Redis Concepts
- Chapter 8: Advanced Data Types
- Chapter 9: Redis Memory Management
- Chapter 10: Cache Design Patterns
- Chapter 11: Redis Security
- Chapter 12: Replication and High Availability
- Chapter 13: Redis Cluster
- Chapter 14: Redis Modules
- Chapter 15: Production and Deployment
- Chapter 16: Internal Working of Redis
- Chapter 17: Your First Program – Hello Redis
- Chapter 18: Project Folder Structure
- Chapter 19: Conclusion and Final Skill Stack
Chapter 1: What is Redis?
1.1 Definition and Simple Explanation
Redis stands for REmote DIctionary Server. It is a database that stores information in the computer’s memory (RAM) instead of on a hard drive. This makes it incredibly fast – often responding in microseconds (millionths of a second).
A simple analogy for a child:
Imagine you have two places to store your toys:
- Your backpack (RAM) – You can grab toys instantly, but there’s limited space.
- Your closet (Hard Drive) – Lots of space, but you have to walk there and open the door.
Redis is like your backpack. It keeps the toys you need right now close to you. When you need a toy, you get it immediately. When you need to store a toy forever, you put it in the closet (hard drive). Redis is the backpack of databases.
Formal definition:
Redis is an in-memory data structure store used as a database, cache, and message broker. Unlike traditional relational databases, Redis stores data in RAM, making it extremely fast, often responding in microseconds.
1.2 History and Evolution of Redis
| Year | Event |
|---|---|
| 2009 | Salvatore Sanfilippo (antirez) started Redis to scale his Italian startup |
| 2010 | Redis became open source and gained popularity |
| 2013 | Redis 2.6 added Lua scripting |
| 2015 | Redis 3.0 added Redis Cluster (native sharding) |
| 2016 | Redis 4.0 added modules system |
| 2018 | Redis 5.0 added Streams data type |
| 2020 | Redis 6.0 added ACLs (Access Control Lists) and multi-threaded I/O |
| 2021 | Redis 7.0 added Redis Functions and better memory efficiency |
| 2023–Present | Redis continues to evolve with JSON, Search, and TimeSeries modules |
1.3 Why Redis Matters
Benefits of Redis:
| Benefit | Explanation |
|---|---|
| Extreme speed | Stores data in RAM – microsecond response times |
| Rich data types | Strings, lists, sets, hashes, streams, geospatial, and more |
| Persistence options | Can save to disk without losing speed |
| Atomic operations | All commands are single-threaded and safe |
| Replication | Master-slave for read scaling |
| High availability | Sentinel for automatic failover |
| Cluster mode | Horizontal scaling across many servers |
| Pub/Sub | Real-time messaging |
When to use Redis:
- When you need data in under 1 millisecond
- For caching frequently accessed data
- For real-time analytics (leaderboards, counters)
- For session storage
- For message queues
- For rate limiting
When NOT to use Redis:
- When data is larger than available RAM
- When you need complex SQL queries (joins, aggregations)
- When you need ACID transactions across many keys
1.4 Where Redis is Used (Use Cases)
| Industry/Use Case | Example |
|---|---|
| Caching | Store database query results, API responses |
| Session storage | User login sessions in web apps |
| Real-time analytics | Counting page views, active users |
| Leaderboards | Gaming scores, top products |
| Rate limiting | API request limits per user |
| Message queues | Background job processing |
| Pub/Sub messaging | Real-time chat, notifications |
| Geospatial | Find nearby restaurants, drivers |
| Machine learning | Feature stores, model caching |
Companies using Redis: Twitter, GitHub, Snapchat, Stack Overflow, Craigslist, Walmart, Uber, Airbnb, Discord.
1.5 Prerequisites for Learning Redis
Before starting Redis, you should know:
| Prerequisite | Level Needed |
|---|---|
| Basic computer skills | Knowing how to open terminal |
| Command line basics | cd, ls, mkdir |
| Basic programming | Any language (Node.js, Python, Java) |
| Understanding of key-value | Like a dictionary or object |
No prior database experience needed! Redis is very beginner-friendly.
Chapter 2: Redis Core Concepts
2.1 Key-Value Data Model
Redis stores data as key-value pairs. Think of it like a giant dictionary or a phonebook.
Example:
Key → Value
"name" → "Alice"
"age" → 25
"colors" → ["red", "blue", "green"]
- Key – A unique identifier (always a string)
- Value – Can be many types (string, list, set, hash, etc.)
Key naming best practices:
- Use colons for hierarchy:
user:1000:name,product:200:price - Keep keys short but descriptive
- Avoid spaces and special characters (use underscore or colon)
2.2 Basic Data Types
| Data Type | What it stores | Example | Use case |
|---|---|---|---|
| String | Text or number | "Hello", 42 | Caching HTML, counters |
| List | Ordered list | ["a", "b", "c"] | Queues, timelines |
| Set | Unique, unordered | {"a", "b", "c"} | Tags, unique visitors |
| Sorted Set | Unique, scored | {"a":10, "b":20} | Leaderboards |
| Hash | Field-value pairs | {name:"Alice", age:25} | Objects, user profiles |
2.3 Key Naming Conventions
Good key names:
user:1000:profile
product:200:price
session:abc123
cache:api:/users
Bad key names:
user1000profile (hard to read)
user/1000/profile (slashes are fine but less common)
a (not descriptive)
2.4 Redis Architecture Overview
Redis uses a single-threaded event-driven architecture:
Client → TCP Connection → Event Loop → Command Processor → Data Store → Response
Why single-threaded?
- No locking overhead
- All operations are atomic
- Simpler to reason about
- Still very fast because it’s in-memory
2.5 Memory Basics and In-Memory Storage
How much memory can Redis use?
- Depends on your RAM
- Small instances: 1-4 GB
- Large instances: 100+ GB
- Redis Cluster: Many servers, each with its own RAM
What happens when memory is full?
Redis uses eviction policies (we’ll cover these later) to remove old data.
Chapter 3: How to Install Redis on Windows, macOS, and Linux
This section is written so even a child can follow along. Each step is numbered and explained in plain English.
3.1 Windows Setup (Step by Step)
Method 1: Using WSL (Windows Subsystem for Linux) – Recommended
Step 1: Install WSL (Windows Subsystem for Linux)
- Open PowerShell as Administrator (Right-click Start → Windows PowerShell (Admin))
- Type this command and press Enter:
wsl --install
- Restart your computer when asked
Step 2: Open Ubuntu (installed automatically with WSL)
- Click Start, type “Ubuntu”, and open it
- Wait for installation to complete (create a username and password)
Step 3: Update package list
sudo apt update
- Enter your password when asked
Step 4: Install Redis
sudo apt install redis-server -y
Step 5: Start Redis server
redis-server
- You should see a Redis logo and “Ready to accept connections”
- Keep this window open!
Step 6: Open a NEW Ubuntu window (keep first one running)
- Click Start, open another Ubuntu terminal
Step 7: Connect to Redis
redis-cli
- You will see a prompt like
127.0.0.1:6379>
Step 8: Test Redis
ping
- Expected output:
PONG
Congratulations! You have Redis running on Windows via WSL.
Method 2: Using Redis for Windows (Memurai – Alternative)
Step 1: Go to https://www.memurai.com/
Step 2: Click “Download Memurai”
Step 3: Run the installer (.exe file)
Step 4: Follow the installation wizard (click “Next” several times)
Step 5: Memurai starts automatically as a Windows service
Step 6: Open Command Prompt
redis-cli
ping
- Expected output:
PONG
Troubleshooting Windows
| Problem | Solution |
|---|---|
wsl is not recognized | You need Windows 10 version 2004 or newer |
redis-cli: command not found | Wait for installation to complete, then try again |
Connection refused | Redis server not running. Run redis-server first |
3.2 macOS Setup (Step by Step)
Method 1: Using Homebrew (Easiest)
Step 1: Install Homebrew (if you don’t have it)
- Open Terminal (Finder → Applications → Utilities → Terminal)
- Paste this command and press Enter:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Follow the instructions (may take 5 minutes)
Step 2: Install Redis
brew install redis
Step 3: Start Redis as a background service
brew services start redis
- You should see “Successfully started”
Step 4: Verify Redis is running
redis-cli ping
- Expected output:
PONG
Step 5: Open Redis CLI
redis-cli
- You will see a prompt like
127.0.0.1:6379>
Congratulations! You have Redis running on macOS
Method 2: Manual Installation (Without Homebrew)
Step 1: Download Redis from https://redis.io/download/
Step 2: Extract the downloaded file
Step 3: Open Terminal and navigate to Redis folder
cd ~/Downloads/redis-7.x.x
Step 4: Compile Redis
make
Step 5: Install
sudo make install
Step 6: Start Redis
redis-server
Step 7: Open another terminal and run
redis-cli
Troubleshooting macOS
| Problem | Solution |
|---|---|
brew: command not found | Install Homebrew first (see Step 1) |
redis-cli: command not found | Run brew reinstall redis |
Address already in use | Another Redis is running. brew services stop redis then restart |
3.3 Linux (Ubuntu) Setup (Step by Step)
Step-by-Step for Ubuntu 20.04 / 22.04
Step 1: Open Terminal
- Press
Ctrl + Alt + Ton your keyboard
Step 2: Update your package list
sudo apt update
- Enter your password when asked
Step 3: Install Redis
sudo apt install redis-server -y
Step 4: Check that Redis installed correctly
redis-server --version
Step 5: Start Redis service
sudo systemctl start redis-server
Step 6: Enable Redis to start automatically when your computer boots
sudo systemctl enable redis-server
Step 7: Check that Redis is running
sudo systemctl status redis-server
- You should see “active (running)” in green text
Step 8: Test Redis
redis-cli ping
- Expected output:
PONG
Step 9: Open Redis CLI
redis-cli
- You will see a prompt like
127.0.0.1:6379>
Congratulations! You have Redis running on Ubuntu Linux.
Alternative: Install Latest Redis from Official Repository
Step 1: Add Redis official repository
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
Step 2: Update and install
sudo apt update
sudo apt install redis -y
Step 3: Start Redis
sudo systemctl start redis-server
sudo systemctl enable redis-server
Step 4: Connect
redis-cli
Troubleshooting Linux
| Problem | Solution |
|---|---|
Unable to locate package redis-server | Run sudo apt update first |
Failed to start redis-server | Check logs: sudo journalctl -u redis-server |
Connection refused | Redis not running: sudo systemctl start redis-server |
3.4 Verifying Your Installation
Run these tests to make sure everything works:
Test 1: Check Redis version
redis-server --version
Expected output: Shows version number (e.g., Redis 7.x.x)
Test 2: Check Redis CLI version
redis-cli --version
Test 3: Ping the server
redis-cli ping
Expected output: PONG
Test 4: Set and get a value
redis-cli
SET greeting "Hello Redis"
GET greeting
Expected output: "Hello Redis"
3.5 Environment Setup
What you need for professional development:
1. Code Editor: VS Code (free)
- Download from https://code.visualstudio.com/
- Install Redis extension (search for “Redis” in extensions)
2. Node.js (if using JavaScript)
- Download from https://nodejs.org/
- Verify:
node --version
3. Python (if using Python)
- Download from https://python.org/
- Verify:
python --version
4. Redis Insight (GUI – optional but helpful)
- Download from https://redis.com/redis-enterprise/redis-insight/
- Connect to your local Redis instance
5. Docker (optional)
- Download from https://docker.com/
- We’ll cover Dockerizing Redis later
Chapter 4: Redis CLI and Server Basics
4.1 Redis CLI Basics (redis-cli)
What is redis-cli?
It is the command-line interface for Redis. You type commands, and Redis responds.
Basic navigation commands:
# Connect to local Redis
redis-cli
# Connect to remote Redis
redis-cli -h 192.168.1.100 -p 6379
# Connect with password
redis-cli -a mypassword
# Exit CLI
exit
# or press Ctrl+C
4.2 Redis Server Basics (redis-server)
What is redis-server?
It is the Redis database server process.
Starting Redis server:
# Start with default settings
redis-server
# Start with specific config file
redis-server /path/to/redis.conf
# Start as daemon (background)
redis-server --daemonize yes
4.3 Redis Configuration File (redis.conf)
The redis.conf file controls Redis behavior.
Important configuration settings:
| Setting | Purpose | Default |
|---|---|---|
bind | Which IP addresses to listen on | 127.0.0.1 |
port | Which port to use | 6379 |
requirepass | Password for authentication | (none) |
maxmemory | Maximum memory to use | (none) |
maxmemory-policy | What to do when memory is full | noeviction |
save | RDB snapshot intervals | save 900 1 |
appendonly | Enable AOF persistence | no |
Where to find redis.conf:
| OS | Location |
|---|---|
| Linux (apt) | /etc/redis/redis.conf |
| macOS (brew) | /usr/local/etc/redis.conf |
| Windows (WSL) | /etc/redis/redis.conf |
Editing the config file:
# Open with nano editor
sudo nano /etc/redis/redis.conf
# After editing, restart Redis
sudo systemctl restart redis-server
4.4 Redis Insight Tool
Redis Insight is a free GUI tool for Redis.
Features:
- Browse keys visually
- View and edit data
- Run commands
- Monitor performance
- Analyze memory usage
Installation:
- Go to https://redis.com/redis-enterprise/redis-insight/
- Download for your OS
- Install and run
- Connect to
localhost:6379
Chapter 5: Basic Commands and Operations
5.1 Basic Commands (SET, GET, DEL, EXISTS)
# SET – Store a value
SET name "Alice"
# GET – Retrieve a value
GET name
# Output: "Alice"
# DEL – Delete a key
DEL name
# EXISTS – Check if a key exists
EXISTS name
# Output: 0 (false) or 1 (true)
# TYPE – Check data type
TYPE name
# KEYS – Find keys matching pattern (avoid in production!)
KEYS user:*
5.2 Expiration and TTL (Time To Live)
TTL (Time To Live) tells Redis to automatically delete a key after a certain time.
# Set key with 10 second expiration
SET session "abc123" EX 10
# Set expiration on existing key
EXPIRE session 30
# Check remaining time (seconds)
TTL session
# Output: 25 (means 25 seconds left)
# Check if key has expiration (returns -1 if no expiration)
TTL permanent_key
# Output: -1
# Remove expiration (make permanent)
PERSIST session
# Set expiration in milliseconds
SET temp "value" PX 5000 # 5 seconds (5000 ms)
Use case: Session storage, temporary cache, rate limiting.
5.3 Working with Strings
Strings are the simplest data type – text or numbers.
# Basic string operations
SET user:1:name "Alice"
GET user:1:name
# Increment numbers (atomic)
SET counter 0
INCR counter # 1
INCR counter # 2
INCRBY counter 5 # 7
DECR counter # 6
DECRBY counter 2 # 4
# Append to string
SET greeting "Hello"
APPEND greeting " World"
GET greeting # "Hello World"
# Get string length
STRLEN greeting # 11
# Set if not exists (only set if key doesn't exist)
SETNX user:1:email "alice@example.com"
# Returns 1 if set, 0 if key already exists
# Get multiple keys at once
MSET a 1 b 2 c 3
MGET a b c # ["1", "2", "3"]
5.4 Working with Lists
Lists are ordered sequences (like arrays). You can push/pop from both ends.
# Push to right (end)
RPUSH tasks "task1"
RPUSH tasks "task2"
RPUSH tasks "task3"
# Push to left (beginning)
LPUSH tasks "urgent_task"
# Pop from right
RPOP tasks # removes and returns last task
# Pop from left
LPOP tasks # removes and returns first task
# Get range (0 = first, -1 = last)
LRANGE tasks 0 -1 # all elements
LRANGE tasks 0 2 # first 3 elements
# Get length
LLEN tasks
# Get element by index
LINDEX tasks 0
# Trim list (keep only range)
LTRIM tasks 0 100 # keep first 101 elements
# Blocking pop (waits if list empty)
BLPOP queue 30 # waits up to 30 seconds
Use case: Queues, timelines, message buffers.
5.5 Working with Sets
Sets are unordered collections of unique elements (no duplicates).
# Add members
SADD tags "redis"
SADD tags "database"
SADD tags "nosql"
# Get all members
SMEMBERS tags
# Check if member exists
SISMEMBER tags "redis" # 1 (true)
SISMEMBER tags "mysql" # 0 (false)
# Remove member
SREM tags "nosql"
# Get number of members
SCARD tags
# Random member (without removing)
SRANDMEMBER tags
# Pop random member (removes it)
SPOP tags
# Set operations
SADD set1 {1,2,3}
SADD set2 {2,3,4}
SUNION set1 set2 # {1,2,3,4}
SINTER set1 set2 # {2,3}
SDIFF set1 set2 # {1}
Use case: Tags, unique visitors, friends lists.
5.6 Working with Sorted Sets
Sorted Sets are like sets but each member has a score for sorting.
# Add members with scores
ZADD leaderboard 100 "Alice"
ZADD leaderboard 95 "Bob"
ZADD leaderboard 87 "Charlie"
ZADD leaderboard 100 "David" # Duplicate score allowed
# Get rank (lowest score = rank 0)
ZRANK leaderboard "Alice" # returns rank (0 is best if ascending)
# Get reverse rank (highest score = rank 0)
ZREVRANK leaderboard "Alice" # returns position from top
# Get range by rank (lowest to highest)
ZRANGE leaderboard 0 -1 WITHSCORES
# Get range by rank (highest to lowest) – Top 3
ZREVRANGE leaderboard 0 2 WITHSCORES
# Get range by score
ZRANGEBYSCORE leaderboard 90 100 # scores between 90 and 100
# Increment score
ZINCRBY leaderboard 5 "Bob" # Bob's score increases by 5
# Remove member
ZREM leaderboard "Charlie"
# Get count of members
ZCARD leaderboard
Use case: Leaderboards, priority queues, trending topics.
5.7 Working with Hashes
Hashes store field-value pairs inside a key – perfect for objects.
# Set single field
HSET user:1000 name "Alice"
HSET user:1000 age 25
# Set multiple fields
HSET user:1000 email "alice@example.com" city "New York"
# Get single field
HGET user:1000 name
# Get multiple fields
HMGET user:1000 name age
# Get all fields and values
HGETALL user:1000
# Get all field names
HKEYS user:1000
# Get all values
HVALS user:1000
# Check if field exists
HEXISTS user:1000 email
# Increment numeric field
HINCRBY user:1000 age 1
# Delete field
HDEL user:1000 city
# Get number of fields
HLEN user:1000
Use case: User profiles, product details, any object.
Chapter 6: Redis Persistence
6.1 Persistence Introduction
Persistence means saving data to disk so it survives a restart. Redis offers two persistence methods: RDB and AOF.
| Method | What it does | Best for |
|---|---|---|
| RDB | Takes snapshots at intervals | Backups, disaster recovery |
| AOF | Logs every write operation | Durability, minimal data loss |
6.2 RDB Persistence (Snapshotting)
RDB creates point-in-time snapshots of your data.
How it works:
- Redis forks a child process
- Child writes data to a temporary RDB file
- When complete, replaces old RDB file
Configuration in redis.conf:
save 900 1 # Save if at least 1 key changes in 900 seconds (15 min)
save 300 10 # Save if at least 10 keys change in 300 seconds (5 min)
save 60 10000 # Save if at least 10000 keys change in 60 seconds (1 min)
dbfilename dump.rdb
dir /var/lib/redis
Manual snapshot:
# Blocking save (stops serving until done)
SAVE
# Background save (recommended)
BGSAVE
Pros:
- Compact single file
- Faster for large datasets
- Good for backups
Cons:
- Possible data loss between snapshots
- Can be slow on large datasets (fork overhead)
6.3 AOF Persistence (Append Only File)
AOF logs every write operation. On restart, Redis replays the log.
Configuration:
appendonly yes
appendfilename "appendonly.aof"
# fsync policies:
appendfsync always # Every write (slowest, safest)
appendfsync everysec # Every second (good balance)
appendfsync no # Let OS decide (fastest, riskier)
AOF Rewrite (compacts the log):
# Manual rewrite
BGREWRITEAOF
Auto rewrite configuration:
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
Pros:
- More durable (less data loss)
- Human readable (you can open and see commands)
Cons:
- Larger file size
- Slower writes
- Longer restart time
6.4 RDB vs AOF Comparison
| Feature | RDB | AOF |
|---|---|---|
| File size | Smaller | Larger |
| Restart speed | Faster | Slower |
| Data loss risk | Minutes of data | Up to 1 second (with everysec) |
| Write performance | Better | Slightly slower |
| Readability | Binary | Human readable |
| Best for | Backups, replicas | Durability-critical apps |
Best practice: Use both! RDB for backups, AOF for durability.
Chapter 7: Intermediate Redis Concepts
7.1 Redis Pub/Sub (Publish/Subscribe)
Pub/Sub is a messaging pattern where publishers send messages to channels, and subscribers receive them.
# Terminal 1 – Subscribe to channel
SUBSCRIBE news
# Terminal 2 – Publish message
PUBLISH news "Breaking news!"
# Terminal 1 will receive:
# "Breaking news!"
# Subscribe to multiple channels
SUBSCRIBE news sports weather
# Pattern matching subscription
PSUBSCRIBE news.*
# Unsubscribe
UNSUBSCRIBE news
Use case: Real-time chat, notifications, live updates.
7.2 Transactions (MULTI/EXEC/WATCH)
Redis transactions are atomic – all commands run in sequence without interruption.
# Start transaction
MULTI
SET account:1 100
SET account:2 200
INCRBY account:1 50
EXEC # Executes all commands
# or
DISCARD # Cancels transaction
# Optimistic locking with WATCH
WATCH account:1
val = GET account:1
MULTI
SET account:1 val+10
EXEC # Only succeeds if account:1 didn't change
7.3 Introduction to Caching
Caching stores frequently accessed data in Redis to reduce load on your main database.
Simple caching pattern:
def get_user(user_id):
# Try Redis first
cached = redis.get(f"user:{user_id}")
if cached:
return cached
# Cache miss – get from database
user = db.query("SELECT * FROM users WHERE id = ?", user_id)
# Store in Redis for next time
redis.setex(f"user:{user_id}", 3600, user)
return user
7.4 Pipelines
Pipelines send multiple commands in one network round-trip.
# Without pipeline (4 round trips)
redis.set("a", 1)
redis.set("b", 2)
redis.set("c", 3)
redis.get("a")
# With pipeline (1 round trip)
pipe = redis.pipeline()
pipe.set("a", 1)
pipe.set("b", 2)
pipe.set("c", 3)
pipe.get("a")
pipe.execute()
7.5 Lua Scripting Introduction
Lua scripts run inside Redis – they are atomic and fast.
-- Script to limit API calls
-- KEYS[1] = user_id, ARGV[1] = limit, ARGV[2] = window_seconds
local key = "rate:" .. KEYS[1]
local current = redis.call('INCR', key)
if current == 1 then
redis.call('EXPIRE', key, ARGV[2])
end
if current > tonumber(ARGV[1]) then
return 0 -- Rate limit exceeded
end
return 1 -- Allowed
Load and run script:
SCRIPT LOAD "the script" # Returns SHA hash
EVALSHA <sha> 1 user123 10 60
Chapter 8: Advanced Data Types
8.1 Bitmaps
Bitmaps let you manipulate individual bits in a string.
# Set bit at position 100 to 1
SETBIT login:2024-01-01 100 1
# Get bit at position 100
GETBIT login:2024-01-01 100
# Count bits set to 1
BITCOUNT login:2024-01-01
# Find first bit set to 1 or 0
BITPOS login:2024-01-01 1
# Bitwise operations
BITOP AND result key1 key2
BITOP OR result key1 key2
Use case: Tracking daily active users (each user ID = bit position).
8.2 HyperLogLog
HyperLogLog estimates unique counts using very little memory (about 12KB).
# Add elements
PFADD unique_visitors "user123" "user456" "user789"
# Count unique elements (approximate)
PFCOUNT unique_visitors
# Merge multiple HyperLogLogs
PFMERGE total_visitors today yesterday
Use case: Unique visitor counting, search term uniqueness.
8.3 Streams
Streams are like append-only logs – perfect for event sourcing and message queues.
# Add to stream
XADD mystream * user "Alice" message "Hello"
# Returns auto-generated ID like "1704123456789-0"
# Read from stream
XRANGE mystream - + COUNT 10
# Read new messages (blocking)
XREAD BLOCK 0 STREAMS mystream $
# Consumer groups (multiple consumers)
XGROUP CREATE mystream mygroup 0
XREADGROUP GROUP mygroup consumer1 STREAMS mystream >
Use case: Event sourcing, message queues, activity feeds.
8.4 Geospatial Indexes
Store and query locations (longitude, latitude).
# Add locations
GEOADD cities 13.361389 38.115556 "Palermo"
GEOADD cities 15.087269 37.502669 "Catania"
# Calculate distance between two cities
GEODIST cities Palermo Catania km
# Find cities within 100 km of Palermo
GEORADIUS cities 13.361389 38.115556 100 km
# Get coordinates
GEOPOS cities Palermo
Use case: Find nearby restaurants, delivery drivers, friends.
Chapter 9: Redis Memory Management
9.1 Memory Management Basics
How much memory does Redis use?
- Each key has overhead (~50-100 bytes per key)
- Data structures add overhead
- Strings: length + small overhead
Check memory usage:
# General memory info
INFO memory
# Memory used by keys (estimate)
MEMORY USAGE key_name
# Find largest keys
redis-cli --bigkeys
9.2 Eviction Policies
When memory reaches maxmemory, Redis evicts keys according to policy:
| Policy | Behavior |
|---|---|
noeviction | Return error on writes (default) |
allkeys-lru | Remove least recently used keys |
volatile-lru | Remove LRU from keys with expiration |
allkeys-random | Remove random keys |
volatile-random | Remove random keys with expiration |
volatile-ttl | Remove keys with shortest TTL |
Set policy in redis.conf:
maxmemory 2gb
maxmemory-policy allkeys-lru
9.3 Memory Optimization Strategies
Tips to reduce memory usage:
- Use shorter keys:
u:1000instead ofuser:1000 - Use hashes for objects instead of separate keys
- Use appropriate data types (intset for small sets)
- Set expiration on temporary data
- Use compression for large values (client-side)
Chapter 10: Cache Design Patterns
10.1 Cache-Aside Pattern (Most common)
def get_product(product_id):
# Check cache
product = redis.get(f"product:{product_id}")
if product:
return product
# Cache miss – get from database
product = db.query("SELECT * FROM products WHERE id = ?", product_id)
# Store in cache
redis.setex(f"product:{product_id}", 3600, product)
return product
10.2 Write-Through Cache
Write to cache and database at the same time.
def update_product(product_id, data):
# Update database
db.execute("UPDATE products SET ... WHERE id = ?", product_id, data)
# Update cache
redis.set(f"product:{product_id}", data)
10.3 Write-Back (Write-Behind) Cache
Write to cache immediately, write to database later (async).
def update_product(product_id, data):
# Write to cache immediately
redis.set(f"product:{product_id}", data)
# Queue for later database write
redis.lpush("write_queue", f"{product_id}:{data}")
10.4 Session Storage Implementation
# Store user session
def create_session(user_id):
session_id = str(uuid.uuid4())
redis.hset(f"session:{session_id}", mapping={
"user_id": user_id,
"created_at": time.time(),
"ip": request.ip
})
redis.expire(f"session:{session_id}", 86400) # 24 hours
return session_id
# Validate session
def get_session_user(session_id):
return redis.hgetall(f"session:{session_id}")
10.5 Rate Limiting Systems
def rate_limit(user_id, limit=100, window=60):
key = f"rate:{user_id}"
current = redis.incr(key)
if current == 1:
redis.expire(key, window)
return current <= limit
Chapter 11: Redis Security
11.1 Authentication (AUTH)
Set password in redis.conf:
requirepass your_strong_password
Connect with password:
redis-cli -a your_strong_password
Or inside CLI:
AUTH your_strong_password
11.2 ACL (Access Control Lists) – Redis 6+
ACLs let you create users with specific permissions.
# Create user with read-only access
ACL SETUSER readonly on >password ~* +@read
# Create user with access to specific keys only
ACL SETUSER appuser on >apppass ~user:* +@all
# List all users
ACL LIST
# Show user details
ACL GETUSER appuser
11.3 Network Security
Bind to specific IP (redis.conf):
bind 127.0.0.1 # Only localhost
bind 10.0.0.1 # Only internal network
Protected mode (enabled by default):
protected-mode yes
11.4 Encryption (TLS)
For production, enable TLS:
tls-port 6379
port 0
tls-cert-file /path/to/redis.crt
tls-key-file /path/to/redis.key
tls-ca-cert-file /path/to/ca.crt
Chapter 12: Replication and High Availability
12.1 Master-Slave Replication
On replica server (redis.conf):
replicaof 192.168.1.100 6379
Check replication status:
INFO replication
12.2 Redis Sentinel Architecture
Sentinel provides high availability with automatic failover.
sentinel.conf:
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
Start Sentinel:
redis-sentinel sentinel.conf
12.3 Failover Mechanism
- Sentinel detects master is down
- Sentinel elects a leader
- Leader promotes a replica to master
- Other replicas reconfigure to new master
- Application connects to new master
Chapter 13: Redis Cluster
13.1 Redis Cluster Architecture
Redis Cluster provides horizontal scaling across multiple nodes.
Features:
- Automatic sharding (data distribution)
- High availability (failover)
- Linear scalability (add nodes to increase capacity)
13.2 Sharding and Data Partitioning
Data is distributed using hash slots (16384 total slots).
Key "user:1000" → CRC16("user:1000") % 16384 = slot 5782 → Node A
Key "product:200" → CRC16("product:200") % 16384 = slot 10234 → Node B
Create a cluster:
redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 --cluster-replicas 1
13.3 Consistent Hashing
Consistent hashing minimizes key redistribution when nodes are added/removed.
When to use client-side sharding:
- You need more control
- Simpler setup than Redis Cluster
- Use libraries like
redis-shardortwemproxy
Chapter 14: Redis Modules
14.1 RedisJSON – Store and query JSON
# Install module
# Load with: redis-server --loadmodule ./redisjson.so
# Set JSON
JSON.SET user:1000 $ '{"name":"Alice","age":25}'
# Get JSON
JSON.GET user:1000 $.name
# Update nested value
JSON.SET user:1000 $.age 26
# Increment number
JSON.NUMINCRBY user:1000 $.age 1
14.2 RedisSearch – Full-text search
# Create index
FT.CREATE idx:users ON JSON PREFIX 1 user: SCHEMA $.name AS name TEXT $.age AS age NUMERIC
# Search
FT.SEARCH idx:users "@name:Alice"
# Aggregate
FT.AGGREGATE idx:users "*" GROUPBY 1 @age REDUCE COUNT 0 AS count
14.3 RedisGraph – Graph database
# Create nodes and relationships
GRAPH.QUERY social "CREATE (:Person {name:'Alice'})-[:KNOWS]->(:Person {name:'Bob'})"
# Query
GRAPH.QUERY social "MATCH (p:Person)-[:KNOWS]->(f:Person) RETURN p.name, f.name"
14.4 RedisTimeSeries – Time-series data
# Create time-series
TS.CREATE temperature RETENTION 86400000
# Add data points
TS.ADD temperature 1704123456789 23.5
# Query range
TS.RANGE temperature 1704123000000 1704124000000
Chapter 15: Production and Deployment
15.1 Dockerizing Redis
docker-compose.yml:
version: '3.8'
services:
redis:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
- redis_data:/data
- ./redis.conf:/usr/local/etc/redis/redis.conf
command: redis-server /usr/local/etc/redis/redis.conf
volumes:
redis_data:
Run:
docker-compose up -d
docker exec -it redis redis-cli
15.2 Kubernetes Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
spec:
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:7-alpine
ports:
- containerPort: 6379
---
apiVersion: v1
kind: Service
metadata:
name: redis
spec:
selector:
app: redis
ports:
- port: 6379
targetPort: 6379
15.3 Redis Cloud Platforms
| Provider | Service |
|---|---|
| AWS | ElastiCache for Redis |
| Azure | Azure Cache for Redis |
| GCP | Memorystore for Redis |
| Redis Labs | Redis Enterprise Cloud |
15.4 Backup and Disaster Recovery
# Manual RDB snapshot
redis-cli BGSAVE
# Backup the RDB file
cp /var/lib/redis/dump.rdb /backup/redis-$(date +%Y%m%d).rdb
# Backup AOF file
cp /var/lib/redis/appendonly.aof /backup/
# Restore
# Stop Redis, copy backup to data directory, restart
15.5 Monitoring with Prometheus and Grafana
redis_exporter exposes Redis metrics:
# Run redis_exporter
docker run -d -p 9121:9121 oliver006/redis_exporter --redis.addr redis://localhost:6379
Prometheus config:
scrape_configs:
- job_name: 'redis'
static_configs:
- targets: ['localhost:9121']
Metrics to monitor:
connected_clients– Active connectionsused_memory– Memory usagetotal_commands_processed– Command raterejected_connections– Max client limit reachedkeyspace_hits/misses– Cache hit ratio
Chapter 16: Internal Working of Redis
16.1 Core Architecture (Single-Threaded Event Loop)
Redis uses a single-threaded event-driven architecture:
Client Connections → Event Loop → Command Processor → Data Store → Response
↓
Optional Persistence
Why single-threaded?
- No locks, no context switching
- All operations are atomic
- Predictable performance
- Simple code (no race conditions)
How is it fast?
- In-memory (no disk I/O for reads)
- Efficient data structures
- Non-blocking I/O (epoll/kqueue)
16.2 Internal Execution Flow
- Client sends command (e.g.,
SET key value) - Command reaches Redis server via TCP port 6379
- Event loop picks up the request (non-blocking)
- Command is parsed from RESP (Redis Serialization Protocol)
- Command handler is looked up in command table
- Data structure operation is executed in memory
- Optional persistence triggers (RDB/AOF)
- Response is formatted in RESP
- Response sent back to client
- Event loop continues to next request
16.3 Memory Flow
Application Data → Redis Command → Memory Allocation → Data Structure
↓
Optional: Persistence to Disk
↓
RDB Snapshot or AOF Log
Memory layout:
- Each key has a dictEntry (key pointer, value pointer, next pointer)
- Keys are stored as SDS (Simple Dynamic Strings)
- Values are stored as redisObjects with type encoding
16.4 Command Execution Flow (Detailed)
1. Client sends: *3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n
2. Redis parses RESP to command array: ["SET", "key", "value"]
3. Look up "SET" in command table → points to setCommand()
4. setCommand() calls genericSetCommand()
5. Key is created or updated in dict (hash table)
6. Value is stored as robj (redis object)
7. SignalModifiedKey() is called
8. If keyspace notifications enabled, publish event
9. If AOF enabled, append command to AOF buffer
10. If replicas exist, propagate command to replication buffer
11. Response: "+OK\r\n" sent to client
16.5 Module System
Redis supports modules (dynamic libraries) that can:
- Add new data types
- Add new commands
- Extend existing functionality
Example module commands:
JSON.SET(RedisJSON)FT.SEARCH(RedisSearch)TS.ADD(RedisTimeSeries)
16.6 Full Pipeline from Code to Output
Developer writes: redis.set("name", "Alice")
↓
Redis client library formats command as RESP
↓
TCP connection to Redis server (port 6379)
↓
Redis event loop receives data
↓
Command parser extracts "SET name Alice"
↓
Redis stores in memory hash table
↓
Response "OK" sent back
↓
Client library returns true
↓
Developer sees success
Chapter 17: Your First Program – Hello Redis
17.1 Step-by-Step Code Explanation
Command in Redis CLI:
SET greeting "Hello Redis"
Line-by-line understanding:
| Part | Meaning |
|---|---|
SET | Command to store a key-value pair |
greeting | The key (unique identifier) |
"Hello Redis" | The value (string data) |
What Redis does internally:
- Receives the SET command
- Creates or updates key “greeting”
- Stores value “Hello Redis” in memory
- Returns “OK”
- (If persistence enabled) writes to RDB/AOF
Get the value:
GET greeting
# Returns: "Hello Redis"
17.2 Running Your First Commands
Start Redis:
redis-server
Open another terminal and start CLI:
redis-cli
Your first interaction:
127.0.0.1:6379> SET name "Redis Learner"
OK
127.0.0.1:6379> GET name
"Redis Learner"
127.0.0.1:6379> EXISTS name
(integer) 1
127.0.0.1:6379> DEL name
(integer) 1
127.0.0.1:6379> GET name
(nil)
Congratulations! You have successfully used Redis.
Chapter 18: Project Folder Structure
18.1 Basic Project Structure
my-redis-project/
│
├── src/
│ ├── app.js # Main application
│ ├── redis-client.js # Redis connection setup
│ └── config.js # Configuration
│
├── scripts/
│ └── setup.sh # Setup scripts
│
├── tests/
│ └── redis.test.js # Unit tests
│
├── .env # Environment variables
├── .gitignore
├── package.json
└── README.md
18.2 Advanced Project Structure
production-redis-project/
│
├── src/
│ ├── controllers/
│ │ └── userController.js
│ ├── services/
│ │ ├── cacheService.js
│ │ └── rateLimiter.js
│ ├── middleware/
│ │ └── auth.js
│ ├── utils/
│ │ └── redisClient.js
│ └── app.js
│
├── config/
│ ├── redis.conf
│ ├── sentinel.conf
│ └── production.conf
│
├── docker/
│ ├── Dockerfile
│ └── docker-compose.yml
│
├── k8s/
│ ├── deployment.yaml
│ └── service.yaml
│
├── scripts/
│ ├── backup.sh
│ ├── monitor.sh
│ └── deploy.sh
│
├── tests/
│ ├── unit/
│ ├── integration/
│ └── e2e/
│
├── docs/
│ └── architecture.md
│
├── .env
├── .gitignore
├── package.json
└── README.md
Chapter 19: Conclusion and Final Skill Stack
After completing this roadmap, you will be able to:
✅ Understand Redis fundamentals and use cases
✅ Install Redis on Windows, macOS, and Linux
✅ Use all basic data types (Strings, Lists, Sets, Sorted Sets, Hashes)
✅ Set expiration and TTL
✅ Configure persistence (RDB and AOF)
✅ Use Pub/Sub for messaging
✅ Write transactions and Lua scripts
✅ Use advanced data types (Bitmaps, HyperLogLog, Streams, Geospatial)
✅ Implement caching patterns
✅ Set up replication and Sentinel for high availability
✅ Deploy Redis Cluster for horizontal scaling
✅ Use Redis modules (JSON, Search, TimeSeries)
✅ Dockerize and deploy on Kubernetes
✅ Monitor with Prometheus and Grafana
✅ Secure Redis with authentication and ACLs
✅ Design production-ready systems with Redis
Final Thoughts
To a child starting out:
Imagine Redis as a magical whiteboard. You can write anything on it – words, lists, even maps. The magic is that you can read what you wrote instantly – faster than opening a notebook or asking a friend. When you’re done, you can erase it or let it disappear by itself after a certain time. That is Redis – a magical whiteboard for computer programs.
Your journey:
Start with installation. Then learn SET and GET. Then explore lists, sets, and hashes. Add expiration. Learn about persistence. Then Pub/Sub. Then replication. Then clustering. Each step builds on the last.
Remember: Redis is used by the biggest companies in the world – Twitter, GitHub, Discord, Snapchat. They started just like you – with a simple SET and GET. With practice, you can build systems that handle millions of requests per second.
Keep coding. Keep caching. Keep scaling.