AI-assisted edge cache optimization for content delivery networks.
SmartEdgeCDN is a simulation framework that demonstrates how machine learning can optimize cache selection at CDN edge points-of-presence (POPs) to reduce latency, improve hit rates, and lower egress costs.
CDNs distribute content globally through edge POPs, but cache capacity is limited. Traditional heuristics (LRU, popularity-based) don't anticipate future request patterns. Poor cache decisions lead to:
- Higher latency: Cache misses force origin fetches (4x slower)
- Increased costs: Origin egress is 10x more expensive than edge serving
- Lower hit rates: Suboptimal asset selection wastes capacity
SmartEdgeCDN uses a lightweight Logistic Regression classifier to predict which assets are likely to be requested soon, then selects cache contents to maximize expected hits within capacity constraints.
Key Innovation: Score assets by predicted_hit_probability / size_kb instead of just popularity / size_kb.
With deterministic simulation on 4,000 requests across 4 regions:
Policy Avg(ms) p95(ms) HitRate Cost($)
--------------------------------------------------
Base 142.1 220.0 0.41 0.153
AI 126.0 200.5 0.47 0.131
Δ(%) -11.3% -8.8% +14.6% -14.4%
Summary:
- 11.3% faster average response time
- 14.6% higher cache hit rate
- 14.4% lower egress costs
┌─────────────────────────────────────────────────────────┐
│ SmartEdgeCDN Pipeline │
└─────────────────────────────────────────────────────────┘
1. DATA GENERATION
┌──────────────┐
│ 600 Assets │ → size, type, popularity
└──────────────┘
↓
┌──────────────┐
│ 4000 Requests│ → timestamp, region, asset_id
└──────────────┘ + recent_hits feature
↓
┌──────────────┐
│ Train (60%) │
│ Test (40%) │
└──────────────┘
2. MODEL TRAINING
┌────────────────────────────────────┐
│ LogisticRegression │
│ Features: size, popularity, │
│ recent_hits, region │
│ Label: will_hit (next 50 requests) │
└────────────────────────────────────┘
3. CACHE SELECTION (per region)
Baseline: AI Policy:
┌──────────────┐ ┌──────────────┐
│ popularity │ │ pred_prob │
│ ────────── │ │ ────────── │
│ size_kb │ │ size_kb │
└──────────────┘ └──────────────┘
↓ ↓
┌──────────────────────────────┐
│ Greedy fill until 10MB │
└──────────────────────────────┘
4. SIMULATION
┌─────────────────────────────────┐
│ For each test request: │
│ • Check if asset in cache │
│ • Hit: latency/4, low cost │
│ • Miss: full latency, high cost│
└─────────────────────────────────┘
5. METRICS & VISUALIZATION
┌──────────────────────────────────┐
│ • Average & P95 latency │
│ • Cache hit rate │
│ • Total egress cost │
│ • Comparison plots (PNG) │
└──────────────────────────────────┘
git clone https://github.com/yourusername/smartedgecdn.git
cd smartedgecdn
make installOr with pip:
pip install -e .smartedgecdn compareThis runs the complete pipeline:
- Generates synthetic data
- Trains ML model
- Simulates both policies
- Outputs metrics and plots
After running, check the outputs/ directory:
outputs/
├── requests.csv # Generated request stream
├── metrics_baseline.csv # Baseline policy metrics
├── metrics_ai.csv # AI policy metrics
├── plot_latency.png # Latency comparison
├── plot_cache_hit.png # Hit rate comparison
└── plot_cost.png # Cost comparison
# Generate data only
smartedgecdn make-data
# Train model only
smartedgecdn train
# Run single policy
smartedgecdn simulate --policy baseline
smartedgecdn simulate --policy ai
# Full comparison (recommended)
smartedgecdn compareRun deterministic tests that verify AI improvements:
make testOr with pytest directly:
pytest -vKey test: test_end_to_end.py::test_ai_improves_over_baseline verifies:
- AI reduces avg latency by ≥5%
- AI improves hit rate by ≥5%
- AI reduces egress cost by ≥5%
smartedgecdn/
├── smartedgecdn/
│ ├── __init__.py
│ ├── config.py # Settings & configuration
│ ├── data.py # Synthetic data generation
│ ├── model.py # ML model training
│ ├── policies.py # Cache selection policies
│ ├── simulate.py # Simulation engine
│ ├── metrics.py # Performance metrics
│ ├── plot.py # Visualization
│ └── cli.py # Command-line interface
├── tests/
│ ├── test_data.py
│ ├── test_policies.py
│ ├── test_metrics.py
│ └── test_end_to_end.py
├── pyproject.toml
├── Makefile
├── README.md
└── LICENSE
Assets: 600 synthetic assets with realistic size distributions:
- Videos: 1-5 MB (20%)
- Images: 100-1000 KB (40%)
- HTML/JS/CSS: 50-500 KB (40%)
Requests: 4,000 requests with:
- Regional distribution (US-heavy: 60% US, 40% EU/Asia)
- Popularity-based sampling (Beta distribution)
- Temporal features (recent_hits rolling window)
Numeric features:
size_kb: Asset sizepopularity_score: Base popularity (0-1)recent_hits: Count of requests in last 10 timestamps
Categorical features:
region: One-hot encoded (US-West, US-East, Europe, Asia)
Label creation:
will_hit = 1if asset requested again within next 50 requests in same regionwill_hit = 0otherwise
LogisticRegression(
max_iter=1000,
class_weight="balanced", # Handle imbalance
random_state=42 # Reproducibility
)StandardScaler for numeric features + OneHotEncoder for regions.
Baseline Policy:
score = popularity_score / size_kb
# Select highest-scoring assets until capacityAI Policy:
score = predicted_hit_probability / size_kb
# Select highest-scoring assets until capacityBoth use greedy knapsack approach with 10 MB capacity per region.
For each request:
- Cache hit:
latency = base_latency / 4,cost = $0.00002/MB - Cache miss:
latency = base_latency,cost = $0.0002/MB
Base latencies by region:
- US-West: 80ms → 20ms (hit)
- US-East: 100ms → 25ms (hit)
- Europe: 180ms → 45ms (hit)
- Asia: 220ms → 55ms (hit)
- LRU Baseline: Implement time-based eviction policy
- Cooperative Caching: Share popularity signals across regions
- Prefetching: Proactively cache assets before requests
- Reinforcement Learning: Use contextual bandits for online learning
- Multi-objective: Balance latency, cost, and capacity jointly
- Real Data: Apply to CDN access logs (Cloudflare, Akamai)
- Neural Networks: Try LSTM for temporal patterns
- A/B Testing: Simulate gradual rollout with traffic splitting
Edit smartedgecdn/config.py to adjust:
- Number of assets/requests
- Region distributions
- Cache capacities
- Cost model parameters
- Model hyperparameters