CDN (Content Delivery Network), web içeriğini kullanıcılara en hızlı şekilde ulaştırmak için tasarlanmış dağıtık bir ağ altyapısıdır. Bu yazıda, CDN'in nasıl çalıştığını lab ortamında inceleyeceğiz.
CDN Nedir?
CDN, statik içerikleri (images, CSS, JS, video) coğrafi olarak dağıtık sunucularda (edge servers) cache'leyerek kullanıcılara en yakın lokasyondan sunmayı sağlar.
Temel Bileşenler
- Origin Server: Orijinal içeriğin bulunduğu sunucu
- Edge Servers: İçeriğin cache'lendiği dağıtık sunucular
- PoP (Point of Presence): Edge server'ların bulunduğu data center'lar
- DNS: Kullanıcıyı en yakın edge server'a yönlendiren sistem
Lab Ortamı Kurulumu
Docker ile CDN Simülasyonu
# Origin server (Nginx)
docker run -d --name origin-server \
-p 8080:80 \
-v $(pwd)/content:/usr/share/nginx/html \
nginx:alpine
# Edge server 1 (Istanbul)
docker run -d --name edge-istanbul \
-p 8081:80 \
-e ORIGIN_URL=http://origin-server:80 \
nginx:alpine
# Edge server 2 (Frankfurt)
docker run -d --name edge-frankfurt \
-p 8082:80 \
-e ORIGIN_URL=http://origin-server:80 \
nginx:alpine
Nginx Cache Konfigürasyonu
http {
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m;
server {
listen 80;
server_name edge.example.com;
location / {
proxy_cache my_cache;
proxy_cache_valid 200 60m;
proxy_cache_key "$scheme$request_method$host$request_uri";
add_header X-Cache-Status $upstream_cache_status;
proxy_pass http://origin-server;
}
}
}
Cache Stratejileri
Cache-Control Headers
# Statik dosyalar için uzun süreli cache
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
# HTML için kısa süreli cache
location ~* \.(html)$ {
expires 5m;
add_header Cache-Control "public, must-revalidate";
}
Purge/Invalidation
# Cache'i temizle (Cloudflare API)
curl -X POST "https://api.cloudflare.com/client/v4/zones/{zone_id}/purge_cache" \
-H "Authorization: Bearer {api_token}" \
-H "Content-Type: application/json" \
--data '{"files":["https://example.com/image.jpg"]}'
Gerçek Dünya Testi
RTT (Round Trip Time) Karşılaştırması
# CDN kullanmadan
time curl -o /dev/null -s -w "%{time_total}\n" http://origin-server.com/image.jpg
# Sonuç: 450ms
# CDN ile
time curl -o /dev/null -s -w "%{time_total}\n" http://cdn.example.com/image.jpg
# Sonuç: 45ms
Cache Hit Ratio
# Nginx cache stats
curl -s http://edge-server/nginx_status | grep cache
# cache_hits: 9876
# cache_misses: 234
# cache_hit_ratio: 97.7%
CloudFlare Workers
Edge'de JavaScript çalıştırma:
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const cache = caches.default
let response = await cache.match(request)
if (!response) {
response = await fetch(request)
// Cache for 1 hour
const headers = new Headers(response.headers)
headers.set('Cache-Control', 'max-age=3600')
response = new Response(response.body, {
status: response.status,
headers: headers
})
event.waitUntil(cache.put(request, response.clone()))
}
return response
}
Load Balancing
GeoDNS ile Routing
; Istanbul kullanıcıları için
edge-tr.example.com. 300 IN A 185.10.10.10
; Avrupa kullanıcıları için
edge-eu.example.com. 300 IN A 195.20.20.20
; Global fallback
edge.example.com. 300 IN CNAME edge-eu.example.com.
Monitoring
Grafana Dashboard
# Cache hit rate
rate(nginx_http_cache_hits_total[5m]) /
rate(nginx_http_cache_total[5m]) * 100
# Average response time
histogram_quantile(0.95,
rate(nginx_http_request_duration_seconds_bucket[5m]))
# Bandwidth saved
(rate(nginx_http_cache_hits_total[5m]) * avg(response_size)) / 1024 / 1024
Best Practices
- Aggressive Caching: Statik içerikleri uzun süre cache'le
- Version Control: Dosya adlarına hash ekle (app.v1.2.3.css)
- Gzip Compression: Tüm text-based dosyaları sıkıştır
- HTTP/2: Multiplexing ve server push kullan
- Image Optimization: WebP formatını tercih et
Maliyet Analizi
CDN kullanımı ile:
- Origin sunucu yükü: %95 azalma
- Bandwidth maliyeti: %70 azalma
- TTFB (Time to First Byte): %80 iyileşme
- Sayfa yükleme hızı: 3x daha hızlı
Sonuç
CDN, modern web uygulamaları için vazgeçilmezdir. Doğru cache stratejileri ve edge computing ile hem performans hem de maliyet optimizasyonu sağlanabilir.
"Every millisecond counts in user experience. CDN makes those milliseconds matter."