How sujaydevx/url-shortner works
Purpose and Runtime
This is a Java/Gradle URL-shortening service, built as a Spring Boot application. It creates shortened URLs, resolves short codes into redirects, tracks URL usage, supports expiration, and exposes operational health/metrics. The process starts at src/main/java/com/sujay/urlshortener/UrlShortenerApplication.java; HTTP entry points are in src/main/java/com/sujay/urlshortener/controller/UrlController.java and src/main/java/com/sujay/urlshortener/controller/HomeController.java.
HTTP API and Boundary Layer
Controllers accept URL-shortening requests and redirect/statistics-facing traffic, using request/response DTOs to isolate the HTTP contract from domain persistence: src/main/java/com/sujay/urlshortener/dto/request/ShortenUrlRequest.java, src/main/java/com/sujay/urlshortener/dto/response/ShortenUrlResponse.java, and src/main/java/com/sujay/urlshortener/dto/response/UrlStatsResponse.java. Cross-origin behavior and API documentation configuration are centralized in src/main/java/com/sujay/urlshortener/Config/CorsConfig.java and src/main/java/com/sujay/urlshortener/Config/OpenApiConfig.java.
Core URL Lifecycle
UrlService is the principal business boundary: src/main/java/com/sujay/urlshortener/service/UrlService.java and src/main/java/com/sujay/urlshortener/service/impl/UrlServiceImpl.java. On creation, it derives a code through the short-code abstraction (src/main/java/com/sujay/urlshortener/shortcode/ShortCodeGenerator.java, src/main/java/com/sujay/urlshortener/shortcode/SqidsShortCodeGenerator.java), calculates expiration (src/main/java/com/sujay/urlshortener/util/ExpiryCalculator.java), and persists a Url record through src/main/java/com/sujay/urlshortener/repository/UrlRepository.java. Expiry behavior is modeled by src/main/java/com/sujay/urlshortener/enums/ExpiryOption.java.
Persistence and Cache Read Path
The durable URL model is src/main/java/com/sujay/urlshortener/entity/Url.java, backed by the repository layer. Redis is configured at src/main/java/com/sujay/urlshortener/Config/RedisConfig.java and accessed through src/main/java/com/sujay/urlshortener/cache/service/RedisCacheService.java. The expected redirect path is cache-first, with persisted lookup as the fallback; cached URL representation is src/main/java/com/sujay/urlshortener/cache/model/CachedUrl.java. This creates a clear boundary between low-latency resolution state and durable URL records.
Analytics and Background Processing
Redirect activity is accumulated via src/main/java/com/sujay/urlshortener/cache/service/AnalyticsCacheService.java, then periodically persisted by src/main/java/com/sujay/urlshortener/service/AnalyticsFlushService.java. Scheduling is separated into src/main/java/com/sujay/urlshortener/scheduler/AnalyticsFlushScheduler.java and src/main/java/com/sujay/urlshortener/scheduler/ExpiredUrlScheduler.java; the latter drives cleanup through src/main/java/com/sujay/urlshortener/service/CleanupService.java. This decouples redirect latency from analytics writes and expired-record maintenance.
Failure, Health, and Observability
Domain failures distinguish missing codes from expired URLs through src/main/java/com/sujay/urlshortener/exception/ShortUrlNotFoundException.java and src/main/java/com/sujay/urlshortener/exception/UrlExpiredException.java; src/main/java/com/sujay/urlshortener/exception/GlobalExceptionHandler.java maps them to HTTP errors. Operational endpoints/checks and cache metrics are implemented in src/main/java/com/sujay/urlshortener/health/HealthCheck.java and src/main/java/com/sujay/urlshortener/metrics/MetricsService.java.
Infrastructure and Load Characteristics
The service depends on Redis for caching and buffered analytics, with connection settings provided by application configuration classes. docker-compose.yml and prometheus/prometheus.yml indicate containerized local infrastructure and Prometheus scraping. Load scenarios target the two core traffic modes—URL creation and redirect resolution—in load-test/create-url.js and load-test/redirect.js.
Main entry points: UrlService.java URL service · UrlController.java URL controller · UrlRepository.java URL repository · AnalyticsFlushService.java Analytics flush service
Components 20
HTTP boundary
- URL controller · Handles shortening, redirects, and URL statistics.
src/main/java/com/sujay/urlshortener/controller/UrlController.java - Home controller
src/main/java/com/sujay/urlshortener/controller/HomeController.java - API DTOs
src/main/java/com/sujay/urlshortener/dto/request/ShortenUrlRequest.java - Global exception handler · Maps missing and expired URL failures to HTTP errors.
src/main/java/com/sujay/urlshortener/exception/GlobalExceptionHandler.java
URL lifecycle
- URL service · Creates, resolves, and retrieves URL statistics.
src/main/java/com/sujay/urlshortener/service/UrlService.java - Short-code generator
src/main/java/com/sujay/urlshortener/shortcode/ShortCodeGenerator.java - Expiry calculation
src/main/java/com/sujay/urlshortener/util/ExpiryCalculator.java
Storage & cache
- URL repository
src/main/java/com/sujay/urlshortener/repository/UrlRepository.java - URL records · Stores destination, short code, expiry, and usage state.
src/main/java/com/sujay/urlshortener/entity/Url.java - Redis cache service · Serves cached URL resolution records.
src/main/java/com/sujay/urlshortener/cache/service/RedisCacheService.java - Redis · Holds resolution cache and buffered analytics.
- Analytics cache service · Accumulates redirect activity outside the request write path.
src/main/java/com/sujay/urlshortener/cache/service/AnalyticsCacheService.java
Background processing
- Analytics flush service
src/main/java/com/sujay/urlshortener/service/AnalyticsFlushService.java - Analytics flush scheduler
src/main/java/com/sujay/urlshortener/scheduler/AnalyticsFlushScheduler.java - Expired URL scheduler
src/main/java/com/sujay/urlshortener/scheduler/ExpiredUrlScheduler.java - Cleanup service
src/main/java/com/sujay/urlshortener/service/CleanupService.java
Operations
- Health checks
src/main/java/com/sujay/urlshortener/health/HealthCheck.java - Metrics service · Publishes cache metrics for operational monitoring.
src/main/java/com/sujay/urlshortener/metrics/MetricsService.java - Prometheus
prometheus/prometheus.yml
Other components
- Spring Boot application
src/main/java/com/sujay/urlshortener/UrlShortenerApplication.java
Connections 21
- Spring Boot application → URL controller: starts
- Spring Boot application → Home controller: starts
- URL controller → API DTOs: uses
- URL controller → URL service: create, resolve, stats
- URL controller → Global exception handler: errors handled by
- URL service → Short-code generator: generates code
- URL service → Expiry calculation: calculates expiry
- URL service → Redis cache service: cache-first resolution
- Redis cache service → Redis: reads and writes
- URL service → URL repository: fallback and persistence
- URL repository → URL records: stores
- URL service → Analytics cache service: records redirects
- Analytics cache service → Redis: buffers counters
- Analytics flush scheduler → Analytics flush service: triggers
- Analytics flush service → Analytics cache service: drains
- Analytics flush service → URL repository: persists usage
- Expired URL scheduler → Cleanup service: triggers
- Cleanup service → URL repository: removes expired records
- Health checks → Redis: checks
- Metrics service → Redis: collects cache metrics
- Prometheus → Metrics service: scrapes