What is HTTP Caching?
HTTP caching is a mechanism that improves performance by reusing responses for identical requests.Without Caching
Problem: Queries the DB for the same data every time (slow, server load)With Caching
Benefits: Instant response without server request (fast, reduced server load)Cache-Control Header
Basic Structure
public: Can be stored by all caches (browser, CDN)max-age=3600: Valid for 3600 seconds (1 hour)
Key Directives
- Storage Location
- No Caching
- Expiration Time
- Revalidation
public vs private
- public: Can be stored by all caches (browser, CDN, proxy)
- Use case: Same response for all users (product lists, announcements)
- private: Can only be stored in browser (not CDN/proxy)
- Use case: Different response per user (my page, shopping cart)
Stale-While-Revalidate
A strategy that returns expired cache (stale) immediately while updating in the background.How It Works
Benefits:- User always gets fast response (returns stale immediately)
- Background update ensures fresh data for next user
stale-while-revalidate.
Stale-If-Error
Uses stale cache when errors occur.- Normal situation: Updates every 60 seconds
- Server error occurs: Uses stale cache for up to 86400 seconds (1 day)
- Server recovers: Returns to normal updates
Vary Header
Allows different caches based on request headers for the same URL.Accept-Language header value
Example:
Accept-Language: Multi-language supportAccept-Encoding: Compression method (gzip, br)Authorization: Authentication status
Sonamu vs BentoCache
Sonamu has two caching mechanisms:- Cache-Control (HTTP)
- BentoCache (Server)
Browser/CDN CachingLocation: Browser, CDN, Proxy
Control: HTTP headers
Target: Final response received by client
Characteristics:
- Reduces network traffic
- Client controlled (browser manages cache)
- Difficult to invalidate
Combined Usage
Using both together provides maximum performance:- First request: DB query (slow) → Server cache + HTTP cache
- Within 60 seconds: Returns from browser cache (very fast, no server request)
- 60 seconds - 10 minutes: Server request but uses server cache (fast, no DB query)
- After 10 minutes: DB query (slow) → Cache again
Caching Layers
Role of each layer:- Browser Cache: Per-user cache, fastest
- CDN Cache: Regional cache, reduces network latency
- Server Cache: Reduces DB load, instant invalidation possible
- Database: Source of truth
Practical Examples
1. Public API (Same for all users)
2. Personalized API (Different per user)
3. SSR Page
4. Static Files (With Hash)
5. Sensitive Data
Precautions
Next Steps
Cache Presets
Using predefined cache configurations
Using in APIs
Apply Cache-Control to @api decorators
Using in SSR
Apply Cache-Control to SSR pages