Caching Explained
How the Internet Avoids Repeated Work
Caching is the process of storing copies of data in a temporary storage location (a cache) so that future requests for that data can be served faster. In the context of the web, caching happens at many different layers between the user and the server.
Layers of Caching
- Browser Caching: Your web browser (Chrome, Firefox, Safari) stores images, scripts, and stylesheets on your local hard drive. When you revisit a site, the browser loads these files from your disk instead of downloading them again.
- CDN Caching: Content Delivery Networks store copies of your website's files on servers located close to your users.
- Reverse Proxy/Load Balancer Caching: Tools like Nginx or Varnish can sit in front of your web server and cache entire pages or API responses.
- Application/Database Caching: Developers use tools like Redis or Memcached to store the results of expensive database queries or complex calculations in memory (RAM).
Cache Invalidation: The Hard Part
The biggest challenge in caching is knowing when to delete or update the cached data. This is called **cache invalidation**. If you cache a profile picture forever, and the user changes it, they will keep seeing the old one.Common strategies include:
- TTL (Time to Live): Giving the cached data an expiration date (e.g., "cache this for 1 hour").
- Purging: Explicitly telling the cache to delete a specific file because it has changed.
- Cache Busting: Adding a unique version number or hash to the filename (e.g.,
style.v2.css). If the file changes, the name changes, and the cache treats it as a brand-new file.
Cache-Control Headers
Web servers use HTTP headers to tell browsers and CDNs how to cache content:- Cache-Control: public, max-age=31536000: Anyone can cache this for 1 year. (Good for static images).
- Cache-Control: no-store: Do not cache this at all. (Good for sensitive bank account info).
- Cache-Control: no-cache: You can cache this, but you must check with the server before using it to see if it's still valid.
Key Metrics
- Cache Hit: The requested data was found in the cache. This is fast!
- Cache Miss: The data was not in the cache and had to be fetched from the original source. This is slower.
- Hit Ratio: The percentage of requests served from the cache (e.g., a 95% hit ratio means the cache is very effective).