A URL shortening service built with Go, optimized for low-latency redirects and high throughput.
Sub-millisecond redirects through Redis L1 cache with automatic fallback to PostgreSQL. Connection pooling via pgxpool eliminates handshake overhead on database queries.
Distributed ID generation using Snowflake algorithm - no database sequences, no coordination overhead, no hotspots. IDs are generated in-process with guaranteed uniqueness across instances.
Compact representation via Base62 encoding reduces URL length while maintaining readability (62 characters vs 16 for hex).
Snowflake-based 64-bit IDs composed of:
- Uses a custom epoch (November 2023) to maximize ID space
- Combines timestamp (milliseconds), machine ID, and sequence number
- Thread-safe generation with mutex locks
- No collisions even under high load
These IDs are then encoded to Base62 (a-z, A-Z, 0-9) to create compact URL-friendly codes.
Start PostgreSQL and Redis:
docker-compose up -dCreate .env file:
DATABASE_URL=postgres://refx_user:refx-password@localhost:5432/refx_db
REDIS_CONNECTION=redis://localhost:6379Run the service:
go run cmd/main.goCreate short URL:
curl -X POST http://localhost:8080/api/urls \
-H "Content-Type: application/json" \
-d '{"long_url": "https://example.com"}'Redirect to original URL:
curl http://localhost:8080/api/urls/{short_code}