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:
- 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.
- 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.
- 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
| Pattern | How it Works | Advantages | Disadvantages |
|---|---|---|---|
| Cache-Aside | App checks cache; queries DB on miss; populates cache | Only requested data is cached; resilient to cache node restarts | Cache miss penalty; risk of serving stale data if not invalidated |
| Write-Through | App writes to cache; cache synchronously writes to DB | Cache is always up to date with the database | Higher write latency; caches data that might never be read again |
| Write-Behind | App writes to cache; cache asynchronously batches to DB | Ultra-fast write throughput | Risk of data loss if cache crashes before flushing to disk |
Cache hit and miss
Cache: product:42 → Keyboard
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.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.