Skip to content

tanmayg1313/Load-balanced-URL-Shortner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

URL Shortener Service

A scalable URL shortener service built with Flask and Redis, deployed using Docker and Kubernetes.

Features

  • 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

Tech Stack

  • Python 3.12
  • Flask web framework
  • Redis for storage
  • Docker for containerization
  • Kubernetes for orchestration
  • Gunicorn for production WSGI server

Local Development

Prerequisites

  • Docker and Docker Compose
  • Python 3.12 (for local development without Docker)
  • kubectl (for Kubernetes deployment)

Running with Docker Compose

  1. Clone the repository
  2. Navigate to the project directory
  3. Start the services:
    docker-compose up --build
  4. The service will be available at http://localhost:8000

Running Locally (without Docker)

  1. Create a virtual environment:
    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
  2. Install dependencies:
    pip install -r app/requirements.txt
  3. Start Redis (required)
  4. Run the application:
    cd app
    python app.py

Kubernetes Deployment

Prerequisites

  • Kubernetes cluster
  • kubectl configured to connect to your cluster
  • Docker registry access

Deployment Steps

  1. Build and push the Docker image:

    docker build -t your-registry/url-shortener:latest ./app
    docker push your-registry/url-shortener:latest
  2. Update the image in k8s/url-shortener-deployment.yaml to match your registry.

  3. Apply the Kubernetes manifests:

    kubectl apply -f k8s/
  4. Verify the deployment:

    kubectl get pods
    kubectl get services
    kubectl get ingress

API Documentation

Endpoints

POST /shorten

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"
}

GET /{short_code}

Redirects to the original URL.

GET /health

Health check endpoint.

Response:

{
    "status": "healthy",
    "redis": "connected"
}

Testing

Manual Testing

  1. Create a short URL:

    curl -X POST -H "Content-Type: application/json" -d '{"url":"YOUR_LONG_URL"}' http://localhost:8000/shorten
  2. Access the shortened URL:

    curl -L http://localhost:8000/{short_code}

Load Testing

You can use tools like Apache Benchmark or wrk for load testing:

ab -n 1000 -c 10 http://localhost:8000/health

Monitoring and Scaling

The 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%)

Security Considerations

  • Running as non-root user in containers
  • Resource limits enforced
  • Input validation for URLs
  • No sensitive data exposure
  • Rate limiting (to be implemented)

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Managing the Docker containers:

  • Stop the containers:

    docker-compose down
    
  • Start the containers again:

    docker-compose up -d
    
  • View logs:

    docker-compose logs
    

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors