🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101

URL shortener with click analytics

Turn long links into short codes that redirect, and count clicks by day, country-free and privacy-friendly.

Intermediate⏱ 8–12 hoursđŸĒœ 7 stepsSpring BootPostgreSQL/MySQLRedis (optional)

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. 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. 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. 3

    Validate URLs

    Accept only http/https with java.net.URI and reject your own domain to avoid redirect loops.

  4. 4

    Redirect

    @GetMapping("/{code}") returns ResponseEntity.status(302).location(uri).build(); 404 or 410 when missing or expired.

  5. 5

    Record clicks

    Insert a click row on each redirect (or run it with @Async so the redirect stays fast).

  6. 6

    Analytics endpoint

    GET /api/links/{code}/stats returns clicks per day using a GROUP BY date query.

  7. 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