Workflows trên Cloudflare: chạy tác vụ dài hơi trên nền tảng Workers Workflows on Cloudflare: long-running jobs on the Workers platform Workflows on Cloudflare: long-running jobs on the Workers platform
Workflows giống checklist tự động có trí nhớ: bước 1 xong mới bước 2, lỗi thì thử lại, có thể chờ vài giờ — không ép mọi thứ vào một request 30 giây. Workflows are like a stateful checklist: step two runs after step one, retries on failure, can wait hours — without cramming everything into one 30-second request. Workflows are like a stateful checklist: step two runs after step one, retries on failure, can wait hours — without cramming everything into one 30-second request.
Ứ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
Vì sao Worker request ngắn không đủ cho mọi việc? Why a short Worker request is not enough for every job Why a short Worker request is not enough for every job
Cloudflare Workers xuất sắc với logic gắn request/response: nhận HTTP, xử lý vài trăm millisecond đến vài chục giây, trả kết quả. Nhưng nhiều quy trình thực tế dài hơn và nhiều bước: gửi email chào mừng, đợi người dùng xác nhận, gọi API thanh toán, cập nhật database, gửi thông báo Slack — nếu bước 3 fail, bạn muốn retry bước 3, không chạy lại từ đầu. Cloudflare Workers excel at request/response logic: receive HTTP, process for milliseconds to tens of seconds, return a result. But many real processes are longer and multi-step: send a welcome email, wait for user confirmation, call a payment API, update the database, notify Slack — if step 3 fails, you want to retry step 3, not restart from scratch. Cloudflare Workers excel at request/response logic: receive HTTP, process for milliseconds to tens of seconds, return a result. But many real processes are longer and multi-step: send a welcome email, wait for user confirmation, call a payment API, update the database, notify Slack — if step 3 fails, you want to retry step 3, not restart from scratch.
Workflows là nền tảng durable execution trên Cloudflare: bạn định nghĩa các bước (steps), hệ thống lưu trạng thái, tự retry theo chính sách, và cho phép sleep/chờ giữa các bước. Trên blog.cloudflare.com, các bài về Workflows và Developer Platform mô tả đây là cách mở rộng Workers từ “hàm một lần” sang “quy trình sống lâu” mà không tự dựng queue + worker + dead-letter bằng tay. Workflows is durable execution on Cloudflare: you define steps, the platform persists state, retries per policy, and allows sleep/wait between steps. Cloudflare Blog posts on Workflows and the Developer Platform describe this as extending Workers from “one-shot functions” to “long-lived processes” without hand-rolling queues, workers, and dead-letter queues. Workflows is durable execution on Cloudflare: you define steps, the platform persists state, retries per policy, and allows sleep/wait between steps. Cloudflare Blog posts on Workflows and the Developer Platform describe this as extending Workers from “one-shot functions” to “long-lived processes” without hand-rolling queues, workers, and dead-letter queues.
Hãy so sánh: Worker thường giống nhân viên trả lời một câu hỏi tại quầy; Workflows giống hồ sơ xử lý hồ sơ vay — có thể để trên bàn vài ngày, đánh dấu đã xong bước nào, ai phụ trách bước tiếp theo. Compare: a regular Worker is like staff answering one question at the counter; Workflows is like a loan file — it can sit on the desk for days, marking which steps finished and what comes next. Compare: a regular Worker is like staff answering one question at the counter; Workflows is like a loan file — it can sit on the desk for days, marking which steps finished and what comes next.
Ứ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
Ba khái niệm cốt lõi: step, state, retry Three core ideas: step, state, retry Three core ideas: step, state, retry
Step (bước) là đơn vị công việc có tên — ví dụ `chargePayment`, `sendEmail`, `updateInventory`. Mỗi step nên idempotent khi có thể: chạy lại không tạo hai lần charge. State (trạng thái) được Workflows lưu giữa các lần chạy — bạn không phải tự ghi “đang ở bước 2” vào KV nếu platform đã lo. A step is a named unit of work — for example `chargePayment`, `sendEmail`, `updateInventory`. Each step should be idempotent when possible: reruns should not double-charge. State is persisted between runs — you do not have to write “currently on step 2” to KV if the platform handles it. A step is a named unit of work — for example `chargePayment`, `sendEmail`, `updateInventory`. Each step should be idempotent when possible: reruns should not double-charge. State is persisted between runs — you do not have to write “currently on step 2” to KV if the platform handles it.
Retry: khi API bên thứ ba timeout, Workflows có thể thử lại với backoff thay vì fail cả pipeline. Sleep: chờ 24 giờ trước email nhắc — không giữ Worker instance “treo” suốt 24 giờ. Đây là điểm khác biệt lớn so với cố nhét `setTimeout` dài trong một invocation. Retry: when a third-party API times out, Workflows can retry with backoff instead of failing the whole pipeline. Sleep: wait 24 hours before a reminder email — without holding a Worker instance open for 24 hours. That is a major difference from cramming a long `setTimeout` into one invocation. Retry: when a third-party API times out, Workflows can retry with backoff instead of failing the whole pipeline. Sleep: wait 24 hours before a reminder email — without holding a Worker instance open for 24 hours. That is a major difference from cramming a long `setTimeout` into one invocation.
Workflows thường kết hợp bindings khác: gọi D1 để ghi đơn hàng, R2 để lưu file export, Workers AI cho bước tóm tắt, Queues cho fan-out nếu cần. Sơ đồ fullstack-application trên hub minh họa Workers, D1, R2, KV, Durable Objects cùng nhau — Workflows là lớp điều phối thời gian dài. Workflows often combine other bindings: D1 for orders, R2 for export files, Workers AI for a summarization step, Queues for fan-out when needed. The fullstack-application diagram on this hub shows Workers, D1, R2, KV, and Durable Objects together — Workflows is the long-horizon orchestration layer. Workflows often combine other bindings: D1 for orders, R2 for export files, Workers AI for a summarization step, Queues for fan-out when needed. The fullstack-application diagram on this hub shows Workers, D1, R2, KV, and Durable Objects together — Workflows is the long-horizon orchestration layer.
Use case thực tế: onboarding, đơn hàng, pipeline nội dung Real use cases: onboarding, orders, content pipelines Real use cases: onboarding, orders, content pipelines
Onboarding user: đăng ký → gửi email xác minh → chờ click → tạo workspace mặc định → ghi audit log. Mỗi bước có thể fail độc lập; user không thấy “lỗi 500” chỉ vì SMTP chậm ở bước email. E-commerce: đặt hàng → reserve inventory → charge → ship notification — nếu charge fail, release inventory ở bước bù trừ. User onboarding: signup → send verification email → wait for click → create default workspace → write audit log. Each step can fail independently; users do not see “500 error” just because SMTP was slow on the email step. E-commerce: order → reserve inventory → charge → ship notification — if charge fails, release inventory in a compensating step. User onboarding: signup → send verification email → wait for click → create default workspace → write audit log. Each step can fail independently; users do not see “500 error” just because SMTP was slow on the email step. E-commerce: order → reserve inventory → charge → ship notification — if charge fails, release inventory in a compensating step.
Pipeline nội dung: upload video → transcode (có thể gọi service ngoài) → tạo thumbnail → cập nhật CMS. Workflows phù hợp khi thời gian xử lý vượt giới hạn HTTP một lần. Blog Cloudflare về automation và Workers thường nhấn mạnh: chọn đúng công cụ — không mọi thứ cần Workflows; request ngắn vẫn là Worker thường hoặc Queue đơn giản. Content pipeline: upload video → transcode (maybe external service) → generate thumbnail → update CMS. Workflows fits when processing exceeds a single HTTP window. Cloudflare Blog automation and Workers posts stress picking the right tool — not everything needs Workflows; short work may stay a plain Worker or simple Queue. Content pipeline: upload video → transcode (maybe external service) → generate thumbnail → update CMS. Workflows fits when processing exceeds a single HTTP window. Cloudflare Blog automation and Workers posts stress picking the right tool — not everything needs Workflows; short work may stay a plain Worker or simple Queue.
So với Durable Objects: DO mạnh khi cần stateful object một instance (chat room, game session, coordination). Workflows mạnh khi quy trình là chuỗi bước có thứ tự và thời gian chờ dài. Nhiều kiến trúc dùng cả hai — Workflows gọi DO ở bước cần lock hoặc realtime state. Versus Durable Objects: DOs shine for single-instance stateful objects (chat room, game session, coordination). Workflows shine for ordered multi-step processes with long waits. Many architectures use both — Workflows calls a DO when a step needs a lock or realtime state. Versus Durable Objects: DOs shine for single-instance stateful objects (chat room, game session, coordination). Workflows shine for ordered multi-step processes with long waits. Many architectures use both — Workflows calls a DO when a step needs a lock or realtime state.
Checklist trung cấp trước khi chuyển cron hack sang Workflows Intermediate checklist before moving cron hacks to Workflows Intermediate checklist before moving cron hacks to Workflows
Một: vẽ sơ đồ bước trên giấy — bao gồm bước bù trừ khi fail (compensation). Hai: đặt tên step rõ ràng và log correlation id xuyên suốt. Ba: test retry bằng cách inject lỗi API giả trong staging. Bốn: định nghĩa timeout và số lần retry tối đa — tránh loop vô hạn. Năm: xem xét idempotency key cho thanh toán và ghi DB. One: draw steps on paper — including compensation on failure. Two: name steps clearly and log a correlation ID throughout. Three: test retries by injecting fake API errors in staging. Four: define timeouts and max retries — avoid infinite loops. Five: consider idempotency keys for payments and DB writes. One: draw steps on paper — including compensation on failure. Two: name steps clearly and log a correlation ID throughout. Three: test retries by injecting fake API errors in staging. Four: define timeouts and max retries — avoid infinite loops. Five: consider idempotency keys for payments and DB writes.
Deploy qua Wrangler như Workers khác; đọc trang Workflows và lộ trình Developer Platform trên hub. Nếu bạn mới học Workers, hoàn thành bài Workers intro và KV trước — Workflows dễ hiểu hơn khi bạn đã có một API + binding hoạt động. Deploy via Wrangler like other Workers; read the Workflows product page and Developer Platform track on this hub. If you are new to Workers, finish the Workers intro and KV posts first — Workflows clicks faster once you have a working API + binding. Deploy via Wrangler like other Workers; read the Workflows product page and Developer Platform track on this hub. If you are new to Workers, finish the Workers intro and KV posts first — Workflows clicks faster once you have a working API + binding.
Câu hỏi tự kiểm tra: “Nếu platform chạy lại bước này hai lần, tiền hoặc email có bị duplicate không?” Nếu có, sửa idempotency trước khi production. Mở bài gốc blog.cloudflare.com về Workflows để cập nhật giới hạn và pattern mới nhất. Self-check: “If the platform runs this step twice, do we double-charge or double-email?” If yes, fix idempotency before production. Open original blog.cloudflare.com Workflows posts for the latest limits and patterns. Self-check: “If the platform runs this step twice, do we double-charge or double-email?” If yes, fix idempotency before production. Open original blog.cloudflare.com Workflows posts for the latest limits and patterns.
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.
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
Workflows có thay Queues không? Do Workflows replace Queues? Do Workflows replace Queues?
Không hoàn toàn. Queues tốt cho fan-out và xử lý message độc lập. Workflows tốt cho chuỗi bước có thứ tự, chờ, và retry có cấu trúc. Nhiều hệ thống dùng cả hai. Not entirely. Queues excel at fan-out and independent messages. Workflows excel at ordered steps, waits, and structured retries. Many systems use both. Not entirely. Queues excel at fan-out and independent messages. Workflows excel at ordered steps, waits, and structured retries. Many systems use both.
Workflows có phù hợp job chạy hàng giờ không? Are Workflows suitable for jobs that run for hours? Are Workflows suitable for jobs that run for hours?
Workflows hỗ trợ tác vụ dài hơn request Worker thường, với sleep và durable state — nhưng vẫn có giới hạn platform. Job cực dài hoặc compute nặng liên tục có thể cần kiến trúc khác (container, batch bên ngoài). Luôn kiểm tra docs giới hạn hiện tại. Workflows support longer work than a typical Worker request, with sleep and durable state — but platform limits still apply. Extremely long or heavy continuous compute may need another architecture (containers, external batch). Always check current docs for limits. Workflows support longer work than a typical Worker request, with sleep and durable state — but platform limits still apply. Extremely long or heavy continuous compute may need another architecture (containers, external batch). Always check current docs for limits.
Nên học Workflows trước hay Durable Objects trước? Should I learn Workflows or Durable Objects first? Should I learn Workflows or Durable Objects first?
Nếu bạn có quy trình nhiều bước (email, thanh toán, pipeline), học Workflows trước. Nếu bạn cần realtime state một object (chat, game), học Durable Objects trước. Cả hai là trung cấp sau Workers cơ bản. If you have multi-step processes (email, payments, pipelines), learn Workflows first. If you need single-object realtime state (chat, game), learn Durable Objects first. Both are intermediate after basic Workers. If you have multi-step processes (email, payments, pipelines), learn Workflows first. If you need single-object realtime state (chat, game), learn Durable Objects first. Both are intermediate after basic 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)
- Workflows (trang sản phẩm) Workflows (product page) Workflows (product page)
- Workers (trang sản phẩm) Workers (product page) Workers (product page)
- Lộ trình Developer Platform Developer Platform track Developer Platform track
- Use case: ứng dụng serverless Use case: build a serverless app Use case: build a serverless app
- So sánh với Durable Objects Compare with Durable Objects Compare with Durable Objects
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.