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.
- 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
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 │ └──────────┘
└─────────┘
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 |
- Docker and Docker Compose
- Java 17 or higher (for local development)
- Maven 3.6+ (for local development)
-
Clone the repository
git clone https://github.com/wastech/url-shortener.git cd url-shortener
-
Start all services
docker-compose up -d
-
Verify services are running
docker-compose ps
The application will be available at http://localhost:8080
-
Start infrastructure services
docker-compose up -d postgres redis kafka zookeeper
-
Run the application
./mvnw spring-boot:run
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 |
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)
POST /api/shorten
Content-Type: application/json
{
"longUrl": "https://www.example.com"
}
Response:
{
"shortCode": "00008Ck",
}
GET /{shortCode}
Response: Redirects to the original long URL
GET /api/url/{shortCode}
Response:
{
"shortCode": "00008Ck",
"longUrl": "https://example/00008Ck",
"createdAt": "2025-06-13T10:30:00Z"
}
- Duplicate Check: Service checks if the long URL already exists
- Key Generation: Unique short code generated via Key Generation Service
- Async Persistence: URL mapping published to Kafka topic
- Caching: Mapping cached in Redis for fast retrieval
- Response: Short code returned to client
- Cache Check: Redis cache checked first for O(1) lookup
- Database Fallback: If cache miss, query PostgreSQL database
- Cache Population: Retrieved data cached for future requests
- Redirect: Client redirected to original URL
- 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
./mvnw test
./mvnw integration-test
# Using curl for basic testing
curl -X POST http://localhost:8080/shorten \
-H "Content-Type: application/json" \
-d '{"longUrl": "https://example.com"}'
- Application:
http://localhost:8080/actuator/health
- Database:
http://localhost:8080/actuator/health/db
- Redis:
http://localhost:8080/actuator/health/redis
- Prometheus metrics:
http://localhost:8080/actuator/prometheus
- Application metrics:
http://localhost:8080/actuator/metrics
# 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
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
# Clean build
./mvnw clean package
# Skip tests
./mvnw clean package -DskipTests
# Build Docker image
docker build -t url-shortener .
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Spring Boot team for the excellent framework
- Redis Labs for the blazing-fast cache
- Apache Kafka for reliable messaging
- PostgreSQL for robust data persistence
For questions and support:
- Create an issue on GitHub
- Contact: [[email protected]]
- Documentation: Wiki
⭐ Star this repository if you find it helpful!