Skip to content

wastech/url-shortener

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

URL Shortener

A high-performance, scalable URL shortening service built with Spring Boot. This service provides fast URL shortening and retrieval with Redis caching, Kafka-based asynchronous processing, and PostgreSQL persistence.

πŸš€ Features

  • Fast URL Shortening: Generate short codes for long URLs with sub-second response times
  • Redis Caching: Intelligent caching layer for optimal performance
  • Duplicate Detection: Prevents duplicate entries for the same long URL
  • Asynchronous Processing: Kafka-based event-driven architecture for scalability
  • Retry Logic: Robust error handling with exponential backoff
  • Transaction Support: ACID compliance with PostgreSQL
  • Docker Support: Fully containerized deployment

πŸ—οΈ Architecture

The service follows a layered architecture with the following components:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Client API    │───▢│ Spring Boot  │───▢│ Key Generation  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚   Service    β”‚    β”‚    Service      β”‚
                       β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β–Ό         β–Ό         β–Ό
            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
            β”‚  Redis   β”‚ β”‚  Kafka  β”‚ β”‚PostgreSQLβ”‚
            β”‚  Cache   β”‚ β”‚ Message β”‚ β”‚ Database β”‚
            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚  Queue  β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ› οΈ Technology Stack

Component Technology Purpose
Backend Java 17 + Spring Boot Main application framework
Database PostgreSQL Persistent storage for URL mappings
Cache Redis High-speed caching layer
Message Queue Apache Kafka Asynchronous processing
Coordination Apache ZooKeeper Kafka cluster coordination
Containerization Docker Application packaging and deployment

πŸ“‹ Prerequisites

  • Docker and Docker Compose
  • Java 17 or higher (for local development)
  • Maven 3.6+ (for local development)

πŸš€ Quick Start

Using Docker Compose (Recommended)

  1. Clone the repository

    git clone https://github.com/wastech/url-shortener.git
    cd url-shortener
  2. Start all services

    docker-compose up -d
  3. Verify services are running

    docker-compose ps

The application will be available at http://localhost:8080

Local Development Setup

  1. Start infrastructure services

    docker-compose up -d postgres redis kafka zookeeper
  2. Run the application

    ./mvnw spring-boot:run

πŸ”§ Configuration

Environment Variables

Variable Default Description
POSTGRES_URL jdbc:postgresql://localhost:5432/urlshortener PostgreSQL connection URL
REDIS_HOST localhost Redis server host
REDIS_PORT 6379 Redis server port
KAFKA_BOOTSTRAP_SERVERS localhost:9092 Kafka broker addresses
CACHE_TTL_SECONDS 3600 Redis cache TTL in seconds

Docker Compose Services

services:
  - app: Spring Boot application (port 8080)
  - postgres: PostgreSQL database (port 5432)
  - redis: Redis cache (port 6379)
  - kafka: Apache Kafka (port 9092)
  - zookeeper: ZooKeeper coordination (port 2181)

πŸ“‘ API Endpoints

Shorten URL

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

{
  "longUrl": "https://www.example.com"
}

Response:

{
  "shortCode": "00008Ck",
}

Retrieve Original URL

GET /{shortCode}

Response: Redirects to the original long URL

Get URL Information

GET /api/url/{shortCode}

Response:

{
  "shortCode": "00008Ck",
  "longUrl": "https://example/00008Ck",
  "createdAt": "2025-06-13T10:30:00Z"
}

πŸ”„ How It Works

URL Shortening Flow

  1. Duplicate Check: Service checks if the long URL already exists
  2. Key Generation: Unique short code generated via Key Generation Service
  3. Async Persistence: URL mapping published to Kafka topic
  4. Caching: Mapping cached in Redis for fast retrieval
  5. Response: Short code returned to client

URL Retrieval Flow

  1. Cache Check: Redis cache checked first for O(1) lookup
  2. Database Fallback: If cache miss, query PostgreSQL database
  3. Cache Population: Retrieved data cached for future requests
  4. Redirect: Client redirected to original URL

Resilience Features

  • Retry Logic: Failed Kafka operations retried with exponential backoff
  • Circuit Breaker: Graceful degradation on service failures
  • Transaction Management: ACID compliance for data consistency
  • Caching Strategy: Multi-level caching for performance optimization

πŸ§ͺ Testing

Run Tests

./mvnw test

Integration Tests

./mvnw integration-test

Load Testing

# Using curl for basic testing
curl -X POST http://localhost:8080/shorten \
  -H "Content-Type: application/json" \
  -d '{"longUrl": "https://example.com"}'

πŸ“Š Monitoring

Health Checks

  • Application: http://localhost:8080/actuator/health
  • Database: http://localhost:8080/actuator/health/db
  • Redis: http://localhost:8080/actuator/health/redis

Metrics

  • Prometheus metrics: http://localhost:8080/actuator/prometheus
  • Application metrics: http://localhost:8080/actuator/metrics

🐳 Docker Commands

# Build application image
docker build -t url-shortener:latest .

# Start all services
docker-compose up -d

# View logs
docker-compose logs -f app

# Stop services
docker-compose down

# Rebuild and restart
docker-compose up -d --build

πŸ”§ Development

Project Structure

src/
β”œβ”€β”€ main/java/com/wastech/url_shortener/
β”‚   β”œβ”€β”€ controller/     # REST controllers
β”‚   β”œβ”€β”€ service/        # Business logic
β”‚   β”œβ”€β”€ repository/     # Data access layer
β”‚   β”œβ”€β”€ model/          # Entity classes
β”‚   └── config/         # Configuration classes
β”œβ”€β”€ main/resources/
β”‚   β”œβ”€β”€ application.yml # Application configuration
β”‚   └── db/migration/   # Database migrations
└── test/               # Unit and integration tests

Building from Source

# Clean build
./mvnw clean package

# Skip tests
./mvnw clean package -DskipTests

# Build Docker image
docker build -t url-shortener .

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Spring Boot team for the excellent framework
  • Redis Labs for the blazing-fast cache
  • Apache Kafka for reliable messaging
  • PostgreSQL for robust data persistence

πŸ“ž Support

For questions and support:


⭐ Star this repository if you find it helpful!

About

A high-performance, scalable URL shortening service built with Spring Boot. This service provides fast URL shortening and retrieval with Redis caching, Kafka-based asynchronous processing, and PostgreSQL persistence.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors