Cloudflare Dynamic Workers — Serverless Có Trạng Thái Cho Kỷ Nguyên AI Agent
Posted on: 4/24/2026 6:10:35 PM
Table of contents
- Mục lục
- 1. Giới thiệu: Tại sao cần Stateful Serverless?
- 2. Dynamic Workers là gì?
- 3. Durable Object Facets: Cơ sở dữ liệu riêng cho mỗi Agent
- 4. Kiến trúc Supervisor — Facet
- 5. Sandboxes GA: Môi trường cô lập bền vững
- 6. Workers AI: 70+ model, 12+ provider qua một API
- 7. Toàn cảnh Agents Week 2026
- 8. So sánh: Dynamic Workers vs AWS Lambda vs Azure Durable Functions
- 9. Code mẫu thực tế
- 10. Chi phí và Free Tier
- 11. Kết luận
Mục lục
- Giới thiệu: Tại sao cần Stateful Serverless?
- Dynamic Workers là gì?
- Durable Object Facets: Cơ sở dữ liệu riêng cho mỗi Agent
- Kiến trúc Supervisor — Facet
- Sandboxes GA: Môi trường cô lập bền vững
- Workers AI: 70+ model, 12+ provider qua một API
- Toàn cảnh Agents Week 2026
- So sánh: Dynamic Workers vs AWS Lambda vs Durable Functions
- Code mẫu thực tế
- Chi phí và Free Tier
- Kết luận
1. Giới thiệu: Tại sao cần Stateful Serverless?
Serverless computing đã thay đổi cách chúng ta xây dựng ứng dụng — không cần quản lý server, tự động scale, chỉ trả tiền khi code chạy. Nhưng có một hạn chế lớn: stateless by default. Mỗi lần function chạy xong, toàn bộ trạng thái bị xóa sạch. Muốn lưu gì? Phải gọi ra database bên ngoài, thêm latency, thêm chi phí.
Với sự bùng nổ của AI Agent năm 2026, vấn đề này trở nên cấp bách hơn bao giờ hết. Một AI Agent cần:
- Persistent state — nhớ ngữ cảnh hội thoại, lưu kết quả trung gian
- Dynamic code execution — chạy code do AI tự sinh ra
- Isolation — an toàn, không ảnh hưởng lẫn nhau
- Long-running tasks — tác vụ kéo dài hàng phút, không chỉ vài giây
Cloudflare đã trả lời bài toán này tại Agents Week 2026 (13–17 tháng 4, 2026) với Dynamic Workers — một bước tiến lớn từ serverless stateless sang serverless có trạng thái.
2. Dynamic Workers là gì?
Dynamic Workers là isolate-based runtime cho phép AI Agent chạy code được sinh ra on-the-fly trong môi trường bảo mật. Khác với Workers truyền thống (deploy code cố định trước), Dynamic Workers nhận code tại runtime và thực thi ngay lập tức.
Điểm khác biệt cốt lõi
Workers truyền thống: bạn deploy code → Cloudflare chạy code đó. Dynamic Workers: AI Agent sinh code → gửi tới Cloudflare → chạy ngay trong isolate riêng biệt, có quyền truy cập storage riêng, database riêng, hoàn toàn cô lập với code của agent khác.
Đặc điểm kỹ thuật
- Isolate, không phải container — V8 isolate khởi động trong mili-giây, không cần boot OS hay runtime
- Auto-scaling vô hạn — từ 0 lên hàng triệu instance đồng thời, không cần cấu hình
- Persistent state tích hợp — trạng thái tồn tại qua các lần gọi và qua cả lần deploy mới
- Tích hợp native với D1 (SQL), R2 (Object Storage), KV (Key-Value), Queues
- Network control — có thể chặn hoàn toàn truy cập mạng (
globalOutbound: null)
3. Durable Object Facets: Cơ sở dữ liệu riêng cho mỗi Agent
Durable Object Facets là tính năng "game-changer" của Agents Week 2026. Ý tưởng: mỗi AI-generated app nhận được database SQLite riêng biệt, chạy ngay trên máy chủ cạnh (edge server) gần người dùng nhất, với zero-latency access vì dữ liệu nằm trên local disk.
graph TD
A[Request từ Client] --> B[Supervisor
Durable Object]
B --> C{Facet đã tồn tại?}
C -->|Có| D[Forward tới Facet]
C -->|Không| E[Tạo Facet mới]
E --> F[Load Dynamic Worker Code]
F --> G[Khởi tạo Durable Object Class]
G --> D
D --> H[Facet xử lý request]
H --> I[(SQLite DB
của Facet)]
B --> J[(SQLite DB
của Supervisor)]
style A fill:#f8f9fa,stroke:#e94560,color:#2c3e50
style B fill:#e94560,stroke:#fff,color:#fff
style D fill:#4CAF50,stroke:#fff,color:#fff
style I fill:#2c3e50,stroke:#fff,color:#fff
style J fill:#2c3e50,stroke:#fff,color:#fff
Luồng xử lý request trong kiến trúc Supervisor — Facet
Dual Storage Model
Mỗi Durable Object chứa hai database SQLite tách biệt:
- Supervisor DB — lưu logs, metrics, billing, cấu hình hệ thống. Chỉ supervisor truy cập được.
- Facet DB — lưu dữ liệu ứng dụng của agent. Chỉ facet truy cập được.
Tại sao dùng SQLite thay vì database tập trung?
SQLite chạy trên cùng máy với Durable Object → không có network round-trip. Truy vấn với latency gần như 0. Mỗi agent có DB riêng → không lo contention. Cloudflare tự xử lý replication và backup.
4. Kiến trúc Supervisor — Facet
Kiến trúc hai tầng Supervisor — Facet là design pattern trung tâm của Dynamic Workers:
Supervisor (Parent Durable Object)
- Nhận request đầu tiên từ client
- Xử lý cross-cutting concerns: logging, metrics, billing, rate limiting
- Quyết định facet nào sẽ xử lý request
- Kiểm soát tài nguyên: giới hạn storage, CPU, network
Facet (Child Durable Object)
- Chứa business logic — code do AI sinh ra hoặc user upload
- Có database SQLite riêng, truy cập qua
storage.kvvàstorage.sql - Hoàn toàn cô lập — không thể truy cập DB của supervisor hoặc facet khác
- "Disposable" — có thể tạo để chạy vài dòng code rồi bỏ đi
sequenceDiagram
participant C as Client
participant S as Supervisor
participant F as Facet
participant DB as SQLite (Facet)
C->>S: HTTP Request
S->>S: Log request, check rate limit
S->>F: this.ctx.facets.get("app")
Note over F: Load Dynamic Worker code
nếu chưa khởi tạo
F->>DB: storage.sql.exec("SELECT...")
DB-->>F: Query results
F-->>S: Response
S->>S: Log response, update metrics
S-->>C: HTTP Response
Sequence diagram: Request flow qua Supervisor → Facet
5. Sandboxes GA: Môi trường cô lập bền vững
Song song với Dynamic Workers, Cloudflare cũng chính thức GA (General Availability) cho Sandboxes — môi trường cô lập bền vững nơi AI Agent có thể:
- Shell access — chạy lệnh terminal như trong VPS thực
- Filesystem — đọc ghi file, cài đặt package
- Background processes — chạy server, watcher, build tool
- State persistence — resume từ trạng thái trước đó khi quay lại
Dynamic Workers vs Sandboxes — khi nào dùng gì?
Dynamic Workers: cho code nhẹ, xử lý request nhanh (API handler, data transform), cần khởi động cực nhanh (sub-ms). Sandboxes: cho tác vụ phức tạp cần full environment (build project, cài dependencies, chạy test suite), cần shell access thực sự.
6. Workers AI: 70+ model, 12+ provider qua một API
AI Gateway của Cloudflare đã phát triển thành unified inference layer cho toàn bộ hệ sinh thái AI:
Các model nổi bật trên Workers AI
| Model | Tham số | P50 Latency | Use case |
|---|---|---|---|
| Llama 4 Scout | 109B | 45ms | Chat, reasoning, code generation |
| Mistral Medium 3 | 73B | 62ms | Multilingual, analysis |
| SDXL Turbo | — | 890ms | Image generation |
| Whisper Large V3 | 1.5B | ~200ms | Speech-to-text |
AI Security Suite
Cloudflare đồng thời ra mắt bộ bảo mật AI hoàn chỉnh:
- AI Firewall — phát hiện prompt injection với độ chính xác 96.3%, false positive dưới 0.1%
- AI WAF — tích hợp phát hiện prompt injection vào Web Application Firewall
- AI Gateway — proxy giữa ứng dụng và AI provider: caching, rate limiting, cost control
Thống kê Q1/2026: 78% tấn công là prompt injection, 12% data exfiltration, 7% model abuse.
7. Toàn cảnh Agents Week 2026
Agents Week 2026 (13–17/04/2026) là tuần lễ công bố lớn nhất của Cloudflare dành cho nền tảng AI Agent:
8. So sánh: Dynamic Workers vs AWS Lambda vs Azure Durable Functions
| Tiêu chí | Dynamic Workers | AWS Lambda | Azure Durable Functions |
|---|---|---|---|
| Cold start | Sub-millisecond | 100ms – 10s (tuỳ runtime) | 1–5s |
| Stateful | Built-in (Durable Objects) | Không (cần DynamoDB/S3) | Có (via orchestration) |
| Dynamic code | Có (core feature) | Không | Không |
| Max duration | 30 phút | 15 phút | Không giới hạn |
| Database tích hợp | SQLite (co-located) | Không (cần RDS/DynamoDB) | Không (cần Azure SQL) |
| Edge deployment | 330+ PoPs tự động | Chọn region cụ thể | Chọn region cụ thể |
| Egress cost | $0 (miễn phí) | $0.09/GB | $0.087/GB |
| AI Agent support | Native (Facets, Memory, Think) | Cần tự build | Cần tự build |
Nhận định
Dynamic Workers không thay thế Lambda hay Durable Functions trong mọi trường hợp. Nhưng cho AI Agent workloads — nơi cần dynamic code execution, persistent state, và edge latency — nó hiện là giải pháp tích hợp nhất trên thị trường, đặc biệt với chi phí egress bằng 0.
9. Code mẫu thực tế
9.1 Supervisor + Facet cơ bản
// Supervisor — quản lý lifecycle của Facet
export class AppRunner extends DurableObject {
async fetch(request) {
// Lấy hoặc tạo facet
let facet = this.ctx.facets.get("app", async () => {
// Load Dynamic Worker code (có thể từ AI-generated source)
let worker = this.#loadDynamicWorker();
let appClass = worker.getDurableObjectClass("App");
return { class: appClass };
});
// Forward request tới facet
return await facet.fetch(request);
}
}
// Facet — business logic với persistent state
export class App extends DurableObject {
async fetch(request) {
// Mỗi facet có SQLite DB riêng
let counter = (await this.ctx.storage.get("counter")) || 0;
counter++;
await this.ctx.storage.put("counter", counter);
return new Response(JSON.stringify({
message: `Request thứ ${counter}`,
timestamp: new Date().toISOString()
}), {
headers: { "Content-Type": "application/json" }
});
}
}
9.2 Workers AI — Gọi model qua AI Gateway
export default {
async fetch(request, env) {
const { messages } = await request.json();
const response = await env.AI.run(
"@cf/meta/llama-4-scout-17b-16e-instruct",
{
messages,
max_tokens: 1024,
temperature: 0.7
}
);
return Response.json(response);
}
};
9.3 Cấu hình wrangler.toml cho Dynamic Workers
name = "my-agent-platform"
main = "src/index.ts"
compatibility_date = "2026-04-15"
[durable_objects]
bindings = [
{ name = "APP_RUNNER", class_name = "AppRunner" }
]
[[migrations]]
tag = "v1"
new_classes = ["AppRunner"]
[ai]
binding = "AI"
10. Chi phí và Free Tier
| Plan | Giá | Worker Requests | Bao gồm |
|---|---|---|---|
| Free | $0 | 100K/ngày | Workers, KV, R2 (10GB), D1 (5GB), basic WAF |
| Pro | $20/tháng | 10M/tháng | + Enhanced WAF, Analytics |
| Business | $200/tháng | 50M/tháng | + 100% Uptime SLA |
| Enterprise | Custom | Custom | + Dedicated support, SLA tùy chỉnh |
Free Tier đáng chú ý
100K requests/ngày miễn phí là đủ cho prototype và side project. Kết hợp D1 (5GB free), R2 (10GB free, 0 egress), KV (100K reads/ngày free) — bạn có thể xây dựng một AI Agent platform hoàn chỉnh với chi phí $0.
11. Kết luận
Agents Week 2026 đánh dấu bước ngoặt quan trọng của Cloudflare: từ CDN + WAF → full-stack cloud platform cho AI Agent. Dynamic Workers giải quyết bài toán mà serverless truyền thống đã loay hoay nhiều năm — chạy code động với trạng thái bền vững, trong môi trường bảo mật, tại edge gần người dùng nhất.
Với Durable Object Facets, mỗi AI Agent có database riêng, cô lập hoàn toàn, zero-latency. Với Sandboxes GA, agent có môi trường đầy đủ để build, test, deploy. Với Workers AI + AI Gateway, agent truy cập 70+ model qua một API duy nhất.
Nếu bạn đang xây dựng AI Agent hoặc platform cho AI Agent, Cloudflare Workers là lựa chọn đáng xem xét nghiêm túc — đặc biệt khi free tier đã đủ mạnh cho hầu hết use case prototype và production nhỏ.
Tham khảo
- Durable Objects in Dynamic Workers: Give each AI-generated app its own database — Cloudflare Blog
- Agents Week 2026 Updates and Announcements — Cloudflare
- Project Think: building the next generation of AI agents on Cloudflare — Cloudflare Blog
- Cloudflare Launches Dynamic Workers and AI Security — Cloudswitched
- Cloudflare Agents Week 2026: Every Release Explained — Lushbinary
DevSecOps — Tích hợp bảo mật vào Pipeline CI/CD hiện đại
Feature Flags và Progressive Delivery: Release an toàn cho Production
Disclaimer: The opinions expressed in this blog are solely my own and do not reflect the views or opinions of my employer or any affiliated organizations. The content provided is for informational and educational purposes only and should not be taken as professional advice. While I strive to provide accurate and up-to-date information, I make no warranties or guarantees about the completeness, reliability, or accuracy of the content. Readers are encouraged to verify the information and seek independent advice as needed. I disclaim any liability for decisions or actions taken based on the content of this blog.