SQL Tutorial 0/85 lessons ~6 min read Lesson 51

    Database Caching

    Caching avoids hitting the DB.

    Course progress0%
    Focus
    7 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    Caching avoids hitting the DB. Layers: query cache, ORM cache, Redis/Memcached in front, CDN for static. Done well, you cut DB load by 10×.

    Understanding the topic

    Core concepts to understand:

    • Buffer cache — DB-internal hot pages in RAM.
    • Materialized views — cached query results.
    • Redis/Memcached — app-level cache.
    • HTTP/CDN cache — for read-only public data.
    • Cache invalidation is the hard part — design upfront.

    Syntax reference

    Visual workflow / architecture:

    bash
    Client → CDN → API → Redis → DB
    ▲ ▲ ▲
    └ cold path falls through layers
    ▼ writes invalidate caches

    Real-world use

    GitHub, Twitter and Reddit all serve > 90% of reads from caches in front of their SQL DBs.

    Best practices

    • Cache hot reads, never writes.
    • Pick a clear invalidation strategy (TTL, event, write-through).
    • Monitor hit ratio.

    Common mistakes

    • Stale caches serving wrong data.
    • Thundering herd on cache miss.
    • Cache stampede — use stampede protection.

    Hands-on exercise

    Interview preparation — practice these questions:

    • Q1. Cache invalidation strategies.
    • Q2. Thundering herd — what is it?
    • Q3. Where do you cache: CDN, app, DB?
    • Q4. Materialized view as cache.
    • Q5. Hit ratio target.
    Ready to mark this lesson complete?Track your journey across the entire course.