skills:cache
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| skills:cache [2026/05/26 06:49] – created phong2018 | skills:cache [2026/05/26 06:58] (current) – [Sequence Diagram] phong2018 | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| ===== Overview ===== | ===== Overview ===== | ||
| - | Cache invalidation | + | Cache invalidation |
| - | In real production systems, the main challenge | + | In real production systems, the hardest problem |
| - | Ensuring consistency between cache and database at scale in distributed | + | Handling concurrency |
| - | Most real systems use multiple | + | Most systems use a combination of patterns, not a single approach. |
| ===== 1. Cache-Aside (Most Common Pattern) ===== | ===== 1. Cache-Aside (Most Common Pattern) ===== | ||
| Line 15: | Line 15: | ||
| ==== Concept ==== | ==== Concept ==== | ||
| - | Application manages cache manually. | + | Application manages cache manually. Cache is a read-through |
| - | + | ||
| - | Cache is treated as a read-through layer, | + | |
| ==== Read Flow ==== | ==== Read Flow ==== | ||
| Line 26: | Line 24: | ||
| < | < | ||
| - | |||
| - | ==== Key Idea ==== | ||
| - | |||
| - | Cache is disposable. If wrong → just delete it. | ||
| ==== Where it is used ==== | ==== Where it is used ==== | ||
| - | E-commerce | + | Microservices systems |
| - | Social | + | E-commerce |
| - | Microservices architectures | + | Social |
| - | Most production APIs | + | |
| ==== Pros ==== | ==== Pros ==== | ||
| Line 43: | Line 36: | ||
| Safe | Safe | ||
| Easy to debug | Easy to debug | ||
| - | Works well in distributed systems | + | Flexible |
| ==== Cons ==== | ==== Cons ==== | ||
| - | First read after cache miss is slow | + | Cache miss causes DB hit |
| - | Possible | + | Small stale window |
| ===== 2. Write-Through Cache ===== | ===== 2. Write-Through Cache ===== | ||
| Line 54: | Line 47: | ||
| ==== Concept ==== | ==== Concept ==== | ||
| - | Every write goes through cache first, then DB. | + | Write goes through cache first, then DB. |
| ==== Flow ==== | ==== Flow ==== | ||
| < | < | ||
| - | |||
| - | ==== Where it is used ==== | ||
| - | |||
| - | Strong consistency systems | ||
| - | Limited financial systems | ||
| - | Some controlled caching layers | ||
| ==== Pros ==== | ==== Pros ==== | ||
| - | Cache always consistent | + | Strong consistency |
| - | No stale data | + | No stale cache |
| ==== Cons ==== | ==== Cons ==== | ||
| Slower writes | Slower writes | ||
| - | Higher | + | Higher cost |
| - | ===== 3. Write-Behind | + | ===== 3. Write-Behind Cache ===== |
| ==== Concept ==== | ==== Concept ==== | ||
| - | Write goes to cache first, DB update happens | + | Write goes to cache first, DB updated |
| ==== Flow ==== | ==== Flow ==== | ||
| - | < | + | < |
| - | + | ||
| - | ==== Where it is used ==== | + | |
| - | + | ||
| - | Gaming leaderboards | + | |
| - | Analytics systems | + | |
| - | High-throughput systems | + | |
| ==== Pros ==== | ==== Pros ==== | ||
| Very fast writes | Very fast writes | ||
| - | High performance | + | High throughput |
| ==== Cons ==== | ==== Cons ==== | ||
| Risk of data loss | Risk of data loss | ||
| - | Complex recovery | + | Complex recovery |
| ===== 4. TTL-Based Invalidation ===== | ===== 4. TTL-Based Invalidation ===== | ||
| Line 106: | Line 87: | ||
| ==== Concept ==== | ==== Concept ==== | ||
| - | Cache automatically | + | Cache expires |
| ==== Example ==== | ==== Example ==== | ||
| - | < | + | < |
| - | + | ||
| - | ==== Where it is used ==== | + | |
| - | + | ||
| - | Everywhere as fallback safety | + | |
| - | Session caching | + | |
| - | API response caching | + | |
| ==== Pros ==== | ==== Pros ==== | ||
| - | Very simple | + | Simple |
| - | No coordination needed | + | Safe fallback |
| ==== Cons ==== | ==== Cons ==== | ||
| - | Data may be stale until expiration | + | Stale data until expiration |
| ===== 5. Event-Driven Invalidation ===== | ===== 5. Event-Driven Invalidation ===== | ||
| Line 131: | Line 106: | ||
| ==== Concept ==== | ==== Concept ==== | ||
| - | When database | + | Database |
| ==== Flow ==== | ==== Flow ==== | ||
| - | < | + | < |
| - | + | ||
| - | ==== Technologies ==== | + | |
| - | + | ||
| - | Kafka | + | |
| - | RabbitMQ | + | |
| - | AWS SNS/SQS | + | |
| - | Redis Pub/Sub | + | |
| - | + | ||
| - | ==== Where it is used ==== | + | |
| - | + | ||
| - | Uber-like systems | + | |
| - | Airbnb-like systems | + | |
| - | Large-scale microservices | + | |
| ==== Pros ==== | ==== Pros ==== | ||
| - | Decoupled | + | Decoupled |
| - | Scales well | + | Scalable |
| - | Works across services | + | |
| ==== Cons ==== | ==== Cons ==== | ||
| - | Eventual consistency | + | Eventual consistency |
| - | Possible event delay or loss | + | Event delay or loss possible |
| - | ==== Production fixes ==== | + | ===== 6. Update-on-write vs Invalidate-on-write ===== |
| - | Retry mechanisms | + | ==== 🟥 6.1 Update-on-write |
| - | Idempotent consumers | + | |
| - | Dead Letter Queue (DLQ) | + | |
| - | Event versioning | + | |
| - | ===== 6. Hybrid Pattern (Real Production Standard) | + | ===== Problem |
| - | ==== Concept ==== | + | Concurrent updates can cause race conditions between DB and cache. |
| - | Real systems combine multiple patterns together. | + | ===== Scenario ===== |
| - | ==== Typical architecture ==== | + | Request A updates value = " |
| + | Request B updates value = " | ||
| - | < | ||
| - | Read Path: | + | ===== Problem ===== |
| - | Client → Service → Cache | + | |
| - | ↓ miss | + | |
| - | Database → Cache | + | |
| - | Safety Net: | + | If execution order is mixed: |
| - | TTL expiration always enabled | + | |
| - | </ | + | |
| - | ==== Why hybrid is used ==== | + | DB = B |
| + | Cache = A ❌ | ||
| - | Each pattern solves a different problem: | + | ===== Conclusion ===== |
| - | Cache-aside → simplicity | + | Update-on-write is unsafe due to: |
| - | Event-driven → consistency | + | |
| - | TTL → safety fallback | + | |
| - | ===== 7. Outbox Pattern (Reliability Layer) ===== | + | Race conditions |
| + | Out-of-order execution | ||
| + | Distributed timing issues | ||
| - | ==== Concept | + | ==== 🟩 6.2 Invalidate-on-write (SAFE pattern) |
| - | Ensures database updates and events are consistent. | + | ===== Concept ===== |
| - | ==== Flow ==== | + | Do NOT update cache. Only delete it. |
| - | < | ||
| - | Worker → reads outbox → publishes event | + | ===== Why it is safe ===== |
| - | </ | + | |
| - | ==== Why it is used ==== | + | Only DELETE operations on cache |
| + | No stale value can be written | ||
| - | Prevents lost events | + | ===== Read Flow ===== |
| - | Ensures reliable event delivery | + | |
| - | ==== Where it is used ==== | + | < |
| - | Large microservices systems | + | Always consistent with DB. |
| - | Financial systems | + | |
| - | E-commerce platforms | + | |
| - | ===== 8. Common Production Problems | + | ===== 7. Comparison Table ===== |
| - | ==== 1. Cache Stampede ==== | + | ^ Aspect ^ Update-on-write ^ Invalidate-on-write ^ |
| + | | Cache operation | SET | DELETE | | ||
| + | | Race condition risk | ❌ High | ✅ Low | | ||
| + | | Out-of-order updates | ❌ Dangerous | ❌ Harmless | | ||
| + | | Cache correctness | ❌ Fragile | ✅ Reliable | | ||
| + | | Debug complexity | High | Low | | ||
| - | Many requests miss cache at the same time. | + | ===== 8. Key Insight ===== |
| - | Fix: | + | Cache is not the source of truth. |
| - | Mutex lock | + | DB = source of truth |
| - | Single-flight | + | Cache = disposable optimization layer |
| - | Request coalescing | + | |
| - | ==== 2. Race Condition (Stale Cache Overwrite) ==== | + | Invalidate-on-write works because: |
| - | Old data overwrites new cache value. | + | Even if cache is wrong, it will be deleted anyway. |
| - | Fix: | + | ===== 9. Production Pattern ===== |
| - | Versioning | + | Most systems use: |
| - | Timestamp checks | + | |
| - | Event ordering | + | |
| - | ==== 3. Event Loss ==== | + | < |
| - | Fix: | + | READ: |
| + | Cache hit → return | ||
| + | Cache miss → DB → set cache | ||
| + | </ | ||
| - | Kafka persistence | + | ===== 10. Why Update-on-write still exists ===== |
| - | Retry + DLQ | + | |
| - | Outbox pattern | + | |
| - | ===== 9. Summary ===== | + | Used only in limited cases: |
| - | In real production systems: | + | Simple data |
| + | Low concurrency | ||
| + | Ultra-fast read-after-write requirement | ||
| - | Cache-aside is the base | + | Examples: |
| - | Event-driven invalidation handles updates | + | |
| - | TTL provides safety net | + | |
| - | Outbox ensures reliability | + | |
| - | ==== Final Mental Model ==== | + | Session storage |
| + | Feature flags | ||
| + | Simple counters | ||
| - | < | + | ===== 11. Bonus Topics ===== |
| + | |||
| + | Cache stampede problem | ||
| + | Mutex / singleflight | ||
| + | Request coalescing | ||
| + | Kafka-based cache invalidation | ||
| + | Outbox pattern | ||
skills/cache.1779778156.txt.gz · Last modified: by phong2018
