Phần 3: Chọn storage đúng Part 3: Pick the right storage · Bài 1/3 Lesson 1/3

KV cho config và cache nhẹ KV for config and light cache

Lưu feature flags, session token ngắn, rate limit counter. Không dùng KV cho giao dịch tài chính cần consistency cao. Store feature flags, short session tokens, rate limit counters. Avoid KV for financial transactions needing strong consistency.

Các bước thực hiện Step-by-step

  1. Tạo KV namespace: `wrangler kv namespace create APP_CONFIG`. Create KV namespace: `wrangler kv namespace create APP_CONFIG`.
  2. Binding trong wrangler.toml / Pages settings. Bind in wrangler.toml / Pages settings.
  3. Đọc/ghi từ Worker: `env.KV.put(key, value)`. Read/write from Worker: `env.KV.put(key, value)`.
  4. Dùng cho flags, cache config — không cho ledger tài chính. Use for flags, config cache — not financial ledgers.

Giải thích chi tiết Detailed explanation

KV eventually consistent, đọc cực nhanh tại edge — ideal cho read-heavy config. KV is eventually consistent with extremely fast edge reads — ideal for read-heavy config.

Lưu ý (best practices) Note (best practices)

Dùng binding (KV, R2, D1…) trực tiếp trong Worker — không gọi REST API Cloudflare từ trong Worker (thêm latency, auth và phức tạp không cần thiết). Use bindings (KV, R2, D1, etc.) directly in Workers — do not call the Cloudflare REST API from within a Worker (adds latency, auth, and unnecessary complexity).

Nguồn: Source: Workers Best Practices Workers Best Practices

Ví dụ triển khai (Cloudflare Resources) Deployment examples (Cloudflare Resources)

Tutorial, solution guide và reference từ developers.cloudflare.com/resources ↗ — gợi ý theo chủ đề bài học. Tutorials, solution guides, and reference docs from developers.cloudflare.com/resources ↗ — matched to this lesson topic.

Duyệt toàn bộ catalog → Browse full catalog →

Tài liệu Cloudflare Developers Cloudflare Developer docs

Cloudflare API (tham khảo) Cloudflare API (reference)

Dùng khi tự động hóa bước này qua script hoặc Terraform — cần API token phù hợp. Use when automating this step via script or Terraform — requires an appropriate API token.

developers.cloudflare.com/api ↗

Sản phẩm liên quan Related products

Đọc thêm trong hub → Read more in the hub →