Initial commit: Test GLM project
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
# Subtask: HTTP Redirect Endpoint
|
||||
|
||||
**Parent Task**: Decomp/001
|
||||
**Subtask ID**: Decomp/001-04
|
||||
**Dependencies**: Decomp/001-01 (Database Schema), Decomp/001-03 (Short Code Generation)
|
||||
|
||||
## Description
|
||||
|
||||
Implement HTTP redirect endpoint that resolves short codes to original URLs. Must support both 301 (permanent) and 302 (temporary) redirects with <50ms p95 latency.
|
||||
|
||||
## Requirements
|
||||
|
||||
- Resolve short code to original URL
|
||||
- Return HTTP 301 for permanent redirects (default)
|
||||
- Return HTTP 302 for temporary redirects (configurable)
|
||||
- Handle expired URLs (404 or custom error page)
|
||||
- Support query parameter forwarding
|
||||
- Log all redirect attempts for analytics
|
||||
|
||||
## Technical Specifications
|
||||
|
||||
**API endpoint**: `GET /{short_code}`
|
||||
|
||||
**Response flow**:
|
||||
```python
|
||||
def handle_redirect(short_code: str, temporary: bool = False) -> Response:
|
||||
# 1. Lookup URL in database
|
||||
url_record = db.query(
|
||||
"SELECT original_url, expires_at FROM urls WHERE short_code = %s",
|
||||
short_code
|
||||
)
|
||||
|
||||
# 2. Handle not found
|
||||
if not url_record:
|
||||
return Response("URL not found", status=404)
|
||||
|
||||
# 3. Check expiration
|
||||
if url_record.expires_at and url_record.expires_at < now():
|
||||
return Response("URL expired", status=404)
|
||||
|
||||
# 4. Log for analytics (async, non-blocking)
|
||||
async_log_click(short_code, request)
|
||||
|
||||
# 5. Redirect
|
||||
status = 302 if temporary else 301
|
||||
return Response(status=status, headers={"Location": url_record.original_url})
|
||||
```
|
||||
|
||||
**Caching strategy**:
|
||||
- Redis LRU cache with 1-hour TTL
|
||||
- Cache key: `redirect:{short_code}`
|
||||
- Cache miss → DB lookup → cache populate
|
||||
- Cache hit: <5ms response time
|
||||
|
||||
**Performance requirements**:
|
||||
- p50 latency: <10ms
|
||||
- p95 latency: <50ms
|
||||
- p99 latency: <100ms
|
||||
|
||||
**Error handling**:
|
||||
- 404: short code not found or expired
|
||||
- 429: rate limit exceeded (client-side)
|
||||
- 500: database errors (with monitoring alert)
|
||||
|
||||
## Acceptance Criteria
|
||||
|
||||
- [ ] Valid short code returns 301/302 to original URL
|
||||
- [ ] Invalid short code returns 404
|
||||
- [ ] Expired URL returns 404
|
||||
- [ ] p95 latency <50ms under 100K req/sec load
|
||||
- [ ] Cache hit rate >95% for popular URLs
|
||||
- [ ] Analytics logging doesn't block redirect response
|
||||
- [ ] Query parameters preserved in redirect (optional)
|
||||
|
||||
## Notes
|
||||
|
||||
**Trade-offs**:
|
||||
- 301 vs 302: 301 for SEO (permanent), 302 for tracking flexibility
|
||||
- Async logging: eventual consistency acceptable for analytics
|
||||
- Redis cache: added complexity but 10x latency improvement
|
||||
|
||||
**Edge cases**:
|
||||
- Redirect loop: detect and break (max 5 hops)
|
||||
- Very long original URLs: no truncation, preserve as-is
|
||||
- Concurrent redirects: handle race conditions in cache population
|
||||
|
||||
**Alternatives considered**:
|
||||
- Synchronous logging: rejected, blocks redirect response
|
||||
- Edge redirects (CDN): rejected, adds external dependency
|
||||
- 308 redirect: rejected, less browser support than 301
|
||||
Reference in New Issue
Block a user