Initial commit: Test GLM project
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
# Subtask: Caching Layer with Redis
|
||||
|
||||
**Parent Task**: Decomp/001
|
||||
**Subtask ID**: Decomp/001-06
|
||||
**Dependencies**: Decomp/001-01 (Database Schema), Decomp/001-04 (Redirect Endpoint)
|
||||
|
||||
## Description
|
||||
|
||||
Implement Redis-based caching layer to reduce database load and improve redirect latency. Uses LRU eviction with configurable TTL.
|
||||
|
||||
## Requirements
|
||||
|
||||
- Cache redirect lookups (short_code → original_url)
|
||||
- LRU eviction when memory limit reached
|
||||
- 1-hour TTL for cache entries
|
||||
- Automatic cache invalidation on URL updates
|
||||
- Fallback to database on cache miss
|
||||
- Handle Redis failures gracefully
|
||||
|
||||
## Technical Specifications
|
||||
|
||||
**Cache schema**:
|
||||
```
|
||||
Key: redirect:{short_code}
|
||||
Value: {"url": "https://...", "expires_at": "2025-..."}
|
||||
TTL: 3600 seconds (1 hour)
|
||||
|
||||
Key: redirect:exists:{short_code}
|
||||
Value: "1" (for negative caching)
|
||||
TTL: 60 seconds (1 minute)
|
||||
```
|
||||
|
||||
**Cache operations**:
|
||||
```python
|
||||
def get_cached_url(short_code: str) -> Optional[dict]:
|
||||
"""Get URL from cache, fallback to DB."""
|
||||
cached = redis.get(f"redirect:{short_code}")
|
||||
if cached:
|
||||
return json.loads(cached)
|
||||
return None
|
||||
|
||||
def set_cached_url(short_code: str, url_data: dict, ttl: int = 3600):
|
||||
"""Store URL in cache."""
|
||||
redis.setex(f"redirect:{short_code}", ttl, json.dumps(url_data))
|
||||
|
||||
def invalidate_url(short_code: str):
|
||||
"""Remove URL from cache (on update/delete)."""
|
||||
redis.delete(f"redirect:{short_code}")
|
||||
```
|
||||
|
||||
**LRU configuration**:
|
||||
- Max memory: 1GB (configurable)
|
||||
- Eviction policy: `allkeys-lru`
|
||||
- Expected cache size: ~2M entries (500 bytes/entry)
|
||||
|
||||
**Failure handling**:
|
||||
- Redis unavailable: degrade to database lookup
|
||||
- Connection timeout: 100ms, then fallback
|
||||
- Retry strategy: exponential backoff, max 3 retries
|
||||
|
||||
**Performance targets**:
|
||||
- Cache hit: <5ms latency
|
||||
- Cache miss: <50ms latency (DB lookup + cache populate)
|
||||
- Hit rate: >95% for popular URLs (Zipf distribution)
|
||||
|
||||
## Acceptance Criteria
|
||||
|
||||
- [ ] Cache hit returns in <5ms
|
||||
- [ ] Cache miss populates cache for next lookup
|
||||
- [ ] Expired URLs not cached (or cached with negative result)
|
||||
- [ ] Redis failure doesn't break redirects (fallback to DB)
|
||||
- [ ] LRU eviction respects memory limit
|
||||
- [ ] Cache invalidation works on URL updates
|
||||
- [ ] Negative caching prevents DB hammering for invalid codes
|
||||
|
||||
## Notes
|
||||
|
||||
**Trade-offs**:
|
||||
- 1-hour TTL: balances freshness vs cache hit rate
|
||||
- Negative caching: prevents DB spam for invalid codes
|
||||
- LRU over LFU: simpler, good enough for temporal locality
|
||||
|
||||
**Edge cases**:
|
||||
- URL updated while cached: eventual consistency (1-hour window)
|
||||
- Redis restart: cache warm-up required (can pre-load top URLs)
|
||||
- Memory pressure: LRU eviction may drop popular but recently unused URLs
|
||||
|
||||
**Alternatives considered**:
|
||||
- Write-through cache: rejected, slower writes
|
||||
- Write-behind cache: rejected, risk of data loss
|
||||
- No caching: rejected, cannot meet <50ms p95 latency at scale
|
||||
Reference in New Issue
Block a user