Minimize latency and load via multi-layer caches, robust population/update patterns, and sound invalidation tactics.
1. Multi-Layer Caching Architecture
Effective scaling uses a hierarchy of caches, each at different proximity to users and services.
| Layer | Purpose | Key Strategy |
|---|---|---|
| CDN (Edge) | Static assets; public read-only content close to end users. | TTL-based expiry; provider cache purge/invalidation. |
| Reverse Proxy / Gateway | Cache API responses before app logic (Nginx, Varnish, API GW). | Honor `Cache-Control`, `ETag`, and conditional requests. |
| Distributed In-Memory | Shared cache cluster (Redis/Memcached) for DB reads, sessions, profiles, computed objects. | Scale reads; manage network latency and memory policies. |
2. Cache Population and Update Patterns
A. Cache-Aside (Lazy Loading)
- Read: Check cache first.
- Hit: Return cached value.
- Miss: Fetch DB → write to cache → return.
- Pros: Cache only what is needed; simple.
- Cons: Miss latency; risk of stampede on popular keys; temporary inconsistency.
B. Write-Through
Synchronously write to cache and DB within the same operation.
- Pros: Cache consistent at write time; simplifies reads.
- Cons: Higher write latency; not ideal for heavy-write workloads.
C. Write-Back (Write-Behind)
Write to cache and acknowledge immediately; cache asynchronously persists to DB.
- Pros: Very fast writes.
- Cons: Risk of data loss on cache crash; requires journaling and recovery.
Strategy Trade-offs
Relative scores (1–5) for Read Latency, Write Latency, Consistency-at-Write, and Operational Complexity.
3. Cache Invalidation Tactics
A. Time-To-Live (TTL)
- Mechanism: Automatic eviction after duration.
- Trade-off: Data can be stale until TTL elapses.
B. Write-Invalidate
- Mechanism: On DB write, explicitly delete affected cache keys.
- Trade-off: Tight coupling; failure to invalidate leads to stale cache until TTL.
C. Change Data Capture (CDC)
- Mechanism: Read transaction logs and publish events to update/invalidate cache.
- Trade-off: Atomic and reliable, but adds tooling and ops complexity.
D. Stale-While-Revalidate
- Mechanism: Serve stale data immediately while refreshing asynchronously.
- Trade-off: Great UX; brief staleness window for some users.