Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

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

What you will learn

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

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

On your resume

Learn on Codify