Projects › Intermediate
URL Shortener with Analytics
Turn long links into short ones, redirect fast and show click analytics — a favourite system-design interview topic.
Level: Intermediate · Time: 1 week · Tech stack: Node.js, Express, MongoDB, React
Features to build
- Create short links (random or custom alias)
- Fast 301/302 redirects
- Click count, last clicked, top referrers and devices
- Expiry dates and QR codes
- Rate limiting and URL validation
What you will learn
- REST API design
- MongoDB indexes
- Unique ID generation (base62)
- Caching and rate limiting
Step-by-step guide
1. Model
const linkSchema = new Schema({
code: { type: String, unique: true }, // "aB3x9Q"
url: { type: String, required: true },
clicks: { type: Number, default: 0 },
expiresAt: Date,
owner: { type: Schema.Types.ObjectId, ref: 'User' },
}, { timestamps: true });
The unique index on code makes lookups O(log n) and prevents duplicates.
2. Generate the code
Random base62 ([0-9a-zA-Z], 7 characters ≈ 3.5 trillion combinations). On a duplicate-key error (11000), just try again.
3. Endpoints
POST /api/links→ validate the URL withnew URL(url), only allow http/https, create the code.GET /:code→ find, check expiry,$incclicks, redirect.GET /api/links/:code/stats→ analytics for the owner.
4. Analytics without slowing redirects
Redirect first, then log the click (IP country, user-agent, referrer) in a separate clicks collection without awaiting it.
5. Speed with a cache
Put code → url in an in-memory LRU map (or Redis). Most traffic hits a few popular links.
6. Abuse protection
Rate-limit link creation per IP and block known malicious domains.
7. Frontend
A React page: input, "Shorten" button, copy-to-clipboard, a table of your links with a small clicks chart.
Level it up
- Redis cache + Docker Compose
- Custom domains
- Bulk shortening via CSV
On your resume
- Built a URL shortener (Node, Express, MongoDB, React) with base62 codes, click analytics, link expiry, LRU caching and rate limiting.