LLD Guide · Module 2: Problem prep · Lesson 02.11
Prep: URL Shortener
Short codes that can never collide, clicks that are never lost, and long or Unicode URLs that come back byte for byte.
By Cruscible Engineering · Published
Two questions decide this problem: how you know a code has never been used, and how a click is counted when a thousand happen at once.
This lesson prepares you for Build a URL Shortener. It doesn't solve it.
1. What the URL shortener contract asks for
shorten stores a URL and returns a new short code. resolve returns the URL and counts a click. Every call to shorten creates a new mapping, even for a URL you've seen before. All data is persisted (Postgres is recommended).
| Method | Returns / does |
|---|---|
shorten(longUrl) | a new code of 6 to 8 characters from a–z, A–Z, 0–9; throws IllegalArgumentException for null |
resolve(code) | the original URL, and the click count goes up by one; null if unknown |
delete(code) | true if the mapping existed, false otherwise |
getClickCount(code) | resolves so far; 0 for an unknown or deleted code |
getStats(code) | shortCode, longUrl, clickCount, createdAt (epoch ms); an empty map if unknown |
2. Lessons to read before the URL shortener
- 01.04 Postgres for LLD: constraints that make a duplicate impossible, and updates that count without losing any.
- 01.02 Thread-safety: many threads shorten and resolve at once.
3. URL shortener design decisions to make first
- How are codes generated? The brief names three ways: a counter in Base62, random codes retried on a collision, or a truncated hash. For yours, is a duplicate impossible, or only detected? And who detects it?
- How does every code end up 6 to 8 characters? A counter's first values encode to one or two characters. A random code needs a fixed length.
- How is a click counted? Reading the count and writing it back in a second step loses clicks under load. Can
resolvefind the URL and count the click in one statement? - Which column types hold the URL exactly? URLs of 2,048 characters, Unicode and special characters must come back unchanged.
- What does
deleteleave behind? After a delete,resolvegivesnull, the count is0and the stats are empty. Is the row gone, or flagged? - What does an empty URL do? The brief leaves it to you: reject it with
IllegalArgumentException, or store it and give""back.
4. URL shortener pitfalls
⚠️ Trap: don't look up an existing code for a URL you've already shortened. The same URL shortened twice gets two codes and two separate counters.
⚠️ Trap: a
VARCHAR(255)column fails or truncates a 2,048-character URL.
⚠️ Trap: checking that a random code is free, then inserting it, is a race. Two threads can pick the same free code in between.
⚠️ Trap: the performance target is 5,000
shortencalls a second at a p99 of 10 ms. Every extra round trip per call counts.
💡 Tip:
createdAtis epoch milliseconds as a number, not a timestamp object.
Before you submit
- Codes are 6 to 8 characters, letters and digits only
- Codes are unique, including under concurrent
shortencalls - The same URL shortened twice gets two different codes
shorten(null)throwsIllegalArgumentException- Long (2,000+ characters), Unicode and special-character URLs resolve back exactly
resolvecounts every click, even under concurrent calls- Unknown codes give
null,0and an empty map - After
delete, the code resolves tonull, its count is0, and a seconddeletereturnsfalse getStatshasshortCode,longUrl,clickCountandcreatedAt- All data is persisted
Further reading
INSERT … ON CONFLICT(PostgreSQL docs)- Sequence functions (PostgreSQL docs)
- Character types (PostgreSQL docs)
After you've tried it
Submit first. The full walkthrough of one solution: Building a URL Shortener.