提供案例
billflare.

把常醒的 Durable Objects 装进 Containers 以降低时长费用

Robert Chandler(@bertie_ai)称让 1,000 个 Durable Objects 常醒每月要 $4,050,而他用 workerd 把它们装进 Containers 共用后,同样 1,000 个每月 $22;串文说明原因、持久性上的取舍,以及什么情况下真 Durable Objects 反而更便宜。数字为作者测算,不是账单。

适用产品
Durable ObjectsContainersR2
来源
作者经验𝕏
作者
Robert Chandler(@bertie_ai)

要点

  • 作者说 Cloudflare 把每个醒着的 Durable Object 按占用 128 MB 内存计费,而在 Cloudflare 的开源运行时 workerd 里,他的每个对象只用了约 200 KB。于是他把对象装进运行 workerd 的容器里,6,000 个 WebSocket 时容器在 1 到 5 个之间自动伸缩,写入过程中在容器间迁移对象,没有丢写入。
  • 他指出代价:真正的 Durable Object 在写入复制完成后才确认,而在他的池子里,容器崩溃可能丢最多 30 秒的写入。他认为光标、在线状态、游戏状态、Agent 草稿这类数据可以接受,涉及钱的数据仍应使用真正的 Durable Objects。
  • 他补充:如果对象大部分时间在休眠,真 Durable Objects 反而更便宜;按他的测算,对象每月醒着超过约 2 小时,池化才划算。仓库 README 也写明大多处于休眠的对象应保留为真 Durable Objects;检查点按每个节点每 30 秒写一个 R2 分段,而不是每个对象一次 PUT,因为逐对象 PUT 的费用会超过容器本身。
  • 他说最有用的发现是:官方 containers 库把每个 WebSocket 帧都经过 JavaScript 转发,上限约每秒 1,000 条消息,而且每条都计费;改成一行直通就解决了。他说全部测试的 Cloudflare 账单是 $0.63。
  • 官方文档(2026-10-16 复核):Durable Objects 时长按分配的 128 MB 计费,与实际用量无关,多个对象共用一个 isolate 时也一样;可休眠的空闲对象不计时长费;对 WebSocket 调用 accept() 会在整个连接期间计时长费,因此建议使用 WebSocket Hibernation API。Containers 在运行时按每 10 毫秒计费,内存和磁盘按所配置实例规格计费。

原始来源

  1. 发布

    Robert Chandler(@bertie_ai)称让 1,000 个 Durable Objects 常醒每月要 $4,050,而他用 workerd 把它们装进 Containers 共用后,同样 1,000 个每月 $22;串文说明原因、持久性上的取舍,以及什么情况下真 Durable Objects 反而更便宜。数字为作者测算,不是账单。

    Robert Chandler

    @bertie_ai · 𝕏 原帖

    keeping 1,000 cloudflare durable objects awake costs $4,050/month i got the same 1,000 running for $22/month. same code, still on cloudflare 🧵
  2. 串文:每个醒着的 Durable Object 按 128 MB 计费,而在 workerd 里每个约 200 KB;他把它们装进容器,6,000 个 WebSocket 时容器在 1 到 5 个之间自动伸缩,写入中迁移对象,丢失写入为 0。

    到 𝕏 阅读

    Robert Chandler

    @bertie_ai · 𝕏 原帖

    cloudflare bills every awake DO like it’s using 128mb of ram. in workerd (their open source runtime) each one used ~200kb so i packed them into containers instead autoscaled 1 to 5 containers for 6,000 websockets moved objects between containers mid-write, 0 writes lost https://t.co/HqvAfNDNgW
  3. 串文:他把自己的做法与 celld(基于 S3 自托管的 Durable Objects)比较,在同样的容器里跑过 celld,CPU 大致持平,而 celld 的持久性比他的好。

    到 𝕏 阅读

    Robert Chandler

    @bertie_ai · 𝕏 原帖

    @rough__sea shipped celld in august: self-hosted durable objects on s3 i went the other way. stay on cloudflare, stop paying per object ran celld in the same containers. roughly tied on cpu, and his durability story is way better than mine https://t.co/ZtWtBtlT6O
  4. 串文:真正的 Durable Object 复制完成后才确认写入;他的池子里容器崩溃最多丢 30 秒写入,涉及钱的仍用真 Durable Objects。

    到 𝕏 阅读

    Robert Chandler

    @bertie_ai · 𝕏 原帖

    the catch: a real DO doesn’t ack a write until it’s replicated. that guarantee is the magic here, if a container crashes you lose up to 30s of writes for cursors, presence, game state, agent scratchpads that’s fine. for anything with money in it, keep real DOs
  5. 串文:对象大多休眠时真 Durable Objects 更便宜,每月醒着 2 小时以上池化才划算;容器可能在很远的地方启动(他在加州得到巴黎和大阪)。附代码链接。

    到 𝕏 阅读

    Robert Chandler

    @bertie_ai · 𝕏 原帖

    also if your DOs mostly sleep, real ones are cheaper anyway. pooling wins once they’re awake 2+ hrs a month and containers boot wherever. from california i got paris and osaka lol code: https://t.co/9ONaBuGq78
  6. 日期未记录

    串文附带的仓库 README:在作者的对比中,常醒对象用池化约便宜 180 倍;建议大多休眠的对象保留为真 Durable Objects;检查点按每节点每 30 秒写一个 R2 分段(每次刷新 2 次 PUT),以避免逐对象 PUT 的费用。

    打开原始来源
  7. 串文:官方 containers 库把每个 WebSocket 帧经 JavaScript 转发,上限约每秒 1,000 条且每条计费;一行直通解决。全部测试账单 $0.63。

    到 𝕏 阅读

    Robert Chandler

    @bertie_ai · 𝕏 原帖

    had claude code build this in a day. best find: the official containers library relays every websocket frame through javascript, caps out around 1k msgs/sec and bills each one. a one line passthrough fixed it whole cloudflare bill for all the testing was $0.63
  8. 串文:附上池化方案的在线演示链接。

    到 𝕏 阅读

    Robert Chandler

    @bertie_ai · 𝕏 原帖

    you can mess around with it here: https://t.co/hYS1KLYeCZ
  9. 作者回复“也可以把多个对象打包进一个 Durable Object”的建议:他认为这样会失去 actor 模型的好处,单个对象容易过载且无法迁移状态,还很快会碰到 6 个并发子请求的上限。

    到 𝕏 阅读

    Robert Chandler

    @bertie_ai · 𝕏 原帖

    @banskyrich tell me more… i think you’d then lose the beauty of the actor model and any one DO could quite easily get overloaded without an ability to move the state from one DO to another. plus the 6 concurrent sub request limit would likely bite pretty fast
社区资源Durable Objects +1

cloudflare-cost-guard 防爆规则库

Louis Cai 开源的防爆规则与 TypeScript 基类,用于约束 Durable Object alarm 与 Worker 定时任务的失控循环。

D1

mcwangcn:bot 攻击后 D1 费用 $1,300。

@mcwangcn 称 D1 写入曾花费上千美元,并另提 Workers CPU 费用但未给金额。他后来晒出 Cloudflare 账单,称 $3,600 中有 $1,300 来自 bot 攻击后的 D1 费用。

其他产品

sylwair:项目失误,不到两天爆了近 900 刀。

@sylwair 称最近因项目失误爆了账单,不到两天爆了近 900 刀。他称在 Cloudflare【可计费使用量】中发现后停掉了异常服务;在回复中补充,由于每日账单延时展示,停服后看到金额没有明显下降。