Workers Workers Workers Cơ bản Entry កម្រិតចាប់ផ្តើម ~7 phút đọc · ~655 từ ~7 min read · ~587 words

D1 là gì? Database SQL nhẹ cho app nhỏ trên Workers What is D1? Lightweight SQL for small apps on Workers What is D1? Lightweight SQL for small apps on Workers

D1 là SQLite trên edge — phù hợp bảng quan hệ nhỏ (signup, quiz, metadata) gắn với Workers. Không thay Postgres lớn, nhưng rất tiện cho side project và MVP. D1 is SQLite at the edge — great for small relational tables (signups, quizzes, metadata) tied to Workers. It does not replace a large Postgres, but it is ideal for side projects and MVPs. D1 is SQLite at the edge — great for small relational tables (signups, quizzes, metadata) tied to Workers. It does not replace a large Postgres, but it is ideal for side projects and MVPs.

Hình 1: Cloudflare Developer Platform

Ứng dụng fullstack Fullstack applications Fullstack applications

Ví dụ fullstack trên Developer Platform — Workers/Pages, storage và AI services trong một kiến trúc thực tế. A practical example of how these services come together in a real fullstack application architecture. A practical example of how these services come together in a real fullstack application architecture.

Thuật ngữ: Concepts: Concepts: Workers · Pages · D1 · R2 · KV · Durable Objects

Sơ đồ chính thức ↗ Official diagram ↗ Official diagram ↗ · Serverless Serverless Serverless

Khi nào app cần SQL thay vì chỉ KV hoặc R2? When does an app need SQL instead of only KV or R2? When does an app need SQL instead of only KV or R2?

Workers KV giống tủ khóa–giá trị: đọc nhanh theo key, không có JOIN hay query phức tạp. R2 lưu file (ảnh, PDF, backup). Nhưng nhiều tính năng cần bảng quan hệ: “mỗi user có nhiều đăng ký workshop”, “đếm số người theo sự kiện”, “lọc bình luận theo ngày”. Đó là lúc SQL hợp lý. Workers KV is like a key–value locker: fast reads by key, no JOINs or complex queries. R2 stores files (images, PDFs, backups). Many features need relational tables: “each user has many workshop signups,” “count attendees per event,” “filter comments by date.” That is when SQL makes sense. Workers KV is like a key–value locker: fast reads by key, no JOINs or complex queries. R2 stores files (images, PDFs, backups). Many features need relational tables: “each user has many workshop signups,” “count attendees per event,” “filter comments by date.” That is when SQL makes sense.

D1 là dịch vụ SQL của Cloudflare, dựa trên SQLite, gắn với Workers qua binding. Các bài trên blog.cloudflare.com về D1 thường nhấn mạnh: bạn viết SQL quen thuộc, chạy gần logic ứng dụng trên edge — không phải tự dựng cluster database chỉ để lưu vài nghìn dòng. D1 is Cloudflare’s SQL service, built on SQLite, bound to Workers. D1 posts on blog.cloudflare.com often stress: you write familiar SQL, run it near app logic at the edge — without building a database cluster for a few thousand rows. D1 is Cloudflare’s SQL service, built on SQLite, bound to Workers. D1 posts on blog.cloudflare.com often stress: you write familiar SQL, run it near app logic at the edge — without building a database cluster for a few thousand rows.

Ví dụ trên hub này: form đăng ký workshop ghi vào bảng `signups` với `event_id`, `email`, `created_at`. Admin xem danh sách theo sự kiện — một câu `SELECT` đơn giản. KV vẫn hữu ích cho feature flag hoặc cache, nhưng không thay thế query quan hệ. Example on this hub: a workshop signup form writes to a `signups` table with `event_id`, `email`, `created_at`. An admin lists rows per event — a simple `SELECT`. KV still helps for feature flags or cache, but it does not replace relational queries. Example on this hub: a workshop signup form writes to a `signups` table with `event_id`, `email`, `created_at`. An admin lists rows per event — a simple `SELECT`. KV still helps for feature flags or cache, but it does not replace relational queries.

Hình 1: Cloudflare Developer Platform

Ứng dụng fullstack Fullstack applications Fullstack applications

Ví dụ fullstack trên Developer Platform — Workers/Pages, storage và AI services trong một kiến trúc thực tế. A practical example of how these services come together in a real fullstack application architecture. A practical example of how these services come together in a real fullstack application architecture.

Thuật ngữ: Concepts: Concepts: Workers · Pages · D1 · R2 · KV · Durable Objects

Sơ đồ chính thức ↗ Official diagram ↗ Official diagram ↗ · Serverless Serverless Serverless

D1 hoạt động cùng Workers như thế nào? How does D1 work alongside Workers? How does D1 work alongside Workers?

Bạn tạo database D1 trong dashboard hoặc bằng `wrangler d1 create`. Trong `wrangler.toml`, khai báo binding — ví dụ `DB` — rồi trong Worker gọi `env.DB.prepare("SELECT ...").bind(...).all()`. Request người dùng chạy code Worker; Worker đọc/ghi D1 trong cùng luồng xử lý. Create a D1 database in the dashboard or with `wrangler d1 create`. Declare a binding in `wrangler.toml` — e.g. `DB` — then in the Worker call `env.DB.prepare("SELECT ...").bind(...).all()`. User requests run Worker code; the Worker reads and writes D1 in the same request path. Create a D1 database in the dashboard or with `wrangler d1 create`. Declare a binding in `wrangler.toml` — e.g. `DB` — then in the Worker call `env.DB.prepare("SELECT ...").bind(...).all()`. User requests run Worker code; the Worker reads and writes D1 in the same request path.

Schema thay đổi qua migrations: file SQL trong thư mục `migrations/`, áp dụng local bằng `wrangler d1 migrations apply <name> --local` và production khi deploy. Điều này giống workflow Rails hoặc Django — chỉ nhẹ hơn vì database nhỏ và gắn edge. Schema changes go through migrations: SQL files under `migrations/`, applied locally with `wrangler d1 migrations apply <name> --local` and to production on deploy. This feels like Rails or Django workflows — lighter because the database is small and edge-attached. Schema changes go through migrations: SQL files under `migrations/`, applied locally with `wrangler d1 migrations apply <name> --local` and to production on deploy. This feels like Rails or Django workflows — lighter because the database is small and edge-attached.

Local dev: `wrangler dev` có thể dùng D1 local với dữ liệu giả. Trên hub, nếu `/api/workshop-events` trả `dbReady: false`, thường là chưa chạy migrations local — không phải lỗi “SQL khó”. Local dev: `wrangler dev` can use local D1 with sample data. On this hub, if `/api/workshop-events` returns `dbReady: false`, you often have not run local migrations — not “SQL is hard.” Local dev: `wrangler dev` can use local D1 with sample data. On this hub, if `/api/workshop-events` returns `dbReady: false`, you often have not run local migrations — not “SQL is hard.”

Ba pattern phổ biến cho app nhỏ Three common patterns for small apps Three common patterns for small apps

Một — đăng ký sự kiện hoặc waitlist: bảng `signups` với unique constraint trên email + event để tránh trùng. Hai — kết quả quiz hoặc khảo sát: lưu `answers` JSON hoặc cột chuẩn hóa, aggregate bằng SQL. Ba — metadata nội dung: bài blog draft, tag, trạng thái publish — khi bạn không muốn rebuild static site mỗi lần sửa một trường. One — event signup or waitlist: a `signups` table with a unique constraint on email + event to prevent duplicates. Two — quiz or survey results: store `answers` as JSON or normalized columns, aggregate with SQL. Three — content metadata: draft posts, tags, publish state — when you do not want to rebuild a static site for every field edit. One — event signup or waitlist: a `signups` table with a unique constraint on email + event to prevent duplicates. Two — quiz or survey results: store `answers` as JSON or normalized columns, aggregate with SQL. Three — content metadata: draft posts, tags, publish state — when you do not want to rebuild a static site for every field edit.

D1 không phải lựa chọn cho analytics hàng tỷ dòng hoặc workload ghi cực nặng đồng thời. Blog và docs Cloudflare thường gợi ý: bắt đầu D1 cho MVP, khi scale hoặc cần Postgres đầy đủ thì đồng bộ hoặc chuyển dần — nhưng nhiều side project không bao giờ cần bước đó. D1 is not for billion-row analytics or extreme concurrent write loads. Cloudflare blog and docs often suggest: start with D1 for an MVP, sync or migrate when you need full Postgres at scale — though many side projects never need that step. D1 is not for billion-row analytics or extreme concurrent write loads. Cloudflare blog and docs often suggest: start with D1 for an MVP, sync or migrate when you need full Postgres at scale — though many side projects never need that step.

Bắt đầu an toàn: migrations, backup và secrets Start safely: migrations, backups, and secrets Start safely: migrations, backups, and secrets

Luôn version schema bằng migrations — không sửa bảng production tay trong dashboard rồi quên. Test query trên dataset nhỏ local trước. Với email hoặc PII, cân nhắc mã hóa cột nhạy cảm và retention policy; D1 không tự thay bạn tuân thủ GDPR hay luật địa phương. Always version schema with migrations — do not hand-edit production tables in the dashboard and forget. Test queries on a small local dataset first. For email or PII, consider encrypting sensitive columns and retention policies; D1 does not automatically make you GDPR or local-law compliant. Always version schema with migrations — do not hand-edit production tables in the dashboard and forget. Test queries on a small local dataset first. For email or PII, consider encrypting sensitive columns and retention policies; D1 does not automatically make you GDPR or local-law compliant.

Đọc trang sản phẩm D1 và lộ trình Developer Platform trên hub, rồi mở bài gốc trên blog.cloudflare.com khi cần chi tiết replication hoặc pricing mới. Câu hỏi tự kiểm tra: “Query này có cần JOIN hoặc aggregate không?” Nếu có, D1 hoặc SQL bên ngoài hợp lý hơn KV. Read the D1 product page and Developer Platform track on this hub, then open original blog.cloudflare.com posts for replication or pricing updates. Self-check: “Does this query need JOINs or aggregates?” If yes, D1 or external SQL fits better than KV. Read the D1 product page and Developer Platform track on this hub, then open original blog.cloudflare.com posts for replication or pricing updates. Self-check: “Does this query need JOINs or aggregates?” If yes, D1 or external SQL fits better than KV.

Hình minh họa Cloudflare Cloudflare visuals Cloudflare visuals

Sơ đồ Reference Architecture chính thức và (khi có) ảnh Dashboard — giúp đối chiếu khi học. Official Reference Architecture diagrams and (when available) Dashboard screenshots — useful while you learn. Official Reference Architecture diagrams and (when available) Dashboard screenshots — useful while you learn.

Hình 1: Kiến trúc truyền thống một vùng

API serverless toàn cầu Serverless global APIs Serverless global APIs

Ví dụ kiến trúc API serverless trên Cloudflare — compute và data product (Workers, D1, R2) tương tác thế nào. An example architecture of a serverless API on Cloudflare and aims to illustrate how different compute and data products could interact with each other. An example architecture of a serverless API on Cloudflare and aims to illustrate how different compute and data products could interact with each other.

Thuật ngữ: Concepts: Concepts: Workers · D1 · R2 · Global API · Edge compute

Sơ đồ chính thức ↗ Official diagram ↗ Official diagram ↗ · Serverless Serverless Serverless

Câu hỏi thường gặp Frequently asked questions Frequently asked questions

D1 khác Workers KV chỗ nào? How is D1 different from Workers KV? How is D1 different from Workers KV?

KV tối ưu đọc key đơn, eventual consistency, không có SQL. D1 là SQLite quan hệ: SELECT/WHERE/JOIN, phù hợp dữ liệu có cấu trúc và query linh hoạt hơn. KV optimizes single-key reads, is eventually consistent, and has no SQL. D1 is relational SQLite: SELECT/WHERE/JOIN, suited to structured data and flexible queries. KV optimizes single-key reads, is eventually consistent, and has no SQL. D1 is relational SQLite: SELECT/WHERE/JOIN, suited to structured data and flexible queries.

D1 có thay thế PostgreSQL production lớn không? Can D1 replace a large production PostgreSQL? Can D1 replace a large production PostgreSQL?

Thường không cho workload enterprise nặng. D1 phù hợp app edge nhỏ và vừa. Khi cần Postgres đầy đủ, nhiều team dùng Hyperdrive hoặc DB bên ngoài kết hợp Workers. Usually not for heavy enterprise workloads. D1 fits small and mid edge apps. When you need full Postgres, many teams use Hyperdrive or an external DB with Workers. Usually not for heavy enterprise workloads. D1 fits small and mid edge apps. When you need full Postgres, many teams use Hyperdrive or an external DB with Workers.

Làm sao deploy schema D1 lên production? How do you deploy D1 schema to production? How do you deploy D1 schema to production?

Commit file migration SQL, chạy `wrangler d1 migrations apply <database> --remote` (hoặc trong CI). Binding trong `wrangler.toml` phải trùng tên Worker production. Commit migration SQL files, run `wrangler d1 migrations apply <database> --remote` (or in CI). The binding name in `wrangler.toml` must match production Workers. Commit migration SQL files, run `wrangler d1 migrations apply <database> --remote` (or in CI). The binding name in `wrangler.toml` must match production Workers.

Học tiếp trên hub (on-page backlinks) Keep learning on this hub (on-page links) Keep learning on this hub (on-page links)

Nguồn tham khảo (blog.cloudflare.com) Sources (blog.cloudflare.com) Sources (blog.cloudflare.com)

Nội dung được viết lại để dễ hiểu hơn; luôn đọc bài gốc trên blog.cloudflare.com và docs chính thức khi cần chi tiết kỹ thuật hoặc cập nhật mới nhất. Content is rewritten for clarity; always read the original posts on blog.cloudflare.com and official docs for technical detail or the latest updates. Content is rewritten for clarity; always read the original posts on blog.cloudflare.com and official docs for technical detail or the latest updates.

What are Cloudflare Workers? Serverless at the edge, explained simply
Workers Workers Workers Cơ bản Entry កម្រិតចាប់ផ្តើម ~7 phút ~7 min ~7 នាទី

Cloudflare Workers là gì? Serverless ở “mép mạng” giải thích đơn giản What are Cloudflare Workers? Serverless at the edge, explained simply What are Cloudflare Workers? Serverless at the edge, explained simply

Workers giống thuê “người trực” ngay gần khách hàng: nhận request, xử lý nhanh, trả kết quả — bạn không phải tự mua và bảo trì cả tòa nhà server. Workers are like stationing helpers near customers: take a request, handle it quickly, return a result — without buying and maintaining a whole server building. Workers are like stationing helpers near customers: take a request, handle it quickly, return a result — without buying and maintaining a whole server building.

Đọc bài → Read post → អានអត្ថបទ →

Cloudflare Developer Platform: build global apps without managing servers yourself
Developer Platform Developer Platform Developer Platform Trung cấp Intermediate កម្រិតមធ្យម ~9 phút ~9 min ~9 នាទី

Cloudflare Developer Platform: xây ứng dụng toàn cầu mà không tự quản lý server Cloudflare Developer Platform: build global apps without managing servers yourself Cloudflare Developer Platform: build global apps without managing servers yourself

Developer Platform giống bộ lego edge: compute (Workers), hộp đựng (R2/KV/D1), AI, và lớp bảo vệ — lắp theo use case thay vì mua cả trung tâm dữ liệu. The Developer Platform is like an edge Lego set: compute (Workers), storage boxes (R2/KV/D1), AI, and protection layers — assemble by use case instead of buying a whole data center. The Developer Platform is like an edge Lego set: compute (Workers), storage boxes (R2/KV/D1), AI, and protection layers — assemble by use case instead of buying a whole data center.

Đọc bài → Read post → អានអត្ថបទ →

What is a CDN? Understanding Cloudflare Cache in plain language
CDN CDN CDN Cơ bản Entry កម្រិតចាប់ផ្តើម ~6 phút ~6 min ~6 នាទី

CDN là gì? Hiểu Cloudflare Cache như đang giải thích cho bạn bè What is a CDN? Understanding Cloudflare Cache in plain language What is a CDN? Understanding Cloudflare Cache in plain language

CDN giống mạng kho hàng gần người dùng. Cloudflare Cache giữ bản sao nội dung tĩnh gần bạn — trang mở nhanh hơn, server gốc đỡ “mệt”. Think of a CDN as warehouses near your users. Cloudflare Cache keeps copies of static content close by — pages load faster and origin servers work less. Think of a CDN as warehouses near your users. Cloudflare Cache keeps copies of static content close by — pages load faster and origin servers work less.

Đọc bài → Read post → អានអត្ថបទ →

Xem tất cả bài blog Browse all blog posts Browse all blog posts

Chuỗi bài AI · Security · CDN · Workers · Developer Platform — viết lại cho người mới và trung cấp. AI · Security · CDN · Workers · Developer Platform — rewritten for entry and intermediate learners. AI · Security · CDN · Workers · Developer Platform — rewritten for entry and intermediate learners.