Caching Strategies for Better Performance
Advertisement
Ad
What is Caching?
Caching stores copies of data so future requests are served faster — avoiding repeated expensive work like database queries or network calls.
Types of Caching
| Type | Where |
|---|---|
| Browser cache | User's device |
| CDN cache | Edge servers |
| Server cache | Redis, Memcached |
| Database cache | Query results |
Browser Caching
Cache-Control: max-age=31536000, immutable # static assets
Cache-Control: no-cache # always revalidate
Server-Side with Redis
// Check cache first
let data = await redis.get("users");
if (!data) {
data = await db.query("SELECT * FROM users");
await redis.set("users", JSON.stringify(data), "EX", 300);
}
Cache Invalidation
"There are only two hard things: cache invalidation and naming things." Strategies:
- TTL — expire after a set time.
- On write — clear cache when data changes.
- Versioned URLs —
app.v2.js.
FAQs
What should I cache?
Frequently-read, rarely-changing data — static assets, API responses. More in our Performance guides.
What is a CDN?
A network of servers that cache your content near users globally.
