Cloudflare Developer Platform Handbook
Chuỗi bài về Cloudflare developer platform: Workers, D1, R2, KV, Queues, Durable Objects, Workers AI, Vectorize. Kinh nghiệm thực tế từ blog này, không phải tóm tắt docs.
20 bài · ~1 bài/tuần · RSS
- 01 Cloudflare developer platform là gì, và vì sao khác Lambda
Cloudflare không còn chỉ là CDN. Workers, D1, R2, KV, Queues, DOs, Workers AI, Vectorize tạo thành nền tảng edge-native. Mental model đầu tiên, so sánh với Lambda.
- 02 Workers runtime mental model: lifecycle, context, limit
fetch handler, ExecutionContext, waitUntil, giới hạn subrequest, CPU vs wall time, cold start thực tế. 6 ngộ nhận khi dev từ Node/Lambda sang Workers. Code mẫu từ blog này.
- 03 Mental model 3 tầng binding: Request, Identity, Storage
Khung tư duy chung cho mọi Worker: Request là cửa vào, Identity là ai đang gọi, Storage là đọc ghi đâu. Áp dụng vào Worker đang chạy blog này và cách chọn storage primitive đúng.
- 04 Wrangler và Miniflare dev loop: từ init tới deploy trong 30 phút
Dev loop thực tế của Workers: wrangler init, dev local với Miniflare, vitest, D1 migration, secret, triển khai 300+ PoP trong 30 giây. Vòng đời từ file trống đến production.
- 05 KV deep-dive: cache toàn cầu, eventual consistency, vs D1
Cloudflare KV là eventually-consistent KV store với cache tại từng PoP. Consistency model thực tế, giới hạn, 5 pattern đúng, 3 anti-pattern phổ biến, và gotcha thực tế.
- 06 D1 trong production: primary-replica, batch, và 7 gotcha
D1 là SQLite ở edge với primary region và read replica: kiến trúc, 5 query method, Sessions API cho read replica, prepared statement cache, migration, và 7 gotcha production.
- 07 R2 object storage: S3-compat, egress free, và 4 access pattern
R2 là object storage S3-compatible của Cloudflare, không phí egress. So sánh R2 vs S3, 4 pattern phục vụ object, migration từ S3, gotcha về consistency, metadata, lifecycle.
- 08 Queues và Durable Objects: async messaging và single-writer state
2 primitive khó nhất khi Worker cần state. Queues cho fire-and-forget job với retry và DLQ. Durable Objects cho single-writer coordination. Khi nào dùng cái nào, pattern và gotcha.
- 09 Router cho Workers: vanilla, Itty, hay Hono
3 lựa chọn: vanilla fetch (0 bundle), Itty (3KB), Hono (13KB). Cú pháp, chuỗi middleware, validate Zod, khi nào chọn cái nào, và vì sao blog này dùng vanilla dù có 40 route.
- 10 ORM cho D1: Drizzle, Prisma, hay raw SQL
3 lựa chọn: SQL thô (0KB), Drizzle (10KB, TS-first), Prisma (500KB WASM). Quy trình, pattern query phức tạp, migration, type safety, và khi nào ORM hại nhiều hơn lợi.
- 11 Astro, Remix, SvelteKit trên Workers: adapter và trade-off
3 framework full-stack trên Workers khác nhau về render, JS client, adapter và bindings. Thiết lập thực tế từng cái, SSG vs SSR vs hybrid, và vì sao blog này chọn Astro.
- 12 CI/CD với Wrangler + GitHub Actions: pipeline, smoke test
Pipeline 4 bước: test → build → deploy → smoke. Scoped API token, smoke test 19 assertion, concurrent lock, preview env, rollback 10 giây. Full workflow file từ blog này.
- 13 Workers AI + AI Gateway: catalog, pricing, vs Bedrock/OpenAI
Workers AI chạy inference trên edge GPU, AI Gateway proxy OpenAI/Anthropic/Bedrock/Google với cache + rate limit. Catalog, giá, khi nào chọn cái nào, retry/fallback production.
- 14 Vectorize + RAG: embeddings, top-K, hybrid search edge
Vectorize là vector DB native của Cloudflare, kết hợp Workers AI bge-m3 cho RAG trọn edge. Pipeline ingest + query, chunking, lọc metadata, hybrid search D1, reranking.
- 15 Durable Objects cho realtime: chat, collab, game state
Durable Object là single-writer coordination của Cloudflare: 1 roomId = 1 instance, WebSocket hibernation, storage persistent. 6 pattern, API cốt lõi, và khi nào DO là quá mức.
- 16 Stream và Images: media pipeline ở edge, khi nào dùng product nào
3 cách xử lý media của Cloudflare: Stream cho video (HLS/DASH), Images cho upload-transform-deliver, Image Resizing / cf.image. Pipeline, giá, responsive, OG động.
- 17 Observability cho Worker: Logs, Tail Workers, Analytics
4 tầng observability Cloudflare: Workers Logs (retention 3 ngày), Tail Workers (realtime), Logpush (batch tới R2/SIEM), Analytics Engine. Structured logging, alert, debug prod.
- 18 Security cho Worker: secrets, CSP, Bot Management, Turnstile
Defense-in-depth cho Cloudflare Worker: WAF + Bot Management, Turnstile, Access JWT, secret management, CSP/HSTS, 4 pattern auth, validation Zod, và anti-pattern cần tránh.
- 19 Cost model Cloudflare Developer Platform: tier, so sánh AWS
Pricing từng primitive Cloudflare (Workers, D1, KV, R2, Queues, DOs, Vectorize, Workers AI), breakpoint, so sánh AWS, 3 scenario: blog, SaaS 10k user, app 100M req/tháng.
- 20 Migration AWS/Vercel sang Cloudflare: playbook thực tế
Playbook migrate production từ AWS (Lambda, DynamoDB, RDS, S3, SQS, ElastiCache) sang Cloudflare: mapping primitive, 3 chiến lược, data migration, cutover, rollback, 10 pitfall.