In today's fast-paced digital world, long and complicated URLs can be a real headache. They're difficult to share, hard to remember, and often look messy in print or on social media. This common problem led me to build the Go Distributed & Scalable Link Shortener API – a service designed to transform those unwieldy links into short, memorable, and trackable ones, all while ensuring speed, security, and scalability.
This project isn't just about shortening links; it's about providing a reliable, high-performance API with essential features like automatic expiration, robust validation, and intelligent rate limiting to prevent misuse.
Let's dive into how it's built and why these choices were made.
The Powerhouse Behind the Links: Our Technology Stack
Choosing the right tools is crucial for any scalable application. Here's what powers our link shortener and the rationale behind each choice:
Go (Golang): Go's strengths in concurrency management, rapid compilation, static typing, and performance make it ideal for high-concurrency services. Its built-in HTTP server is fast and reliable.
Redis: Used as the primary datastore. Its in-memory design ensures fast read/write, and TTL (Time-To-Live) simplifies auto-expiration. Atomic operations make it great for rate limiting.
Docker: Provides containerized environments that ensure consistent development and production deployments.
Gorilla Mux: A versatile router that handles routing, path variables, and method matching effectively in Go.
Upstash Redis: A managed, serverless Redis solution that simplifies deployment and maintenance with built-in TLS and scalability.
Architectural Blueprint: How It All Connects
The service operates as a stateless microservice:
+------------------+ HTTPS +---------------------+ HTTP/S +-------------------+
| Client Device | <------------>| Render (Proxy) | <------------->| Go API Service |
| (Browser/cURL) | | (HTTPS Termination) | | (Docker Container)|
+------------------+ +---------------------+ +--------+----------+
|
| TLS
|
+-------+--------+
| Upstash Redis |
| (Data Storage, |
| Rate Limiting) |
+----------------+
Workflow at a Glance:
- Request Ingress: Client sends HTTP/HTTPS requests.
- Render Proxy: Handles HTTPS and forwards to Go service.
- Go API Service:
- Rate Limiting: Redis checks IP-based usage.
- URL Validation: Ensures HTTPS and no malicious input.
- Duplicate Check: Returns existing short URL if already stored.
- Short Code Generation: Creates or verifies custom short code.
- Redis Storage: Stores mappings with 7-day TTL.
-
Response: Returns short URL and remaining rate limit.
- Redirection: Resolves short URL to long URL using Redis and sends a 301 redirect.
- Redis Connectivity: All interactions are TLS-encrypted.
Key Features That Make a Difference
- Link Shortening: Converts long URLs to concise versions.
- Optional Custom Shorts: User-defined short paths are supported.
- Automatic Expiration (TTL): Links expire in 7 days.
- Duplicate Handling: Existing links return the same short URL and renew the TTL.
- Strict HTTPS Validation: Validates incoming URLs to ensure security.
- Rate Limiting: Max 15 requests per 2 minutes per IP.
-
Enhanced API Response:
/shorten
endpoint includes remaining rate limit. - Dockerized Deployment: Simplifies setup and scaling.
-
Environment Config: All settings handled via
.env
variables. - TLS-Secured Redis Connection: Fully encrypted communication with Redis.
- CORS Support: Manages cross-origin requests with secure defaults.
API Usage: Getting Started
Base URLs:
- Local HTTP:
http://localhost:8080
- Local HTTPS:
https://localhost:8443
- Render Deployment:
https://your-render-service-name.onrender.com
1. Shorten a URL
POST /shorten
Request Body (JSON)
{
"long_url": "https://example.com/very/long/path",
"custom_short": "optional-custom-code"
}
Example curl
curl -k -X POST \
-H "Content-Type: application/json" \
-d '{"long_url": "https://github.com/golang/go"}' \
https://localhost:8443/shorten
Success Response
{
"short_url": "/s/AbC12X",
"long_url": "https://github.com/golang/go",
"limit_remaining": 14,
"message": "URL shortened successfully"
}
2. Redirect from Short URL
GET /s/{short}
Example curl
curl -k -L https://localhost:8443/s/AbC12X
- Returns an HTTP 301 redirect to the long URL.
3. Health Check
GET /health
curl http://localhost:8080/health
- Returns
200 OK
with messageOK
Local Setup
Prerequisites:
- Go 1.22+
- Docker + Docker Compose
- OpenSSL
Steps:
git clone https://github.com/shushantrishav/Go_URL_Shortner.git
cd Go_URL_Shortner
Create .env
file:
REDIS_ADDR=localhost:6379
REDIS_PASSWORD=your_redis_password
REDIS_DB=0
HTTP_PORT=8080
HTTPS_PORT=8443
TLS_CERT_PATH=/app/localhost.crt
TLS_KEY_PATH=/app/localhost.key
Generate Self-Signed Certificates:
openssl req -x509 -newkey rsa:4096 -nodes -keyout localhost.key -out localhost.crt -days 365 -subj "/CN=localhost"
go mod tidy
docker-compose up --build
Seamless Deployment on Render
- Push to GitHub
- Create a new Web Service in Render
- Choose Docker as runtime
-
Set env variables:
PORT=8080
-
REDIS_ADDR
,REDIS_PASSWORD
,REDIS_DB
Render handles HTTPS automatically. No TLS certs required in code.
Important Notes
- Never hardcode secrets like
REDIS_PASSWORD
- Local HTTPS uses self-signed certs. Use
curl -k
to bypass warnings. - CORS config must be production-aware
- Rate limit by IP; can be adjusted for users behind proxies
Future Improvements
- User Auth for managing personal links
- Custom Domain Support
- Analytics: Clicks, referrers, geolocation
- QR Code generation
- Admin dashboard
- Advanced rate limiting strategies
- Database abstraction for metadata
- Monitoring & graceful shutdowns
🔍 Explore the Project
🧪 Live Demo: Cutlink
💻 GitHub Repo: Go_URL_Shortner
Thanks for reading!
👉 Like this project? Drop a star on GitHub
💬 Have questions or feedback? Let’s connect in the comments!
Feel free to contribute or ask questions!
Top comments (0)