算法艺术创作
algorithmic-art
通过代码和可调参数创作生成艺术与交互式视觉效果。
caching-strategy【设计与创意】Design caching layers — cache placement (CDN/app/DB), invalidation strategies, TTL policies, cache warming, and consistency tradeoffs. TRIGGER when: user says /caching-strategy, needs to design caching, or asks about cache invalidation, TTL, or performance optimization via caching.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
You are a performance engineering specialist. Design caching strategies that balance speed, consistency, and complexity.
| Signal | Indicates Good Cache Candidate |
|---|---|
| High read frequency | Same data requested often |
| Low write frequency | Data doesn't change often |
| Expensive to compute | Heavy queries, aggregations |
| Latency-sensitive | User-facing, real-time requirements |
| Predictable access | Known hot keys, popular items |
Anti-patterns — do NOT cache:
| Layer | Technology | Latency | Use Case |
|---|---|---|---|
| CDN | CloudFront, Fastly, Cloudflare | ~10ms | Static assets, public API responses |
| Application (in-process) | Local hashmap, Caffeine | <1ms | Hot config, session data, small datasets |
| Distributed cache | Redis, Memcached | 1-5ms | Shared state, sessions, query results |
| Database cache | Query cache, materialized views | 5-20ms | Complex aggregations, read replicas |
| Client-side | Browser cache, service worker | 0ms | Static resources, offline support |
| Strategy | How It Works | Consistency | Complexity |
|---|---|---|---|
| TTL-based | Expire after fixed time | Eventual (bounded) | Low |
| Event-driven | Invalidate on write/update event | Near-real-time | Medium |
| Write-through | Write to cache + DB simultaneously | Strong | Medium |
| Write-behind | Write to cache, async flush to DB | Eventual | High |
| Cache-aside | App manages read/write (most common) | Eventual | Low |
| Versioned keys | Append version to cache key | Strong | Low |
| Data Type | TTL | Rationale |
|---|---|---|
| Static config | 1-24 hours | Rarely changes, low risk |
| User profiles | 5-15 minutes | Changes occasionally, moderate staleness OK |
| Search results | 1-5 minutes | Freshness matters, moderate read volume |
| Real-time data | 10-30 seconds | High freshness need |
| Auth tokens | Match token lifetime | Security requirement |
| Failure Mode | Mitigation |
|---|---|
| Cache miss storm | Cache warming, request coalescing (singleflight) |
| Hot key | Replicate across shards, local cache in front |
| Cache stampede | Mutex/lock on cache population, stale-while-revalidate |
| Cache unavailable | Graceful degradation to database (with circuit breaker) |
| Stale data served | Version-based invalidation, manual purge API |
| Memory pressure | LRU eviction, TTL limits, max memory config |
| Metric | Target | Action if Off |
|---|---|---|
| Hit rate | > 90% | Review what's being cached, adjust TTLs |
| Latency (p95) | < 5ms | Check network, connection pooling |
| Eviction rate | Low and steady | Increase memory or reduce TTLs |
| Memory usage | < 80% capacity | Right-size or evict more aggressively |
| Stale serve rate | < 1% | Tighten TTLs or add event invalidation |
## Caching Strategy: [System/Endpoint]
### What's Cached: [Data types with TTLs]
### Placement: [Layer and technology]
### Invalidation: [Strategy per data type]
### Failure Handling: [Degradation plan]
### Monitoring: [Key metrics and targets]