Caching (hit vs. miss)
Easy+12 XPWatch the first request go the slow way, then the second one fly — because the cache saved a copy.
No visualization loaded.
Watch
—
Press Run to begin.
A cache is a small, fast box that keeps copies of recent answers close by, so you don't redo slow work. Press Run and watch the SAME request twice: the first time misses (slow), the second time hits (instant).
▸What's the difference between a hit and a miss?
A HIT means the cache already has the answer — it replies instantly. A MISS means the cache doesn't have it, so it has to go ask the slow database, then save a copy for next time. Miss = slow once; hit = fast forever after (until the copy expires).
▸If the cache is so fast, why not store EVERYTHING in it?
Because fast storage is small and expensive. You can't fit the whole database in it. So a cache only keeps the popular stuff and throws out what's not being used — that's called eviction (a common rule is 'throw out the Least Recently Used', LRU).
▸What if the real answer changes but the cache still has the old copy?
That's the big danger: stale data. The cached copy can be out of date. To fight it, copies are given an expiry time (a TTL — 'time to live'); after that, the cache throws the copy away and asks fresh. Caching trades a little freshness for a lot of speed.
▸Where does the cache actually sit?
Between you and the slow thing (here, the database). Every request checks the cache first. Hit? Answer now. Miss? Go the slow way, then save a copy on the way back. That 'check fast box first' habit is the whole pattern.