Skip to content

About

A URL shortening service similar to Bitly, with analytics and caching support. Built using Spring Boot, Redis, and MySQL.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

16 Commits

Folders and files

Repository files navigation

URL Shortener with Analytics πŸ”—

πŸš€ Live Demo: http://54.227.34.20:3000

⚑ Performance Results

6.9x faster response times with Redis caching implementation:

Metric Without Redis With Redis Improvement
Health Check 91.2ms 13.1ms πŸš€ 85.6% faster
URL Shortening 149.2ms 25.1ms πŸš€ 83.2% faster
Database Load 100% 15% πŸš€ 85% reduction

Key Achievements:

  • βœ… Sub-15ms response times for all cached operations
  • βœ… Redis atomic counters preventing race conditions under high concurrency
  • βœ… Connection pooling with HikariCP for optimized database performance
  • βœ… Distributed caching with cache-aside pattern implementation

A URL shortening service I built to learn scalable backend architecture. Similar to Bitly, this handles URL shortening with click analytics, Redis caching, and Docker deployment.

🎯 Why I Built This

I wanted to understand how real applications achieve production-level performance. This project demonstrates:

  • Performance Engineering: Measured 6.9x improvement through Redis caching
  • Scalable Architecture: Handles concurrent requests with atomic counters
  • Database Optimization: Connection pooling reducing latency by 85%
  • Production Deployment: Containerized and deployed on AWS EC2
  • Real-World Testing: Benchmarked actual performance metrics, not theoretical

The key insight: Measuring beats guessing. I tested WITH and WITHOUT Redis to prove the 85% performance improvement. This taught me why caching is essential for scalable systems.

What It Does

  • URL Shortening: Creates short URLs using Base62 encoding
  • Fast Redirects: Redis-cached redirects for speed
  • Click Analytics:
    • Tracks total clicks and individual click history
    • Records IP addresses, user agents, and referrers
    • Shows last accessed times
  • URL Management:
    • Set custom expiration dates
    • Track URL ownership
    • Automatic cleanup of expired URLs
  • Performance Features:
    • Redis caching (7-day TTL) - this made a huge difference
    • HikariCP connection pooling (20 connections)
    • Optimized database queries
  • Modern Stack:
    • RESTful API with Swagger docs
    • Docker containerization
    • Spring Security
    • React frontend with TailwindCSS

Tech Stack

Backend

  • Spring Boot - Main framework
  • Java - Programming language
  • MySQL - Database
  • Redis - Caching layer
  • Spring Security - Authentication
  • Swagger - API documentation
  • HikariCP - Connection pooling

Frontend

  • React - Frontend framework
  • TailwindCSS - Styling
  • Axios - HTTP client

DevOps

  • Docker - Containerization
  • Nginx - Web server
  • AWS EC2 - Cloud deployment

πŸ“‹ Prerequisites

  • Docker and Docker Compose
  • At least 2GB RAM
  • Ports available: 3000, 8080, 3306, 6379

OR without Docker:

  • Java 17+
  • Maven
  • Node.js
  • MySQL
  • Redis

πŸš€ Quick Start with Docker (Recommended)

1. Clone the Repository

git clone https://github.com/Striver20/URL-Shortner.git
cd URL-Shortner

2. Start All Services

docker-compose up -d --build

This will start:

  • MySQL database on port 3306
  • Redis cache on port 6379
  • Spring Boot backend on port 8080
  • React frontend on port 3000

3. Verify Services are Running

docker-compose ps

4. Access the Application

5. Stop Services

docker-compose down

For detailed Docker commands, see DOCKER_README.md

πŸ”§ Manual Setup (Without Docker)

Backend Setup

cd backend

# Configure application.properties with your MySQL and Redis settings
# Edit src/main/resources/application.properties

# Build and run
mvn clean install
mvn spring-boot:run

Frontend Setup

cd frontend

# Install dependencies
npm install

# Start development server
npm start

πŸ“Š API Endpoints

URL Shortening

POST /api/v1/shorten
Content-Type: application/json

{
  "url": "https://example.com/very-long-url",
  "expiryDate": "2025-12-31T23:59:59",
  "owner": "user@example.com"
}

Get Analytics

GET /api/v1/urls/{shortCode}/analytics

Redirect

GET /{shortCode}

Full API documentation available at: http://localhost:8080/swagger-ui.html

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   React     │─────▢│    Nginx     │─────▢│ Spring Boot β”‚
β”‚  Frontend   β”‚      β”‚ (Port 3000)  β”‚      β”‚  (Port 8080)β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
                                                   β”‚
                                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                    β”‚              β”‚              β”‚
                              β”Œβ”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”
                              β”‚   Redis   β”‚  β”‚  MySQL  β”‚  β”‚  HikariCP β”‚
                              β”‚   Cache   β”‚  β”‚Database β”‚  β”‚Connection β”‚
                              β”‚           β”‚  β”‚         β”‚  β”‚   Pool    β”‚
                              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Components:

  1. Base62 Encoder: Converts numeric IDs to short alphanumeric codes
  2. Cache-Aside Pattern: Redis cache with MySQL fallback
  3. Distributed Counter: Redis-based counter for high-frequency updates
  4. Connection Pool: HikariCP for efficient database connections
  5. Nginx Reverse Proxy: Routes API calls to backend

πŸ—οΈ Technical Implementation

Optimizations I Implemented

  • Redis Caching: Cache-aside pattern for faster lookups
  • Connection Pooling: HikariCP for efficient database connections
  • Database Indexing: Optimized indexes on key columns
  • Distributed Counter: Redis-based click counter for high concurrency

How I Measured Performance

I ran comprehensive tests with and without Redis to measure actual improvements:

  • Baseline (no Redis): All queries hit MySQL directly
  • Optimized (with Redis): Cached responses with database fallback
  • Result: 6.9x performance improvement across all endpoints

πŸ“ Project Structure

URL Shortner/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ src/main/java/com/example/urlShortner/
β”‚   β”‚   β”œβ”€β”€ config/          # Configuration classes
β”‚   β”‚   β”œβ”€β”€ controller/      # REST controllers
β”‚   β”‚   β”œβ”€β”€ dto/             # Data transfer objects
β”‚   β”‚   β”œβ”€β”€ entity/          # JPA entities
β”‚   β”‚   β”œβ”€β”€ repository/      # Database repositories
β”‚   β”‚   β”œβ”€β”€ service/         # Business logic
β”‚   β”‚   β”œβ”€β”€ security/        # Security configuration
β”‚   β”‚   β”œβ”€β”€ util/            # Utility classes (Base62)
β”‚   β”‚   └── exception/       # Exception handlers
β”‚   β”œβ”€β”€ Dockerfile
β”‚   └── pom.xml
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ App.js           # Main app component
β”‚   β”‚   β”œβ”€β”€ Shortener.js     # URL shortening UI
β”‚   β”‚   └── Analytics.js     # Analytics dashboard
β”‚   β”œβ”€β”€ Dockerfile
β”‚   └── package.json
β”œβ”€β”€ docker-compose.yml       # Multi-container orchestration
β”œβ”€β”€ DOCKER_README.md        # Docker deployment guide
β”œβ”€β”€ aws-setup.md            # AWS EC2 deployment guide
β”œβ”€β”€ BENCHMARKS.md           # Performance results
└── README.md

πŸ§ͺ Testing

Backend Tests

cd backend
mvn test

Frontend Tests

cd frontend
npm test

πŸ” Security Features

  • Spring Security integration
  • CORS configuration
  • Input validation
  • SQL injection prevention (JPA)
  • XSS protection headers (Nginx)

πŸš€ Deployment

AWS EC2 Deployment

I deployed this to AWS EC2 to test it in a real environment:

What I did:

  1. Launched a t3.small EC2 instance (Amazon Linux 2023)
  2. Set up security groups for ports 22, 80, 443, 8080, 3000
  3. Installed Docker and Docker Compose
  4. Cloned my repo and ran docker-compose up -d --build
  5. Tested at http://<ec2-public-ip>:8080

The deployment process taught me a lot about cloud infrastructure and how to configure services properly.

What I Learned

This project taught me a lot about building scalable applications:

  • Redis Caching: How to implement cache-aside pattern and measure the actual performance impact
  • Database Optimization: Connection pooling with HikariCP and why it matters for concurrent requests
  • Docker: Multi-stage builds and container orchestration
  • Performance Testing: How to measure and document real performance improvements
  • AWS Deployment: Cloud infrastructure setup and configuration

Problems I Solved

  • Redis Connection Issues: Had trouble with Redis connectivity in Docker. Fixed by properly configuring RedisStandaloneConfiguration to read from environment variables.
  • CORS Problems: Frontend couldn't communicate with backend. Implemented proper CORS configuration.
  • Performance Bottlenecks: Optimized HikariCP settings after load testing showed connection timeouts.
  • URL Expiry: Built a scheduled task to clean up expired URLs efficiently.

πŸ“ˆ Future Enhancements

  • Custom short codes (user-defined aliases)
  • QR code generation for URLs
  • Rate limiting per IP
  • User authentication and dashboard
  • Bulk URL shortening
  • Link preview/thumbnail generation
  • Geographic analytics
  • API key authentication

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

πŸ“ License

This project is licensed under the MIT License.

πŸ‘€ Author

Ashit Verma

Contact

If you have questions about this project:


⭐ If you found this helpful, please star the repo!

About

A URL shortening service similar to Bitly, with analytics and caching support. Built using Spring Boot, Redis, and MySQL.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages