A scalable URL shortener service built with Flask and Redis, deployed using Docker and Kubernetes.
- URL shortening using base62 encoding
- Redis-backed storage with configurable expiration
- Load balanced and horizontally scalable
- Health check endpoints
- Docker and Kubernetes ready
- Automatic scaling based on CPU and memory usage
- Python 3.12
- Flask web framework
- Redis for storage
- Docker for containerization
- Kubernetes for orchestration
- Gunicorn for production WSGI server
- Docker and Docker Compose
- Python 3.12 (for local development without Docker)
- kubectl (for Kubernetes deployment)
- Clone the repository
- Navigate to the project directory
- Start the services:
docker-compose up --build
- The service will be available at http://localhost:8000
- Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
pip install -r app/requirements.txt
- Start Redis (required)
- Run the application:
cd app python app.py
- Kubernetes cluster
- kubectl configured to connect to your cluster
- Docker registry access
-
Build and push the Docker image:
docker build -t your-registry/url-shortener:latest ./app docker push your-registry/url-shortener:latest
-
Update the image in
k8s/url-shortener-deployment.yamlto match your registry. -
Apply the Kubernetes manifests:
kubectl apply -f k8s/
-
Verify the deployment:
kubectl get pods kubectl get services kubectl get ingress
Creates a shortened URL.
Request body:
{
"url": "https://example.com/very/long/url"
}Response:
{
"short_url": "http://domain/abc123",
"short_code": "abc123",
"long_url": "https://example.com/very/long/url"
}Redirects to the original URL.
Health check endpoint.
Response:
{
"status": "healthy",
"redis": "connected"
}-
Create a short URL:
curl -X POST -H "Content-Type: application/json" -d '{"url":"YOUR_LONG_URL"}' http://localhost:8000/shorten
-
Access the shortened URL:
curl -L http://localhost:8000/{short_code}
You can use tools like Apache Benchmark or wrk for load testing:
ab -n 1000 -c 10 http://localhost:8000/healthThe service includes:
- Kubernetes HorizontalPodAutoscaler (HPA)
- Resource requests and limits
- Health check endpoints
- Redis connection monitoring
The HPA will automatically scale the deployment based on:
- CPU utilization (target: 70%)
- Memory utilization (target: 80%)
- Running as non-root user in containers
- Resource limits enforced
- Input validation for URLs
- No sensitive data exposure
- Rate limiting (to be implemented)
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
-
Stop the containers:
docker-compose down -
Start the containers again:
docker-compose up -d -
View logs:
docker-compose logs