Implementing Caching Strategies
1. Using Client-Side Caching
| Header | Use |
|---|---|
| Cache-Control: max-age | Fresh duration |
| Cache-Control: stale-while-revalidate | Serve stale + refresh in bg |
| ETag / If-None-Match | Conditional GET → 304 |
| Vary | Cache key per header |
| private vs public | Per-user vs shared |
2. Implementing Server-Side Caching
| Layer | Detail |
|---|---|
| In-process | Caffeine (Java), Guava, lru-cache |
| Distributed | Redis, Memcached |
| Reverse proxy | Varnish, Nginx, Cloudflare |
| Hybrid | L1 local + L2 distributed |
3. Using Distributed Caching
| Engine | Strengths |
|---|---|
| Redis | Data structures, pub/sub, persistence |
| Memcached | Simple, fast, multi-threaded |
| Hazelcast / Ignite | Embedded distributed cache |
| Cluster mode | Sharded keys; HA via replicas |
4. Implementing Cache-Aside Pattern
| Step | Action |
|---|---|
| 1 | App reads cache |
| 2 | Miss → read DB |
| 3 | Populate cache |
| Pros | Simple; cache only what's used |
| Cons | First-read latency; stale risk |
5. Implementing Read-Through Pattern
| Aspect | Detail |
|---|---|
| Mechanism | App reads cache only; cache fetches from DB on miss |
| Library | Caffeine LoadingCache, Spring @Cacheable |
| Pros | Encapsulates load logic |
| Cons | Cache becomes critical path |
6. Implementing Write-Through Pattern
| Aspect | Detail |
|---|---|
| Mechanism | Write to cache + DB synchronously |
| Pros | Cache always fresh |
| Cons | Higher write latency |
7. Implementing Write-Behind Pattern
| Aspect | Detail |
|---|---|
| Mechanism | Write to cache; flush to DB async |
| Pros | High write throughput |
| Cons | Risk of data loss on cache crash |
| Use | High-rate counters, metrics |
8. Managing Cache Invalidation
| Strategy | Detail |
|---|---|
| TTL | Simple, eventual |
| Explicit invalidate | Delete on write |
| Event-driven | Subscribe to domain events |
| Versioned keys | user:42:v3 bumps on change |
| Surrogate keys | Tag-based purges (Varnish, Fastly) |
9. Using Cache Keys
| Practice | Detail |
|---|---|
| Namespace | service:entity:id |
| Versioning | Bump prefix on schema change |
| Hashing | For long composite keys |
| Locale/tenant | Include in key to avoid leakage |
10. Implementing Cache Warming
| Trigger | Detail |
|---|---|
| Startup | Pre-load top-N entries |
| Scheduled | Refresh hot keys ahead of TTL |
| Predictive | Based on access patterns |
| Use | Avoid cold-cache stampedes after deploy |
11. Handling Cache Stampede
| Defense | Detail |
|---|---|
| Mutex (single-flight) | One loader per key; others wait |
| Probabilistic early refresh | Refresh near expiry to avoid herd |
| Stale-while-revalidate | Serve old + refresh async |
| Jittered TTL | Avoid synchronized expiry |
12. Implementing HTTP Caching Headers
| Header | Effect |
|---|---|
| Cache-Control: no-store | Never cache |
| Cache-Control: no-cache | Revalidate every time |
| max-age=N | Fresh for N seconds |
| s-maxage=N | Override for shared caches |
| immutable | Versioned static assets |
| Age | Seconds since cache stored |