Zum Inhalt

⚑ Edge Cache Architecture

Übersicht

Optionaler Cache-Layer auf Cloudflare Workers fΓΌr globale Performance.

Architektur

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Client  β”‚ ──── β”‚ CF Worker (Edge) β”‚ ──── β”‚ Backend API β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚                    β”‚
    β”‚                    β–Ό
    β”‚            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚            β”‚   KV Store   β”‚
    β”‚            β”‚  (Cache)     β”‚
    β”‚            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    └── Response: ~10-50ms (Edge)
        vs ~100-500ms (Direct)

Vorteile

Aspekt Ohne Edge Mit Edge Cache
Latenz 100-500ms 10-50ms
Global Single Region 200+ Locations
Backend Load Hoch Reduziert
Kosten Pay per Request 100k free/day

Implementation

Worker Code (edge/worker.ts)

export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const cacheKey = getCacheKey(request);

    // Cache Hit?
    const cached = await env.CACHE.get(cacheKey, 'json');
    if (cached) {
      return new Response(JSON.stringify(cached.data), {
        headers: { 'X-Cache': 'HIT' }
      });
    }

    // Cache Miss β†’ Backend
    const response = await fetch(env.BACKEND_URL + path);
    const data = await response.json();

    // Store in Cache
    await env.CACHE.put(cacheKey, JSON.stringify(data), {
      expirationTtl: ttl
    });

    return new Response(JSON.stringify(data), {
      headers: { 'X-Cache': 'MISS' }
    });
  }
};

Setup

1. Wrangler CLI installieren

npm install -g wrangler
wrangler login

2. KV Namespace erstellen

wrangler kv:namespace create "CACHE"

3. wrangler.toml konfigurieren

name = "contract-platform-cache"
main = "edge/worker.ts"

[vars]
BACKEND_URL = "https://api.your-domain.com"

[[kv_namespaces]]
binding = "CACHE"
id = "your-kv-namespace-id"

4. Deploy

cd edge
wrangler deploy

Cache Strategie

TTL pro Route

Route Pattern TTL Grund
/api/properties 1h Statisch
/api/contracts 30min Selten geΓ€ndert
/api/integrations/evcc/* 10s Realtime
/api/integrations/sevdesk/* 10min Rate Limits

Cache Invalidation

Optionen fΓΌr Cache-Invalidierung:

  1. TTL Expiry - Automatisch nach TTL
  2. API Call - Worker Endpoint /cache/purge
  3. KV API - Direkt ΓΌber Wrangler CLI
# Manuell lΓΆschen
wrangler kv:key delete --binding=CACHE "cache:/api/contracts"

Monitoring

Headers prΓΌfen

curl -I https://your-edge.workers.dev/api/contracts
X-Cache: HIT
X-Cache-Age: 45
X-Cache-TTL: 1800
CF-Ray: abc123-FRA

Wrangler Logs

wrangler tail

Best Practices

Stale-While-Revalidate

FΓΌr bessere UX: Alte Daten sofort liefern, im Hintergrund refreshen.

Keine sensitiven Daten cachen

User-spezifische Daten nie am Edge cachen!

Cache Key Design

Tenant-ID in Cache Key einbauen fΓΌr Multi-Tenant Isolation.