Skip to content

abdulh-dev/Distributed-Video-Streaming-Platform-with-Real-Time-Observability

Repository files navigation

Distributed Video Streaming CDN with Real-Time Observability

A production-grade Content Delivery Network (CDN) system featuring intelligent load balancing, real-time metrics monitoring with Prometheus, WebSocket-based live dashboards, and fully containerized Docker deployment.

🎯 Features

  • Distributed Architecture: Multi-server video streaming with automatic failover
  • Intelligent Load Balancing: 4-tier weighted algorithm based on CPU, memory, request rate, concurrent streams, and response time
  • Real-Time Monitoring: Prometheus metrics collection with 1-second granularity
  • Live Dashboard: WebSocket-powered real-time visualization using Chart.js
  • Containerized Deployment: Docker Compose orchestration for reproducible environments
  • HTTP Range Support: Efficient partial content delivery for video streaming
  • Per-Server Metrics: Individual throughput and resource tracking

🏗️ Architecture

┌─────────────┐
│   Clients   │
└──────┬──────┘
       │
┌──────▼──────────────┐
│  Load Balancer      │◄──── Prometheus
│  (Port 80)          │      (Port 9090)
└──────┬──────────────┘
       │
       ├──────┬──────┬──────┐
       ▼      ▼      ▼      ▼
   ┌────┐ ┌────┐ ┌────┐ ┌────┐
   │ S1 │ │ S2 │ │ S3 │ │ SN │
   └────┘ └────┘ └────┘ └────┘
   Video Servers (Port 8080)

📋 Prerequisites

  • Python 3.9+
  • Docker & Docker Compose
  • 2GB+ RAM recommended
  • Linux/macOS (or WSL on Windows)

🚀 Quick Start

Option 1: Docker Compose (Recommended)

# Clone the repository
git clone 
cd cdn-streaming-platform

# Place your video file
cp /path/to/your/video.mp4 ./OrcaDocu.mp4

# Start all services
docker-compose up -d

# View logs
docker-compose logs -f

# Access services
# - Video Stream: http://localhost/
# - Dashboard: http://localhost/dashboard
# - Prometheus: http://localhost:9090
# - Metrics API: http://localhost/metrics

Option 2: Manual Setup

1. Install Dependencies

# Update system
sudo apt update && sudo apt upgrade -y

# Install Python packages
pip3 install -r requirements.txt

# Or install individually
pip3 install fastapi uvicorn psutil prometheus-client websockets

2. Setup Video Servers

# On each server node (repeat for Server 1, 2, 3)
python3 video_server.py

# Or with uvicorn directly
uvicorn video_server:app --host 0.0.0.0 --port 8080

3. Configure Load Balancer

Edit load_balancer.py and update server IPs:

servers = {
    "server1-ip": "http://server1-ip:8080/OrcaDocu.mp4",
    "server2-ip": "http://server2-ip:8080/OrcaDocu.mp4",
    "server3-ip": "http://server3-ip:8080/OrcaDocu.mp4"
}

Start the load balancer:

sudo uvicorn load_balancer:app --host 0.0.0.0 --port 80

4. Setup Prometheus

# Download Prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gz
tar xvfz prometheus-2.45.0.linux-amd64.tar.gz
cd prometheus-2.45.0.linux-amd64/

# Start Prometheus
./prometheus --config.file=../prometheus.yml

📊 Monitoring & Metrics

Available Endpoints

Endpoint Description
http://localhost/ Video streaming endpoint
http://localhost/dashboard Real-time metrics dashboard
http://localhost/metrics JSON metrics API
http://localhost/prometheus Prometheus metrics endpoint
http://localhost:9090 Prometheus UI
ws://localhost/ws WebSocket live metrics stream

Tracked Metrics

Global Metrics:

  • Request rate (requests/min)
  • Concurrent streams
  • Bandwidth usage (Mbps)
  • Average response time
  • CPU & Memory utilization

Per-Server Metrics:

  • Individual throughput (Mbps)
  • Server load scores
  • Resource utilization
  • Request distribution

🔧 Configuration

Load Balancing Weights

Adjust weights in load_balancer.py:

weights = {
    "cpu": 0.30,      # CPU usage weight
    "mem": 0.25,      # Memory usage weight
    "req": 0.20,      # Request rate weight
    "streams": 0.15,  # Concurrent streams weight
    "resp": 0.10,     # Response time weight
}

Tier Thresholds

Modify tier boundaries:

def sort_tier(score: float) -> str:
    if score < 25:    return "tier1"  # Low load
    elif score < 50:  return "tier2"  # Medium load
    elif score < 75:  return "tier3"  # High load
    else:             return "tier4"  # Critical load

WebSocket Update Interval

Change dashboard refresh rate (default: 1 second):

# In load_balancer.py WebSocket endpoint
await asyncio.sleep(1)  # Adjust interval here

🧪 Testing

Load Testing

# Install Apache Bench
sudo apt install apache2-utils

# Test with 100 concurrent connections, 1000 requests
ab -n 1000 -c 100 http://localhost/

# Monitor in real-time via dashboard
open http://localhost/dashboard

Health Checks

# Check load balancer
curl http://localhost/metrics

# Check individual servers
curl http://server1-ip:8080/metrics
curl http://server2-ip:8080/metrics
curl http://server3-ip:8080/metrics

# Check Prometheus targets
open http://localhost:9090/targets

📁 Project Structure

cdn-streaming-platform/
├── README.md
├── requirements.txt
├── docker-compose.yml
├── prometheus.yml
├── Dockerfile.loadbalancer
├── Dockerfile.videoserver
├── load_balancer.py
├── video_server.py
├── OrcaDocu.mp4
└── docs/
    └── architecture.md

🐳 Docker Commands

# Start services
docker-compose up -d

# Stop services
docker-compose down

# Rebuild after code changes
docker-compose up -d --build

# View logs
docker-compose logs -f [service_name]

# Scale video servers
docker-compose up -d --scale server=5

# Check service status
docker-compose ps

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages