Backend & APIs · Intermediate
URL Shortener With Click Analytics
Build a link shortener that survives traffic spikes, with cached redirects, custom slugs, and a live per-link click dashboard.
You will build a production-grade URL shortener that generates short codes, stores them in Postgres, and serves redirects from a Redis cache so even viral links respond in under 10ms. A React dashboard reads aggregated click events from the database and shows per-link traffic in real time. This project teaches the full lifecycle of a write-once, read-many API service: data modeling, cache-aside invalidation, and lightweight time-series analytics without a dedicated analytics engine.
What you build
- Create short links with an auto-generated 7-character base-62 code or a user-chosen custom slug
- Redirect requests via a Redis cache-aside layer with automatic cache population on miss
- Record every click with timestamp, referrer, country, and user-agent to a Postgres events table
- Expose a REST API for link creation, deletion, and stats retrieval
- React dashboard shows per-link click totals, a time-series chart, and top referrers
- Rate-limit link creation per IP to prevent abuse
- Return 404 with a branded page when a slug is unknown or has been deleted
What it teaches
- Cache-aside pattern with Redis: when to read from cache, how to handle misses, and how to set TTLs that balance freshness with load reduction
- Postgres schema design for a write-heavy click events table, including indexing on slug and clicked_at for efficient aggregation queries
- Base-62 encoding for generating compact, URL-safe identifiers from sequential integers
- Handling high read-to-write ratios in API design by separating the hot redirect path from the analytics write path
- Rate limiting at the API layer using a sliding-window counter stored in Redis
- Deploying a stateless API server with an external data layer to an edge or container platform
How it works
- 1
Client
- GET /abc123
- 2
API Server
- Check Redis for slug
↓ Cache hit: instant redirect
- 3
Redis Cache
- Return destination URL or MISS
- 4
Postgres
- Fetch slug on MISS
- Write click event row
- 5
Response
- 301 redirect to destination
Sign in to open the build guide
Free account. Get the step-by-step build and every resource link.
Take it further
- Add QR code generation for each short link using a server-side library so users can download a scannable image.
- Implement link expiry: accept an optional expires_at timestamp at creation time, reject redirects past that date, and run a nightly cron job to evict expired slugs from Redis.
- Support UTM parameter passthrough so the shortener appends utm_source and utm_campaign to the destination URL based on per-link config.


