Caching: Why Speed Needs Memory
Store the answer so you never ask twice
Store the answer so you never ask twice
Click "Request Data" to fetch user data. Without cache, every request goes to the slow database (200ms). With cache, the first request is slow but every repeat is instant (1ms). Toggle the cache to see the difference.
Different storage has wildly different speeds. Caching moves data up the hierarchy, closer to the CPU, where access is faster.
Redis stores data as key-value pairs in memory. SET a key, GET it back instantly. It is like a dictionary that lives in RAM and responds in microseconds.
Caching is everywhere, at every level of every system.
Your browser caches images, CSS, and JS. Revisiting a page loads instantly because assets are already on your disk. That is why "clear cache" is a common troubleshooting step.
Content Delivery Networks cache your website on servers worldwide. A user in Tokyo gets content from a nearby CDN server instead of your origin server in New York.
If 1000 users load the homepage, the database query runs once and the result is cached. The other 999 requests get the cached result instantly.
Google caches the entire searchable web. When you search, you are not actually searching the live internet. You are searching Google's cached copy, which is rebuilt by web crawlers continuously. That is why search results appear in milliseconds.
You've learned why caching makes everything faster: store the result of expensive operations so you do not have to repeat them. RAM is 100,000x faster than disk. Use it wisely.
A cache stores a copy of data in a faster location. Instead of querying the database every time, store the result in RAM. Next request gets the cached copy instantly.
Cache hit: the data is in the cache, served instantly. Cache miss: not in cache, must fetch from the slow source, then store for next time. Hit rate is the key metric.
Cached data expires after a Time To Live. Without TTL, you might serve outdated data forever. With TTL, the cache refreshes periodically.
Redis is an in-memory key-value store. It holds data in RAM, responding in microseconds. It sits between your app and database, absorbing repeated queries.
Put your new knowledge into practice!