URL Shortener with Analytics
Distributed URL shortener with real-time click analytics and geographic tracking
Overview
A production-grade URL shortener that handles redirect at sub-10ms latency via Redis caching, captures click events asynchronously for real-time analytics, and provides a dashboard showing geographic distribution, referrer breakdown, and click-over-time charts.
Problem
Short URL services require two conflicting optimization goals: the redirect path must be as fast as possible (users notice latency), while analytics capture must be complete and accurate (business requirement) — doing both synchronously in the redirect handler is not feasible at scale.
Solution
A decoupled architecture: the redirect handler reads from Redis (sub-1ms cache hit) and writes a click event to a queue asynchronously. A background worker drains the queue into PostgreSQL. Analytics queries run against the DB, never blocking redirects.
Architecture
- 1Short code → long URL mapping cached in Redis with TTL fallback to PostgreSQL
- 2Redirect handler returns 302 in <10ms via Redis hit
- 3Click events (IP, user agent, referrer, timestamp) written to an in-process queue
- 4Background worker batches queue events into PostgreSQL analytics tables
- 5Dashboard API aggregates click data with geographic IP resolution
- 6Dockerized for reproducible local and cloud deployment
Technical Challenges
- Guaranteeing click events are not lost if the server restarts before the queue is flushed — addressed with a persistent queue backed by Redis Streams.
- Preventing short code collisions under concurrent creation requests using database-level unique constraints and retry logic.
Results
- Redirect latency under 10ms on cache hit, under 50ms on cache miss.
- Analytics pipeline handles 10k+ clicks/minute without blocking the redirect path.