Appearance
Caching (Sample Note)
Sample Note — Safe to Delete
Local Test Note
This is a temporary sample note created so you can test Cmd+K search, visual pictures, interactive animated flow diagrams, code copy buttons, and collapsible self-check cards locally. Once we start adding the topics you learn, we can remove or replace this file.
In Plain English (30-Second Refresher)
Caching means keeping a copy of frequently needed data in a super-fast, nearby place (like RAM, the computer's Random-Access Memory) so your app doesn't have to do slow work (like querying a disk database or calling a remote API, i.e. application programming interface) over and over again. A cache hit typically answers in about 1 ms (a millisecond, a thousandth of a second).
Real-World Example
Think of caching like keeping your active study notebook on your desk instead of walking down to the basement filing cabinet every time you need a formula:
- Your Desk → Cache (small, super fast—e.g., RAM or Redis)
- Basement Filing Cabinet → Database (huge, slower—stores everything safely)
- Notebook already on desk → Cache Hit (instant!)
- Walking downstairs to grab it → Cache Miss (slow once, then you put it on your desk for next time)
- Clearing old papers when the desk gets full → Eviction Policy (e.g., LRU — Least Recently Used)
- A sticky note saying “re-check in 5 minutes” → TTL (Time-To-Live): how long a cached copy may be used
Visual Mental Model
Caption: A Cache Hit returns from memory in ~1 ms; a Cache Miss fetches from the database and saves a copy in the cache for future requests.
How It Works (Interactive Animated Flow)
Click Cache Hit or Cache Miss below to watch how requests flow step by step:
Cache-Aside Request Flow
Switch scenarios or step through manually to watch live packet flow
- Check the cache first: The app asks the cache for a key (like
user:42). - If found (Cache Hit): Return it immediately—the database isn't even touched.
- If missing (Cache Miss): Fetch from the database, save a copy into the cache with a TTL (Time-To-Live) expiration, and return it.
Minimal Example (Cache-Aside Pattern)
js
async function getUserProfile(userId) {
const key = `user:${userId}`
// 1. Check fast cache first
const cached = await cache.get(key)
if (cached) return JSON.parse(cached) // Cache Hit!
// 2. Cache Miss: load from slow database
const user = await db.findUserById(userId)
// 3. Save to cache for 5 minutes (300 seconds)
await cache.set(key, JSON.stringify(user), { EX: 300 })
return user
}When to Use vs. Avoid (Trade-offs)
| Use Caching When | Be Careful / Avoid When |
|---|---|
| The same data is read far more often than updated (profiles, product pages) | Data must be 100% real-time exact (bank account balance during a transfer) |
| Computing or fetching the data is slow or expensive | Every request asks for completely unique data (cache hit rate stays near 0%) |
Common Gotchas
Watch Out For Stale Data
If a user updates their profile in the database, the old copy sitting in the cache becomes stale (outdated) until it expires (TTL) or you explicitly delete/update the cache key.
Quick Self-Check (Restore Your Memory)
Try answering in your head before expanding each answer:
1. What is the difference between a Cache Hit and a Cache Miss?
A Cache Hit means the requested data was already in the fast cache and returned immediately. A Cache Miss means it wasn't there, so the app had to fetch it from the slower source of truth (like the database) and store a copy in the cache.
2. Why do we set a TTL (Time-To-Live) on cached items?
Two reasons: (1) to prevent stale data from living in the cache forever after the underlying database changes, and (2) to keep limited cache memory from filling up with old, unused entries.