Skip to content

Latest commit

Β 

History

19 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ”— URL Shortener

A Production-Ready URL Shortener built with Spring Boot, MySQL, Redis, Flyway & Docker

Java Spring Boot MySQL Redis Flyway Docker License


πŸ“– About

A Spring Boot URL Shortener application with Redis caching, MySQL persistence, Flyway database migrations, rate limiting, and click analytics.

This project converts long URLs into short, shareable links. It also tracks redirects, stores click data, applies rate limiting, and uses Redis for fast access.

The goal of this project is to showcase backend engineering practices commonly used in modern Java applications.


✨ Features

  • πŸ”— Shorten long URLs into compact short codes
  • ✍️ Support custom aliases
  • β†ͺ️ Redirect short URLs to the original destination
  • πŸ“Š Track click count and click analytics
  • 🚦 Redis-based rate limiting
  • ⚑ Cache original URLs in Redis for fast redirects
  • πŸ—„οΈ Persist URLs and click events in MySQL
  • πŸ”„ Manage database schema with Flyway
  • 🐳 Containerized with Docker and Docker Compose

πŸ—οΈ Application Flow

Main flow

  1. User sends a long URL to the shorten endpoint.
  2. Application generates a unique short code.
  3. URL data is stored in MySQL.
  4. Original URL is cached in Redis.
  5. User opens the short URL.
  6. Application checks Redis first.
  7. If found, the user is redirected immediately.
  8. Click event is stored for analytics.

URL Shortening Flow

ChatGPT Image Aug 1, 2026, 09_12_52 PM


Redirect Flow

ChatGPT Image Aug 1, 2026, 09_13_08 PM


πŸ“Œ Results

Create Short URL

Screenshot 2026-08-01 212920


Redirect

Screenshot 2026-08-01 213011


URL Statistics

Screenshot 2026-08-01 213047


URL Analytics

Screenshot 2026-08-01 213123


πŸ› οΈ Tech Stack

Category Technology
Language Java 21
Framework Spring Boot
Build Tool Maven
Database MySQL
Cache Redis
ORM Spring Data JPA
Migration Flyway
Containerization Docker
Utilities Lombok

πŸ“‚ Project Structure

src
β”œβ”€β”€ main
β”‚   β”œβ”€β”€ java
β”‚   β”‚   └── com.ragul.UrlShortener
β”‚   β”‚       β”œβ”€β”€ config
β”‚   β”‚       β”œβ”€β”€ controller
β”‚   β”‚       β”œβ”€β”€ dto
β”‚   β”‚       β”œβ”€β”€ model
β”‚   β”‚       β”œβ”€β”€ repository
β”‚   β”‚       β”œβ”€β”€ service
β”‚   β”‚       β”œβ”€β”€ exception
β”‚   β”‚       └── util
β”‚   β”‚
β”‚   └── resources
β”‚       β”œβ”€β”€ application.yml
β”‚       └── db
β”‚           └── migration
β”‚
└── test

🌐 REST API

Method Endpoint Description
POST /api/shorten Create a short URL
GET /api/{shortCode} Redirect to original URL
GET /api/stats/{shortCode} URL statistics
GET /api/analytics/{shortCode} Click analytics
DELETE /api/{shortCode} Delete URL

πŸ“₯ Sample Request

{
  "originalUrl": "https://example.com/articles/spring-boot",
  "customAlias": "spring",
  "expiresAt": "2026-12-31T23:59:59"
}

πŸ“€ Sample Response

{
  "shortUrl": "http://localhost:8080/api/spring",
  "shortCode": "spring",
  "originalUrl": "https://example.com/articles/spring-boot",
  "createdAt": "2026-08-01T10:30:00",
  "expiresAt": "2026-12-31T23:59:59"
}

βš™οΈ Configuration

Configure your environment variables:

DB_URL=jdbc:mysql://localhost:3306/url_shortener
DB_USERNAME=root
DB_PASSWORD=password
REDIS_HOST=localhost
REDIS_PORT=6379

Never commit your .env file. Use .env.example instead.


⚑ application.yml Example

server:
  port: 8080

spring:
  application:
    name: UrlShortener

  datasource:
    url: ${DB_URL}
    username: ${DB_USERNAME}
    password: ${DB_PASSWORD}

  jpa:
    hibernate:
      ddl-auto: validate
    show-sql: true

  flyway:
    enabled: true
    locations: classpath:db/migration
    baseline-on-migrate: true

  data:
    redis:
      host: ${REDIS_HOST}
      port: ${REDIS_PORT}
      timeout: 2000ms

url-shortener:
  base-url: http://localhost:8080
  short-code:
    length: 6
    max-attempts: 10
  rate-limit:
    max-requests-per-minute: 2
    max-requests-per-hour: 10
    minute-window: 1m
    hour-window: 1h
  cache:
    ttl-minutes: 30

πŸš€ Running Locally

Clone Repository

git clone https://github.com/<your-username>/UrlShortener.git
cd UrlShortener

Start MySQL & Redis

Make sure both services are running.

Run Application

./mvnw spring-boot:run

or

mvn spring-boot:run

🐳 Docker

Build and start all services

docker compose up --build

Run in detached mode

docker compose up -d

Stop containers

docker compose down

πŸ—„οΈ Database

Flyway automatically creates and manages the database schema.

Main Tables

  • url_data
  • click_events

πŸ“ˆ Future Enhancements

  • Add authentication and user-specific URL management
  • QR code generation
  • Add Swagger/OpenAPI documentation
  • Add unit and integration tests
  • Add a dashboard UI for analytics
  • Add custom domain support
  • Monitoring with Prometheus & Grafana

🀝 Contributing

Contributions, suggestions, and improvements are welcome.

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License.


⭐ If you found this project helpful, consider giving it a star!

Built with ❀️ using Spring Boot, Redis, MySQL, and Docker

About

to generate a short easy to share link

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages