Back to Redis Course
    Module 1

    πŸ“˜ Redis Fundamentals

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

    Redis Architecture Diagram

    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

    AspectRedis (In-Memory)Traditional DB (Disk-Based)
    SpeedSub-millisecond latencyMilliseconds to seconds
    StorageRAM (limited by memory)Disk (virtually unlimited)
    PersistenceOptional (RDB/AOF)Built-in & durable
    Data ModelKey-Value + Rich Data TypesTables, Documents, etc.

    Redis Use Cases

    πŸš€ Caching

    Store frequently accessed data to reduce DB load and improve response times.

    πŸ“‹ Message Queue

    Use Lists or Streams for async task processing and job queues.

    πŸ” Session Store

    Distributed session management for web applications.

    πŸ† Leaderboards

    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 Architecture Diagram
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚                 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)

    Docker Commands
    # 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-cli

    Option 2: Local Installation

    🐧 Linux/WSL
    Linux
    sudoapt update
    sudoaptinstall redis-server
    sudo systemctl start redis
    🍎 macOS
    macOS
    brew install redis
    brew services start redis

    Verify Installation

    Verification
    $ redis-cli ping
    PONG
    $ redis-cli info server |head-5# Server
    redis_version:7.0.11
    redis_git_sha1:00000000
    redis_git_dirty:0

    Redis CLI Basics

    Essential Commands

    Redis CLI - Basic 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

    Redis CLI - TTL Commands
    # 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!)

    πŸ’¬ Comments & Discussion