Netlify Edge Chaos Engineering
Netlify Edge Functions Serverless Deno Runtime Edge Network Chaos Engineering Inject Failures Resilience Testing Steady State Hypothesis Chaos Monkey Netflix
| Tool | ประเภท | Platform | ใช้ทดสอบ |
|---|---|---|---|
| Chaos Monkey | Instance Termination | AWS | Server Failure |
| Gremlin | Full Platform | Any | CPU, Network, Disk |
| LitmusChaos | Kubernetes Native | K8s | Pod, Node, Network |
| Chaos Toolkit | Open Source | Any | Custom Experiments |
| AWS FIS | AWS Native | AWS | EC2, ECS, RDS |
Netlify Edge Functions
# === Netlify Edge Functions Setup ===
# Project Structure
# my-site/
# netlify/
# edge-functions/
# geo-redirect.ts
# ab-test.ts
# auth-check.ts
# netlify.toml
# public/
# index.html
# netlify.toml
# [[edge_functions]]
# path = "/api/*"
# function = "api-handler"
#
# [[edge_functions]]
# path = "/*"
# function = "geo-redirect"
# netlify/edge-functions/geo-redirect.ts
# import type { Context } from "https://edge.netlify.com";
#
# export default async (request: Request, context: Context) => {
# const country = context.geo.country?.code || "US";
# const city = context.geo.city || "Unknown";
#
# // Redirect Thai users to Thai version
# if (country === "TH" && !request.url.includes("/th/")) {
# const url = new URL(request.url);
# url.pathname = `/th`;
# return Response.redirect(url.toString(), 302);
# }
#
# // Add geo headers
# const response = await context.next();
# response.headers.set("X-Country", country);
# response.headers.set("X-City", city);
# return response;
# };
# netlify/edge-functions/ab-test.ts
# export default async (request: Request, context: Context) => {
# const cookie = request.headers.get("cookie") || "";
# let variant = cookie.includes("ab_variant=B") ? "B" : "A";
#
# if (!cookie.includes("ab_variant")) {
# variant = Math.random() < 0.5 ? "A" : "B";
# }
#
# const response = await context.next();
# const html = await response.text();
# const modified = html.replace(
# "",
# variant === "B" ? "" : ""
# );
#
# return new Response(modified, {
# headers: {
# ...Object.fromEntries(response.headers),
# "Set-Cookie": `ab_variant=; Path=/; Max-Age=86400`,
# },
# });
# };
from dataclasses import dataclass
from typing import List
@dataclass
class EdgeFunction:
name: str
path: str
runtime: str
avg_latency_ms: float
use_case: str
functions = [
EdgeFunction("geo-redirect", "/*", "Deno", 2, "Geolocation Redirect"),
EdgeFunction("ab-test", "/landing/*", "Deno", 5, "A/B Testing"),
EdgeFunction("auth-check", "/dashboard/*", "Deno", 8, "JWT Verification"),
EdgeFunction("rate-limit", "/api/*", "Deno", 3, "Rate Limiting"),
EdgeFunction("cache-control", "/*", "Deno", 1, "Dynamic Cache Headers"),
]
print("Netlify Edge Functions:")
for f in functions:
print(f" {f.name} [{f.path}] — {f.avg_latency_ms}ms — {f.use_case}")
Chaos Engineering Experiments
# === Chaos Engineering Experiments ===
# Chaos Toolkit — Install
# pip install chaostoolkit chaostoolkit-lib
# experiment.json
# {
# "title": "Edge Function Latency Resilience",
# "description": "Verify system handles edge latency spikes",
# "steady-state-hypothesis": {
# "title": "Response time under 500ms",
# "probes": [
# {
# "type": "probe",
# "name": "response-time-check",
# "provider": {
# "type": "http",
# "url": "https://mysite.netlify.app/api/health",
# "timeout": 0.5
# },
# "tolerance": {
# "type": "range",
# "range": [200, 299]
# }
# }
# ]
# },
# "method": [
# {
# "type": "action",
# "name": "inject-latency",
# "provider": {
# "type": "process",
# "path": "tc",
# "arguments": "qdisc add dev eth0 root netem delay 200ms 50ms"
# }
# }
# ],
# "rollbacks": [
# {
# "type": "action",
# "name": "remove-latency",
# "provider": {
# "type": "process",
# "path": "tc",
# "arguments": "qdisc del dev eth0 root"
# }
# }
# ]
# }
#
# chaos run experiment.json
from dataclasses import dataclass, field
from typing import List, Dict
from enum import Enum
class ExperimentResult(Enum):
PASSED = "passed"
FAILED = "failed"
ERROR = "error"
@dataclass
class ChaosExperiment:
name: str
target: str
failure_type: str
steady_state: str
result: ExperimentResult
findings: str
experiments = [
ChaosExperiment(
"Edge Latency Spike", "Edge Functions",
"Add 200ms latency to origin",
"Response < 500ms, Error rate < 1%",
ExperimentResult.PASSED,
"Edge cache served stale content, users unaffected"),
ChaosExperiment(
"Origin Server Down", "Origin Server",
"Kill origin server process",
"Site still accessible via CDN cache",
ExperimentResult.PASSED,
"CDN served cached pages, API returned 503 with fallback"),
ChaosExperiment(
"DNS Failure", "DNS Provider",
"Block DNS resolution",
"Site accessible via cached DNS",
ExperimentResult.FAILED,
"No DNS fallback, site down for 5 minutes. Action: Add secondary DNS"),
ChaosExperiment(
"Cache Purge Storm", "CDN Cache",
"Purge all CDN cache simultaneously",
"Origin handles traffic spike",
ExperimentResult.PASSED,
"Origin auto-scaled, slight latency increase 200ms -> 800ms"),
ChaosExperiment(
"Rate Limit Bypass", "Edge Rate Limiter",
"Send 10x normal traffic",
"Rate limiter blocks excess requests",
ExperimentResult.PASSED,
"Rate limiter activated at 1000 req/s, returned 429"),
]
print("=== Chaos Experiments ===\n")
for exp in experiments:
status = "PASS" if exp.result == ExperimentResult.PASSED else "FAIL"
print(f" [{status}] {exp.name}")
print(f" Target: {exp.target} | Failure: {exp.failure_type}")
print(f" Steady State: {exp.steady_state}")
print(f" Findings: {exp.findings}\n")
Resilience Patterns
# === Resilience Patterns for Edge ===
patterns = {
"Circuit Breaker": {
"desc": "หยุดเรียก Service ที่ล้มเหลว ป้องกัน Cascade Failure",
"states": "Closed -> Open (fail) -> Half-Open (test) -> Closed",
"edge_use": "Origin server down, switch to cached response",
},
"Retry with Backoff": {
"desc": "ลองใหม่เมื่อล้มเหลว รอนานขึ้นเรื่อยๆ",
"states": "1s -> 2s -> 4s -> 8s -> give up",
"edge_use": "API timeout, retry with exponential backoff",
},
"Fallback": {
"desc": "ใช้ค่า Default เมื่อ Service ล้มเหลว",
"states": "Primary -> Secondary -> Static Fallback",
"edge_use": "Origin down, serve cached/static version",
},
"Bulkhead": {
"desc": "แยก Resource Pool ป้องกัน Failure แพร่กระจาย",
"states": "Pool A (API) | Pool B (Static) | Pool C (Auth)",
"edge_use": "API failure ไม่กระทบ Static serving",
},
"Rate Limiting": {
"desc": "จำกัดจำนวน Request ป้องกัน Overload",
"states": "Token Bucket / Sliding Window",
"edge_use": "Edge function rate limit per IP/region",
},
}
print("Resilience Patterns:")
for pattern, info in patterns.items():
print(f"\n [{pattern}]")
for k, v in info.items():
print(f" {k}: {v}")
# Monitoring & Alerting
monitoring = [
"Edge Function Latency P50/P95/P99",
"Error Rate per Edge Location",
"Origin Response Time",
"Cache Hit Ratio",
"Rate Limit Trigger Count",
"Circuit Breaker State Changes",
"DNS Resolution Time",
]
print(f"\n\nMonitoring Metrics:")
for i, m in enumerate(monitoring, 1):
print(f" {i}. {m}")
เคล็ดลับ
- Start Small: เริ่มจาก Staging ก่อน แล้วค่อยทดสอบ Production
- Steady State: กำหนด Hypothesis ชัดเจนก่อนทดสอบ
- Rollback: เตรียม Rollback Plan ทุกครั้ง
- Blast Radius: จำกัดขอบเขตผลกระทบ ทดสอบทีละส่วน
- GameDay: จัดทีม GameDay ทดสอบ Chaos Engineering เป็นประจำ
Netlify Edge Functions คืออะไร
Serverless Functions Edge Network Deno Runtime TypeScript Latency ต่ำ A/B Testing Geolocation Auth Personalization ไม่ Cold Start
Chaos Engineering คืออะไร
ทดสอบความทนทาน Inject Failures Production Staging ปิด Server เพิ่ม Latency ตัด Network Netflix Chaos Monkey Steady State Hypothesis
Chaos Monkey คืออะไร
Netflix สุ่มปิด Instance Production ทดสอบทนทาน Simian Army Latency Monkey Conformity Monkey Chaos Gorilla Availability Zone
ใช้ Chaos Engineering กับ Edge Functions อย่างไร
Inject Latency Timeout Handling Origin Failure Fallback Cache Invalidation Rate Limiting Geographic Failover Gremlin LitmusChaos
สรุป
Netlify Edge Functions Deno Serverless Chaos Engineering Inject Failures Chaos Monkey Gremlin LitmusChaos Circuit Breaker Retry Fallback Bulkhead Rate Limiting Resilience Testing GameDay
