Back to course
    article~25 min readCaching & Performance

    Redis Fundamentals

    Apply in-memory caching to reduce latency and database pressure.

    Redis Fundamentals — Beginner Friendly System Design Guide

    Why Applications Become Slow

    Imagine Swiggy database receives:

    10 lakh requests per minute
    

    Every user:

    • searches restaurants
    • refreshes homepage
    • checks orders

    If every request directly hits database:

    Users
      │
      ▼
    Application Server
      │
      ▼
    Database Overload ❌
    

    Problems:

    • slow APIs
    • high latency
    • database crashes

    This problem is solved using:

    Redis
    

    What Is Redis?

    Redis is:

    An extremely fast in-memory database used mainly for caching and real-time systems.

    Redis stores data:

    • in RAM (memory)
    • instead of slow disk storage

    This makes Redis:

    • incredibly fast

    Simple Real-Life Analogy

    Imagine restaurant kitchen.

    Without Redis:

    • chef cooks every order from scratch

    Slow.

    With Redis:

    • popular dishes already prepared nearby

    Much faster.

    Redis works similarly.


    Why Redis Is Fast

    Normal databases:

    • read from disk

    Disk is slower.

    Redis:

    • stores data in memory (RAM)

    Memory is extremely fast.


    Speed Comparison

    Storage TypeApproximate Speed
    Disk DatabaseMilliseconds
    Redis MemoryMicroseconds

    Huge difference at scale.


    Real Example — Swiggy Homepage

    Millions of users open Swiggy homepage.

    Popular restaurant lists rarely change every second.

    Instead of:

    • querying database repeatedly

    Swiggy may cache results in Redis.


    Redis Cache Flow

    User Opens Swiggy
            │
            ▼
    Application Server
            │
            ▼
    Check Redis Cache
       │               │
    Hit              Miss
       │                │
       ▼                ▼
    Fast Response    Query Database
                         │
                         ▼
                  Store In Redis
    

    What Is Cache Hit?

    Data found in Redis.

    Very fast response.


    What Is Cache Miss?

    Data not found in Redis.

    Application queries:

    • database

    then stores result in Redis.


    Why Redis Reduces Database Pressure

    Without Redis:

    Every Request
          │
          ▼
    Database Query
    

    Database becomes bottleneck.


    With Redis

    Most Requests
          │
          ▼
    Redis Cache
          │
          ▼
    Fast Response
    

    Database load reduces massively.


    Real Example — Instagram Likes Count

    Millions repeatedly view:

    • likes
    • follower counts
    • post counts

    Instead of:

    • recalculating every time

    Redis stores counters in memory.


    Common Redis Use Cases

    Use CaseExample
    CachingProduct pages
    SessionsUser login state
    CountersLikes/views
    QueuesBackground jobs
    Rate limitingOTP throttling
    LeaderboardsGaming ranks

    Redis As Session Store

    When user logs into app:

    • session data stored temporarily

    Redis is commonly used.


    Session Flow

    User Logs In
          │
          ▼
    Session Created
          │
          ▼
    Stored In Redis
          │
          ▼
    Future Requests Validated
    

    Why Redis Is Great For Sessions

    Because sessions need:

    • very fast access
    • temporary storage
    • quick expiry

    Redis handles this efficiently.


    Real Example — PhonePe OTP Rate Limiting

    Suppose user requests OTP repeatedly.

    Redis may store:

    phone_number → OTP count
    

    to prevent abuse.


    Rate Limiting Flow

    User Requests OTP
            │
            ▼
    Check Redis Counter
       │               │
    Limit OK        Limit Exceeded
       │                │
       ▼                ▼
    Send OTP        Block Request
    

    Redis Data Structures

    Redis supports multiple structures.


    Common Redis Structures

    StructureUsage
    StringSimple cache
    HashUser profiles
    ListQueues
    SetUnique items
    Sorted SetLeaderboards

    Example — User Cache

    Key:
    user:101
    
    Value:
    {
      "name": "Rahul",
      "city": "Jaipur"
    }
    

    Example — Leaderboard

    Gaming app may use:

    Sorted Set
    

    for rankings.


    Leaderboard Flow

    Player Scores
          │
          ▼
    Redis Sorted Set
          │
          ▼
    Top Rankings Returned
    

    Redis Expiry (TTL)

    Redis supports automatic expiry.


    Example

    OTP expires after 5 minutes
    

    Redis automatically removes expired data.


    Expiry Flow

    Store OTP In Redis
           │
           ▼
    TTL = 300 Seconds
           │
           ▼
    Auto Delete After Expiry
    

    Why Expiry Matters

    Not all cached data should live forever.

    Examples:

    • OTPs
    • sessions
    • temporary cache

    must expire automatically.


    Redis In Ecommerce

    Flipkart may cache:

    • popular products
    • trending searches
    • homepage recommendations

    using Redis.


    Redis + Database Architecture

    Users
      │
      ▼
    Application Server
      │
      ▼
    Redis Cache
      │
     ┌┴──────────┐
     ▼           ▼
    Hit         Miss
     │            │
     ▼            ▼
    Fast       Database Query
    Response
    

    Redis Persistence

    Although Redis is memory-based:

    • it can save data to disk

    using:

    • snapshots
    • append-only logs

    Why Persistence Matters

    Without persistence:

    • server restart loses data

    Persistence improves:

    • recovery
    • durability

    Redis Replication

    Redis can replicate data across servers.


    Replication Flow

    Primary Redis
          │
     ┌────┴────┐
     ▼         ▼
    Replica1 Replica2
    

    Benefits:

    • high availability
    • failover
    • read scaling

    Redis Cluster

    Large-scale systems use:

    • Redis clusters

    to distribute data across nodes.


    Cluster Flow

    Application
         │
         ▼
    Redis Cluster
     ┌────┼────┐
     ▼    ▼    ▼
    Node1 Node2 Node3
    

    Real Example — Hotstar IPL

    During IPL:

    • massive traffic spikes happen

    Redis helps cache:

    • live scores
    • match stats
    • user sessions

    reducing backend pressure.


    Redis vs Traditional Database

    RedisTraditional DB
    In-memoryDisk-based
    Extremely fastSlower
    Temporary data friendlyPersistent storage
    Best for cachingBest for durable storage

    Important Trade-Off

    Redis is fast because:

    • data stored in RAM

    But RAM is:

    • expensive
    • limited

    So Redis usually stores:

    • frequently accessed data only

    What Should Be Stored In Redis?

    Good candidates:

    • cache
    • sessions
    • counters
    • temporary state
    • hot data

    What Should NOT Be Stored Only In Redis?

    Avoid using Redis as:

    • permanent source of truth

    for critical systems like:

    • banking transactions
    • long-term records

    Common Beginner Misconceptions


    "Redis Replaces Database"

    Usually no.

    Redis complements databases.


    "Everything Should Be Cached"

    No.

    Poor caching:

    • wastes memory
    • creates stale data problems

    "Redis Is Only For Big Companies"

    Even small apps benefit from Redis.


    Real Production Architecture

    Users
      │
      ▼
    CDN
      │
      ▼
    Load Balancer
      │
      ▼
    Application Servers
      │
      ▼
    Redis Cache
      │
      ▼
    Primary Database
    

    Final Mental Model

    Redis =
    Ultra-fast temporary storage
    used to reduce latency and database load
    

    One-Line Interview Definition

    Redis is a high-performance in-memory datastore commonly used for caching, sessions, counters, queues, and real-time workloads to reduce latency and offload databases.

    Module context

    From browser cache to Redis clusters and CDNs. Caching strategies that turn slow apps into fast ones, with real-world invalidation patterns.

    Mark this lesson complete when you finish reviewing it.