Implementing Caching Strategies
1. Configuring Cache Storage
| Storage | Latency | Capacity |
|---|---|---|
| In-process LRU | < 1µs | MBs |
| Local disk | 1-5 ms | GBs |
| Redis | 1-2 ms | 10s GBs |
| Memcached | 1-2 ms | 10s GBs |
| CDN edge | 10-30 ms (first), instant repeat | PBs |
2. Setting Cache TTL Values (time-to-live)
| Content | TTL |
|---|---|
| Static assets | 1 year + cache-bust |
| Public API list | 1-5 min |
| User profile | 30s - 5min |
| Search results | 10-60s |
| Real-time data | 0 (no cache) |
3. Using Cache Key Patterns
Example: Composite cache key
key = method + ":" + path + "?" + sorted(query) +
"|user=" + user_id + "|locale=" + accept_lang +
"|enc=" + accept_encoding
hash = sha1(key)
| Include in Key | Why |
|---|---|
| Method | GET vs HEAD differ |
| Path + sorted query | Param order shouldn't matter |
| Vary headers | Per encoding/language |
| Auth scope | Per-user / public split |
4. Implementing Cache Invalidation
| Method | Granularity |
|---|---|
| TTL expiry | Coarse, time-based |
| Explicit purge by key | Single entry |
| Tag-based purge | All "user:42" entries |
| Surrogate-Key header | Fastly/Varnish style |
| Versioned key | Bump version → cache miss |
5. Configuring Cache Bypass Rules
| Bypass If | Reason |
|---|---|
| Authorization header present | Per-user response |
| Cookie set | Personalized |
Cache-Control: no-cache | Client opt-out |
| Admin/debug header | Force origin fetch |
| Non-GET method | Mutations not cached |
6. Setting Up Conditional Caching
Example: ETag conditional GET
GET /resource HTTP/1.1
If-None-Match: "v3-abc123"
HTTP/1.1 304 Not Modified
ETag: "v3-abc123"
Cache-Control: max-age=300
7. Using Cache Warming Strategies
| Strategy | Use |
|---|---|
| Pre-fetch on deploy | Avoid cold start |
| Background refresh | Re-fetch before TTL expires |
| Popular keys list | Top-N analytics-driven |
stale-while-revalidate | Serve stale + async refresh |
8. Implementing Cache Partitioning
| Partition By | Reason |
|---|---|
| Tenant | Isolation, easy purge |
| Region | Data residency |
| Content type | Different eviction policies |
| Cache zone | Hot vs cold tiers |
9. Configuring Cache Size Limits
| Limit | Strategy |
|---|---|
| Max bytes | LRU eviction |
| Max items | Bounded count |
| Per-entry max size | Reject huge responses |
| Eviction policy | LRU, LFU, TinyLFU, ARC |
10. Setting Up Cache Hit Ratio Monitoring
| Metric | Target |
|---|---|
| Hit ratio | > 80% for cacheable |
| Byte hit ratio | > 90% (large objects) |
| Eviction rate | Low & stable |
| Origin offload | % requests not hitting backend |