A high-performance URL shortening service built with Java, Spring Boot, Redis, and PostgreSQL — inspired by Bitly.
This project implements a production-grade URL shortening service that converts long URLs into compact, shareable links and redirects users with minimal latency.
It demonstrates core backend engineering principles across the full stack:
| Concept | Implementation |
|---|---|
| REST API Design | Spring Boot Controllers |
| Caching Strategy | Redis (in-memory) |
| Data Persistence | PostgreSQL |
| ID Generation | Base62 Encoding |
| Containerization | Docker Compose |
| Scalable Architecture | Stateless services + DB replication |
- Client sends a
POST /shortenerrequest with a long URL - Spring Boot API receives and validates the request
- URLConverterService generates a unique short code via Base62 encoding
- URL mapping is persisted in PostgreSQL
- Redis caches the mapping for fast future lookups
- Redirect requests are served directly from Redis when cached, falling back to PostgreSQL on cache miss
| Layer | Technology |
|---|---|
| Language | Java |
| Framework | Spring Boot |
| Cache | Redis |
| Database | PostgreSQL |
| Build Tool | Maven |
| Containers | Docker + Docker Compose |
url-shortener/
└── src/main/java/com/urlshortener/
├── controller/
│ └── URLController.java # REST API endpoints
├── service/
│ └── URLConverterService.java # Core business logic
├── repository/
│ └── URLRepository.java # Redis + PostgreSQL I/O
├── common/
│ ├── IDConverter.java # Base62 encoding (Singleton)
│ └── URLValidator.java # URL format validation
└── URLShortenerApplication.java # Application entry point
IDConverter.java — Singleton responsible for:
- Generating numeric IDs
- Converting numeric IDs into Base62 short codes (e.g.
aB12X) - Decoding short codes back to their original numeric IDs
URLValidator.java — Utility class that validates URL format and structure before processing.
URLController.java — Spring Boot REST controller that:
- Accepts URL shortening requests (
POST /shortener) - Handles redirect logic for short codes (
GET /{shortCode})
URLRepository.java — Data access layer handling read/write operations to both Redis and PostgreSQL.
URLConverterService.java — Core service implementing:
- URL shortening pipeline
- URL retrieval and redirect resolution
URLShortenerApplication.java — Spring Boot application entry point.
POST /shortener
Content-Type: application/jsonRequest Body:
{
"url": "https://example.com/very-long-url"
}Response:
{
"shortUrl": "http://localhost:8080/aB12X"
}GET /{shortCode}Example:
GET http://localhost:8080/aB12X
Response: HTTP 302 Redirect → original URL
Step 1 — Clone the repository
git clone https://github.com/<your-username>/url-shortener
cd url-shortenerStep 2 — Start all services
docker-compose up --buildThis spins up:
- Spring Boot Application
- Redis Server
- PostgreSQL Database
1. Start Redis
redis-server2. Build the project
mvn clean install3. Run the application
mvn spring-boot:runThe server starts at → http://localhost:8080
curl -X POST http://localhost:8080/shortener \
-H "Content-Type: application/json" \
-d '{"url": "https://google.com"}'Redis is used to cache frequently accessed URL mappings for near-instant redirects.
Cache Resolution Flow:
Incoming short code
│
▼
Redis cache hit? ──Yes──▶ Redirect immediately
│
No
│
▼
Query PostgreSQL
│
▼
Store in Redis ──────────▶ Redirect to original URL
This architecture is designed to scale horizontally:
- Load Balancers — Distribute traffic across multiple Spring Boot instances
- Stateless API Layer — Any instance can handle any request
- Redis Clustering — Distributed cache across multiple nodes
- PostgreSQL Replication — Read replicas reduce DB bottlenecks
- Custom alias URLs (e.g.
/my-link) - Link expiration with TTL support
- Analytics dashboard (click counts, geolocation)
- API rate limiting per client
- User authentication and link ownership
Apache License 2.0 © Bathula Ganesh