Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced18 min

Caching Layers: Redis, Cache-Aside, and Invalidation

Understand the idea

Why in-memory caching protects your database, and why cache invalidation is notoriously hard.

The idea

The fastest database query is the one your database never has to run. A caching layer sits between your web app and the database, keeping popular data in super-fast server memory (RAM) using a like Redis. This gives users sub-millisecond responses and prevents your main database from getting overwhelmed.

The most common approach is the Cache-Aside pattern:

  1. When your app needs data (like a product page), it checks the cache first. If it finds it (a cache hit), it returns it in under a millisecond.
  2. If the data is not in the cache (a cache miss), the app queries the database, sends the result to the user, and saves a copy in the cache with an expiration timer.
  3. When data changes in the database, the app performs -removing or updating the old copy in the cache so users don't see outdated info.

Cache invalidation is famously one of the trickiest parts of software engineering: if your code forgets to clear a cached item when data changes, users see stale or incorrect information.

What changes

Caching Strategies Compared
PatternHow it WorksAdvantagesDisadvantages
Cache-AsideApp checks cache; queries DB on miss; populates cacheOnly requested data is cached; resilient to cache node restartsCache miss penalty; risk of serving stale data if not invalidated
Write-ThroughApp writes to cache; cache synchronously writes to DBCache is always up to date with the databaseHigher write latency; caches data that might never be read again
Write-BehindApp writes to cache; cache asynchronously batches to DBUltra-fast write throughputRisk of data loss if cache crashes before flushing to disk

Cache hit and miss

FAST PATH · cache hit0 / 2 hops
Request
Cache · HIT
Response

Cache: product:42 → Keyboard

SLOW PATH · cache miss0 / 5 hops
Request
Cache · MISS
Database
DB result
Cache · SET
Response

Cache: empty

Both requests ask for product:42. The hit starts with a populated cache; the miss starts with an empty cache.

01 / 06Cache hit and miss

The command

Cache-aside pattern with mutation invalidation

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

The mistake

Check the result

Run the example to see the Cache-Aside pattern fetch from DB, hit cache on second read, and cleanly invalidate upon mutation.

Guided challenge

Trigger cache invalidation

Update the function to return true when operation is 'UPDATE' or 'DELETE', and false for 'SELECT'.

Trigger cache invalidation

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

EXPECTED RESULT

true
false

Hints 0/1

    Use all hints or make 3 failed checks (0/3).

    Free exploration

    Change this example and run it again. Each run is a new process. Nothing is saved.

    Your variation

    3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01How does the Cache-Aside (Lazy Loading) pattern handle read requests?
    02What occurs during a 'cache stampede' (dog-piling)?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson