URL shortener with click analytics
Turn long links into short codes that redirect, and count clicks by day, country-free and privacy-friendly.
What you'll learn
- âGenerating unique short codes (Base62)
- âHTTP redirects (302) and caching
- âIndexes and simple analytics queries
What you'll build
- âPOST a long URL, get back a short code
- âGET /{code} redirects
- âCustom alias and expiry date
- âClick count per day
Step-by-step guide
- 1
Design tables
links(id, code UNIQUE, url, created_at, expires_at) and clicks(id, link_id, clicked_at); index clicks(link_id, clicked_at).
- 2
Generate codes
Encode the auto-increment id in Base62 (0-9a-zA-Z) so codes are short and unique; allow an optional custom alias if it is free.
- 3
Validate URLs
Accept only http/https with java.net.URI and reject your own domain to avoid redirect loops.
- 4
Redirect
@GetMapping("/{code}") returns ResponseEntity.status(302).location(uri).build(); 404 or 410 when missing or expired.
- 5
Record clicks
Insert a click row on each redirect (or run it with @Async so the redirect stays fast).
- 6
Analytics endpoint
GET /api/links/{code}/stats returns clicks per day using a GROUP BY date query.
- 7
Cache hot links
Add spring-boot-starter-cache (Caffeine or Redis) with @Cacheable on the code lookup.
đ Make it stand out
- +A small React or Thymeleaf front end
- +QR code for each link (ZXing)
- +Rate limiting per IP