karthik.dev
Back to projects
Infrastructure

URL Shortener with Analytics

Distributed URL shortener with real-time click analytics and sub-10ms redirect latency

Node.jsTypeScriptRedisPostgreSQLDockerREST APIs

Overview

A production-grade URL shortener handling redirects at sub-10ms latency via Redis caching, with asynchronous click analytics providing real-time geographic distribution and referrer breakdown.

Problem

Short URL services need two conflicting optimizations: fast redirects and complete analytics capture — doing both synchronously is not feasible at scale.

Solution

Decoupled architecture: the redirect handler reads from Redis (sub-1ms cache hit) and writes click events to a queue asynchronously. A background worker drains the queue into PostgreSQL.

Architecture

  1. 1Short code → long URL mapping cached in Redis with TTL fallback to PostgreSQL
  2. 2Redirect handler returns 302 in <10ms via Redis hit
  3. 3Click events written to an in-process queue asynchronously
  4. 4Background worker batches queue events into PostgreSQL analytics tables
  5. 5Dashboard API aggregates click data with geographic IP resolution
  6. 6Dockerized for reproducible local and cloud deployment

Technical Challenges

  • Guaranteeing click events are not lost if the server restarts — addressed with Redis Streams.
  • Preventing short code collisions under concurrent creation requests.

Results

  • Redirect latency under 10ms on cache hit, under 50ms on cache miss.
  • Analytics pipeline handles 10k+ clicks/minute without blocking the redirect path.