Skip to content
general performance

Caching

caching performance redis cdn
Plain English

Caching is like keeping a photocopy of a library book on your desk instead of walking to the library every time you need it. The first time you request a web page, your browser downloads it from the server. The second time, it uses the saved copy (the cache), which is much faster. Caching happens at every level of computing: your browser, your DNS server, your web server, your database, and even your CPU all use caches to avoid repeating slow work.

Technical Definition

Caching stores the result of an expensive computation or data retrieval in a faster storage layer so subsequent requests can be served without repeating the original operation.

Cache levels (from closest to user to farthest):

LevelLocationSpeedExample
CPU L1/L2/L3Processor~1-30 nsHardware cache
ApplicationProcess memory~100 nsIn-memory hashmap
DistributedSeparate service~1 msRedis, Memcached
CDN EdgeGlobal PoPs~10-50 msCloudflare, CloudFront
BrowserUser’s device~0 ms (local)HTTP cache, localStorage
DNSResolver~0-50 msDNS record TTL

Cache strategies:

  • Cache-aside (lazy loading): application checks cache first; on miss, fetches from source and populates cache
  • Write-through: writes go to both cache and source simultaneously
  • Write-behind (write-back): writes go to cache first; cache asynchronously writes to source
  • Read-through: cache automatically fetches from source on miss (transparent to application)

Cache invalidation (the hard problem):

  • TTL (Time to Live): entries expire after a fixed duration
  • Event-driven: invalidate when underlying data changes
  • Cache-busting: append version/hash to URLs so new content bypasses old cache (Astro uses content-hashed filenames: /_astro/file.abc123.js)

HTTP caching headers:

  • Cache-Control: max-age=3600: cache for 1 hour
  • Cache-Control: no-cache: validate with server before using cache
  • Cache-Control: no-store: never cache (sensitive data)
  • ETag: content fingerprint for conditional requests (304 Not Modified)

Caching at different layers

# HTTP caching headers (Nginx)
location /_astro/ {
    expires 1y;                  # Immutable hashed assets
    add_header Cache-Control "public, immutable";
}
location /api/ {
    add_header Cache-Control "no-cache, must-revalidate";
}

# Redis caching (CLI)
$ redis-cli SET user:123 '{"name":"Mokey"}' EX 3600  # TTL 3600s
$ redis-cli GET user:123
"{\"name\":\"Mokey\"}"
$ redis-cli TTL user:123
(integer) 3542

# Check browser cache behavior
$ curl -sI https://bytesnation.com/_astro/index.abc123.js | \
  grep -i cache
Cache-Control: public, max-age=31536000, immutable

# DNS cache TTL
$ dig +noall +answer bytesnation.com
bytesnation.com.  300  IN  A  76.76.21.21
# 300 seconds = 5 minute cache
In the Wild

Caching is the single most effective technique for improving application performance. CDNs cache static assets at edge locations worldwide, reducing latency from 100ms+ to under 10ms. Redis is the standard distributed cache for session data, API responses, and computed results. The BytesNation site uses Astro’s content-hashed filenames with 1-year cache headers: browsers cache assets permanently until the content changes (at which point the filename hash changes, busting the cache automatically). The most famous quote in computer science is “there are only two hard things in computer science: cache invalidation and naming things.” Stale cache is a frequent source of “it works for some users but not others” bugs. When troubleshooting, “have you cleared the cache?” remains the most common first step.