Shortlyx is a production-grade URL shortening service engineered for high throughput and low latency. It leverages a layered architecture with Redis-buffered writes to handle traffic spikes efficiently, decoupling real-time request processing from database persistence.
Traditional URL shorteners hit the database for every click event, creating a bottleneck at scale. Shortlyx implements a Write-Behind strategy:
- Ingestion: Click events are atomically incremented in Redis (
INCR). - Buffering: Affected keys are tracked in a Redis Set.
- Persistence: A background worker asynchronously flushes aggregated stats to PostgreSQL.
- Result: Transforms
O(N)database writes intoO(1)batch updates per interval, significantly increasing write capacity.
The codebase follows strict separation of concerns to ensure maintainability and testability:
- Handlers (Transport): HTTP/JSON logic (Gin).
- Service (Business Logic): Validations, Caching rules, and domain logic.
- Repository (Data Access): Abstractions for PostgreSQL and Redis.
- Token Bucket Algorithm: Implemented via Redis to prevent abuse.
- Secure Headers: Configurable Proxy Trust for accurate IP rate limiting in load-balanced environments.
- Base62 Encoding: Efficiently maps unique Integer IDs to short alphanumeric strings.
- Database Sequences: Utilizes PostgreSQL
BIGSERIALfor collision-free, sequential ID generation, ensuring shortest possible URLs.
- Language: Go (Golang) 1.25+
- Web Framework: Gin
- Database: PostgreSQL 15+
- Caching / Broker: Redis 7.x
- Containerization: Docker Support (Planned)
POST /shorten
Request:
{
"long_url": "https://www.google.com/search?q=system+design",
"ttl_days": 7
}Response:
{
"short_url": "http://localhost:8080/1b"
}GET /:code
- Redirects to the original
long_url. - Asynchronously increments click count via Redis buffer.
GET /api/stats/:code
Response:
{
"short_code": "1b",
"long_url": "https://...",
"click_count": 42,
"created_at": "2024-01-01T12:00:00Z"
}Prerequisites:
- Go 1.25+
- PostgreSQL
- Redis
Configuration:
Create a .env file in the root directory:
PORT=8080
APP_ENV=development
DATABASE_URL=postgres://user:pass@localhost:5432/shortlyx?sslmode=disable
REDIS_URL=redis://localhost:6379/0Running the Application:
# Install dependencies
go mod download
# Run migrations (ensure DB exists)
# (Manual SQL execution of migrations/001_create_urls.sql required)
# Start server
go run main.go