🚀 A production-grade URL shortener and async analytics platform built with Java 25, Spring Boot 4.1.1, MySQL 9.x, Redis, and RabbitMQ.
-
➡️ HTTP 302 Found Redirection: Replaced permanent 301 redirects with temporary HTTP 302 redirects (
no-cacheheaders) so client browsers never cache redirects locally, ensuring 100% of click events hit your server for live analytics. -
⚡ Two-Tier (L1/L2) Hierarchical Caching:
-
L1 (Caffeine Local RAM): Sub-millisecond lookup (
$\approx 10\text{--}50,\mu\text{s}$ ) for ultra-hot links. -
L2 (Redis Distributed Cache):
$1\text{--}3,\text{ms}$ network cache with dynamic TTL matching link expiration. -
L3 (MySQL Persistent Store): Indexed lookup on
short_code. - Cache Penetration Protection: Negative sentinel caching (60s TTL) for probing attacks on nonexistent URLs.
- Thundering Herd Mitigation: Striped locking per key serializes concurrent database lookups.
- Redis Pub/Sub Invalidation: Cluster-wide cache eviction across instances when links are updated.
-
L1 (Caffeine Local RAM): Sub-millisecond lookup (
-
📨 Asynchronous Analytics Pipeline (RabbitMQ):
-
Non-Blocking Redirects: The redirect path extracts
User-Agent,Referer, and client IP headers and pushes a message to RabbitMQ in$< 5\text{ms}$ . - Broker Outage Fallback: In the event of a RabbitMQ outage, events are buffered in an in-memory bounded circular queue without failing the redirect.
-
Dead-Letter Queue (DLQ): Messages that fail processing are routed to
click.events.dlq. -
Background Consumer:
@RabbitListenerworker parses user agents, filters web crawlers/bots (Googlebot,Bingbot,Slackbot, etc.), resolves geographic location, and writes micro-batches (50 records or 500ms) to MySQL.
-
Non-Blocking Redirects: The redirect path extracts
-
🎲 Pluggable Base62 Key Generation:
-
Counter-Based Range Allocator: Thread-safe
AtomicLongmemory range allocator converted to 6-char Base62 using zero-dependency arithmetic. -
Randomized Key Generator: Cryptographic
SecureRandomBase62 generator with collision retries. -
Custom Vanity Slugs: Supports custom aliases with reserved route protection (
/api,/admin,/r,/metrics,/actuator).
-
Counter-Based Range Allocator: Thread-safe
-
🛡️ SSRF Defense & Security:
- Rejects local loopbacks (
127.0.0.1,::1,localhost). - Rejects private RFC 1918 subnets (
10.0.0.0/8,172.16.0.0/12,192.168.0.0/16). - Rejects cloud instance metadata service (
169.254.169.254).
- Rejects local loopbacks (
-
📊 Live Analytical Reporting REST APIs:
- Total clicks, unique IP visitors, bot traffic ratio.
- Top 5 referrers with SQL aggregations and Java Stream percentage rankings.
- Top 5 countries and cities.
- Device breakdown (Mobile, Desktop, Tablet, Bot).
-
🧪 83 Passing Automated Tests:
- Full JUnit 5 & Mockito test suite covering boundaries, SSRF, service layers, cache behavior, broker fallback resilience, and MockMvc endpoints.
- Runtime: Java 25 (LTS)
- Framework: Spring Boot 4.1.1 (Spring Framework 7.x, Spring Data JPA, Spring AMQP, Spring Data Redis, Spring Cache)
- Database: MySQL 9.x (
com.mysql:mysql-connector-j) with versioned Flyway migrations (flyway-mysql) - Caches: Caffeine 3.x (L1) + Redis 7.x (L2)
- Message Broker: RabbitMQ 3.13+ (Topic Exchange + DLQ)
- Testing: JUnit 5, Mockito, AssertJ, MockMvc, H2 (test scope)
- Build Tool: Apache Maven 3.9.16 via
.\mvnw.cmd
| Method | Endpoint | Description | Status Code |
|---|---|---|---|
GET |
/r/{shortCode} |
Resolves link and redirects client | 302 Found (Location header) |
| Method | Endpoint | Request Body | Description |
|---|---|---|---|
POST |
/api/v1/links |
{"originalUrl":"...", "customSlug":"...", "expiresInDays":7} |
Creates short link |
GET |
/api/v1/links |
None | Lists all links |
GET |
/api/v1/links/{shortCode} |
None | Gets short link details |
PATCH |
/api/v1/links/{shortCode}/deactivate |
None | Deactivates short link and clears cache |
PUT |
/api/v1/links/{shortCode}/destination |
{"originalUrl":"..."} |
Updates target URL and evicts caches |
POST |
/api/shorten |
{"url":"...", "alias":"..."} |
Legacy backward-compatible endpoint |
| Method | Endpoint | Description |
|---|---|---|
GET |
/api/v1/analytics/links/{shortCode}/summary |
Total clicks, bot count, unique visitors, bot % |
GET |
/api/v1/analytics/links/{shortCode}/referrers?limit=5 |
Top referrers with click counts and percentages |
GET |
/api/v1/analytics/links/{shortCode}/countries?limit=5 |
Top countries with click counts and percentages |
GET |
/api/v1/analytics/links/{shortCode}/devices |
Breakdown by device type (Mobile/Desktop/Tablet) |
.\mvnw.cmd testdocker compose up -d redis rabbitmqMake sure local MySQL service MySQL93 is running on port 3306:
.\mvnw.cmd spring-boot:runApplication starts on http://localhost:8080.
RabbitMQ Management UI is at http://localhost:15672 (guest / guest).