在 Workers Free 上跑一个招聘站:错误 1102、D1 每个请求 50 次查询,以及共享的出站 IP
SagaSu(@sujingshen)描述了把 DMtheFounder 维持在 Workers Free 套餐上的情况:SSR 的 CPU 为 P50 54 ms,对照 10 ms 的限额,导致错误 1102,直到他对匿名页面做了边缘缓存;一项管理任务触及 D1 每次调用 50 次查询的限额,直到他把写入改为批量;IndexNow 的多次 429 来自 Workers 共享的出站 IP。
- 适用产品
- WorkersD1WAF
- 来源
- 作者经验𝕏 文章
- 作者
- SagaSu(@sujingshen)
要点
- 墙 1:错误 1102(Worker exceeded resource limits)。他报告,React SSR 每次请求的 CPU 时间为 P50 54 ms、P90 206 ms 和 P99 437 ms,对照 Free 套餐大约 10 ms。修复:对于没有 session cookie 或 Authorization 头的请求,先检查 caches.default,并把渲染后的页面缓存 5 分钟,缓存键为去掉 utm_* 参数的 URL,加上来自 version_metadata 绑定的部署版本 ID;一次缓存命中约消耗 1 ms 的 CPU。他还改为渲染 60 个职位,而不是全部,并按 isolate 把每个分类的计数记忆 5 分钟。他说,他还没有改动之后的 CPU 数字。
- 墙 2:D1 在 Free 上允许每次 Worker 调用 50 次查询(Paid 上为 1,000 次)。一个管理按钮对每个职位最多执行 8 次 UPDATE,大约超过 6 个职位后就失败。修复:一次 SELECT,在 JavaScript 中决定,只通过 db.batch() 以每批 50 条写入空列。相关陷阱:一条 D1 语句最多只能绑定 100 个参数。
- 墙 3:IndexNow 返回 429,因为 Workers 的出站请求与许多其他网站共享 IP 地址;他把这次推送挪到一个 VPS cron 上,只提交新增、已更改或已移除的 URL。
- 额外一项:一条 WAF 自定义规则(Free 套餐包含 5 条)会在扫描器路径(例如 /wp-admin/install.php)运行到 Worker 之前将其拦截。一旦无法缓存的已登录流量增长,他计划每月支付 $5。
原始来源
发布
原帖
到 𝕏 阅读SagaSu(@sujingshen)描述了把 DMtheFounder 维持在 Workers Free 套餐上的情况:SSR 的 CPU 为 P50 54 ms,对照 10 ms 的限额,导致错误 1102,直到他对匿名页面做了边缘缓存;一项管理任务触及 D1 每次调用 50 次查询的限额,直到他把写入改为批量;IndexNow 的多次 429 来自 Workers 共享的出站 IP。
SagaSu
@sujingshen · 𝕏 原帖
https://t.co/CktVG1TS8Q
阅读全文归档于
SagaSu · @sujingshen
此归档尚未包含完整正文。
Running a job board on Cloudflare Workers' free plan: what broke and how I fixed it
I run DMtheFounder, a job board for early engineering roles that startup founders post themselves on X and Hacker News. It's a one-person project, so I built it on the Cloudflare Workers free plan: TanStack Start for the app, D1 (SQLite) for data, and Workers for everything else. Global, fast, and $0 a month. That worked until it didn't. Here are the three walls I hit, in order, and what got me past each one. Wall 1: Error 1102, or "10ms is not a lot of CPU" One evening I was replying to people on X with links to the site, and a few clicks later the site started showing Error 1102: Worker exceeded resource limits. The free plan gives each request about 10ms of CPU time. The metrics page told the story: CPU time per request was P50 54ms, P90 206ms, P99 437ms. Server-rendering a React page with a few hundred job cards and some counts simply doesn't fit in 10ms. Most requests got through on burst allowance; under a small spike, they didn't. Upgrading to the $5 paid plan would have fixed it in a minute. I wanted to see how far I could get first. Fix 1: cache whole pages at the edge for signed-out visitors. This was the big one. Almost everyone on a job board is anonymous, and they all see the same page. So in the Worker's fetch handler: If the request has no session cookie and no Authorization header, look it up in caches.default first. On a miss, render as usual, then store a copy for 5 minutes. The cache key is the URL with tracking parameters (utm_*) stripped and the rest sorted, plus the current deployment's version ID, so every deploy starts with a clean cache. Signed-in users and admin pages never touch the cache. The version ID comes from the version_metadata binding in wrangler.jsonc. A cache hit costs about a millisecond of CPU instead of fifty. Fix 2: render and query less. The home page used to render every live role. Now it renders 60 with a "Show more" link (?n=120). The per-category counts used by the landing pages are memoized per isolate for five minutes instead of being recomputed on every request. What about the CPU numbers after both changes? Honestly, I don't have them yet. These changes went out recently, and there isn't a full week of post-change metrics to quote. I'd rather not publish a number I can't back up, so I'll update this post with the new P50 and P90 once the data is in. Wall 2: D1 allows 50 queries per request A week later an admin button ("fill empty fields from research") started failing. The SQL was fine. The problem was the count: it ran up to eight small UPDATEs per job, one after another, and the free plan allows 50 D1 queries per Worker invocation (1,000 on paid). Past about six jobs, it died. The fix is the one the D1 docs recommend: read once, decide in JavaScript, write in batches. One SELECT pulls every listing and its current values; only the columns that are actually empty get an UPDATE; the updates go out through db.batch() in chunks of 50. A few hundred jobs now cost a handful of queries. Related trap: a single D1 statement can bind at most 100 parameters. An inArray() over 150 IDs fails. Chunk it, or do the matching in JS. Wall 3: IndexNow kept returning 429 I wanted Bing to pick up new roles quickly, so the site pinged IndexNow whenever a job went live. Every request came back 429 Too Many Requests. IndexNow rate-limits by source IP, and Workers' outbound requests leave from IP addresses shared with a huge number of other sites. My few pings a day were sharing a bucket with everyone else's. I already had a small VPS running the daily data pipeline, so the push moved there: a cron job reads the sitemap, compares it with what it sent last time, and submits only new, changed, or removed URLs. First run: 447 URLs, HTTP 200. The same script pattern now also notifies Google's Indexing API for job pages. Lesson: when something rate-limits by IP, the Workers egress IP is not really yours. Bonus: let the edge drop junk traffic for free Workers metrics also showed a steady trickle of GET /wp-admin/install.php and friends. The site isn't WordPress, but each of those requests still ran the Worker and rendered a 404. One WAF custom rule (the free plan includes five) blocks them before they reach the Worker: Action: Block. Zero CPU for scanners. Where this leaves me The site still runs on the free plan. What made the difference, roughly in order of impact: Edge-cache anonymous pages, keyed by deployment version. Batch D1 writes; read once, write only what changed. Don't render what nobody scrolls to. Move anything IP-sensitive off Workers. Block junk at the edge. I'll upgrade the day signed-in traffic grows, because those requests can't be cached. At $5 a month that's an easy call. But it was worth learning where the limits are first. If you're an engineer who'd rather message a founder than fill in an application form, the roles are at dmthefounder.com. Founders can post for free. Happy to answer questions about the setup in the comments. — Sagasu, building DMtheFounder
以下内容作为原文附录保留,未改动正文。
附录 1 · typescript
```typescript const cacheKey = isEdgeCacheable(request) ? edgeCacheKey(request, env.CF_VERSION_METADATA.id) : null; if (cacheKey) { const hit = await caches.default.match(cacheKey); if (hit) return hit; // ~1ms instead of ~50ms } const response = await render(request); if (cacheKey && isStorable(response)) { ctx.waitUntil(caches.default.put(cacheKey, forEdge(response.clone()))); } ```附录 2 · plaintext
```plaintext (http.request.uri.path contains "/wp-") or (ends_with(http.request.uri.path, ".php")) or (starts_with(http.request.uri.path, "/.env")) or (starts_with(http.request.uri.path, "/.git")) ```同一篇 X 文章的中文版(“用 Cloudflare Workers 免费套餐跑一个招聘站:撞了三堵墙,以及怎么绕过去的”),在同一分钟发布。
到 𝕏 阅读SagaSu
@sujingshen · 𝕏 原帖
https://t.co/0kkj53QNZZ
阅读全文归档于
SagaSu · @sujingshen
此归档尚未包含完整正文。
用 Cloudflare Workers 免费套餐跑一个招聘站:撞了三堵墙,以及怎么绕过去的
我在做 DMtheFounder,一个早期创业公司工程岗的招聘站,岗位都是创始人自己在 X 和 Hacker News 上发的。因为是一个人的项目,我把它整个放在 Cloudflare Workers 免费套餐上:应用用 TanStack Start,数据用 D1(SQLite),其余都跑在 Workers 上。全球都快,每月 0 元。 一直挺顺,直到撞墙。下面按顺序讲三堵墙,以及各自怎么过去的。 第一堵墙:1102 错误,10ms CPU 真的不多 有天晚上我在 X 上回复别人、顺手贴了网站链接,点进来的人一多,网站开始报 Error 1102: Worker exceeded resource limits。 免费套餐每次请求只有大约 10ms 的 CPU 时间。看指标就明白了:每次请求的 CPU 时间 P50 54ms、P90 206ms、P99 437ms。服务端渲染一个有几百张岗位卡片、还要算一堆计数的 React 页面,10ms 根本不够。平时靠突发额度扛过去,稍微来一波流量就扛不住了。 升级到每月 5 美元的付费套餐,一分钟就能解决。但我想先看看不花钱能走多远。 解法一:给没登录的访客做整页边缘缓存。 这是最大的一刀。招聘站绝大多数访客都没登录,看到的页面也一样。所以在 Worker 的 fetch 入口里: 请求里没有登录 cookie、也没有 Authorization 头时,先查 caches.default。 没命中就正常渲染,然后存一份,缓存 5 分钟。 缓存的 key 是去掉 utm_* 追踪参数、剩下参数排好序的网址,再加上当前部署的版本号。每次发布新版本,旧缓存自动作废。 登录用户和后台页面完全不走缓存。 版本号来自 wrangler.jsonc 里的 version_metadata 绑定。命中缓存时 CPU 大约 1ms,原来要 50ms 左右。 解法二:少渲染、少查询。 首页原来一次渲染全部在招岗位,现在先渲染 60 个,底部放一个「Show more」链接(?n=120)。各个专题页要用的分类计数,在每个 isolate 里缓存 5 分钟,不再每次请求都重算。 两项改完以后 CPU 降到多少?老实说,现在还没有数字。这些改动上线不久,还攒不够一整周的数据,我不想贴一个站不住的数。等数据够了,我再回来补上新的 P50 和 P90。 第二堵墙:D1 每次请求最多 50 次查询 一周后,后台一个按钮(「用调研补全空字段」)开始报错。SQL 本身没问题,问题出在次数:它对每个岗位一条一条地执行最多 8 个小的 UPDATE,而免费套餐每次 Worker 调用最多 50 次 D1 查询(付费是 1000 次)。岗位超过六七个就挂了。 解决办法就是 D1 文档推荐的:读一次,在 JS 里判断,批量写。一条 SELECT 读出所有岗位和当前值,只对真正空着的字段生成 UPDATE,再用 db.batch() 每 50 条一批发出去。现在几百个岗位也只占几次查询。 相关的坑:D1 单条语句最多绑定 100 个参数。对 150 个 ID 做 inArray() 会直接报错。要么分批,要么在 JS 里匹配。 第三堵墙:IndexNow 一直返回 429 我想让 Bing 尽快收录新岗位,所以每上架一个岗位,网站就通知一次 IndexNow。结果每次都返回 429 Too Many Requests。 IndexNow 按来源 IP 限流,而 Workers 发出去的请求,用的是和海量其他网站共享的出口 IP。我一天几次的通知,和别人的请求挤在同一个额度里。 我本来就有一台小 VPS 在跑每天的数据流程,于是把推送挪了过去:定时任务读站点地图,和上次推过的清单比较,只推新增、改动和下架的网址。第一次跑:447 个网址,HTTP 200。后来同样的写法也用来通知 Google 的 Indexing API。 教训:凡是按 IP 限流的服务,Workers 的出口 IP 都不算是你自己的。 附加:让边缘免费挡掉垃圾流量 Workers 的指标里还有一串稳定的 GET /wp-admin/install.php 之类的请求。我的网站不是 WordPress,但这些请求照样会运行 Worker、渲染一个 404 页面,白白花 CPU。 一条 WAF 自定义规则(免费套餐有 5 条)就能在它们进 Worker 之前挡掉: 操作选「阻止」。扫描器从此不花一点 CPU。 现在的状态 网站仍然跑在免费套餐上。按效果大小排,真正起作用的是: 匿名访问的整页边缘缓存,按部署版本区分。 D1 批量写入:读一次,只写变了的。 没人会滚动到的内容,就不要渲染。 对 IP 敏感的事情,挪到 Workers 之外去做。 垃圾请求在边缘挡掉。 等登录用户多起来我就升级,因为登录用户的请求没法缓存,每月 5 美元很好决定。但先搞清楚限制在哪里,是值得的。 如果你是工程师,比起填申请表更想直接私信创始人,岗位都在 dmthefounder.com。创始人免费发布岗位。关于这套架构有任何问题,欢迎留言。
以下内容作为原文附录保留,未改动正文。
附录 1 · typescript
```typescript const cacheKey = isEdgeCacheable(request) ? edgeCacheKey(request, env.CF_VERSION_METADATA.id) : null; if (cacheKey) { const hit = await caches.default.match(cacheKey); if (hit) return hit; // ~1ms instead of ~50ms } const response = await render(request); if (cacheKey && isStorable(response)) { ctx.waitUntil(caches.default.put(cacheKey, forEdge(response.clone()))); } ```附录 2 · plaintext
```plaintext (http.request.uri.path contains "/wp-") or (ends_with(http.request.uri.path, ".php")) or (starts_with(http.request.uri.path, "/.env")) or (starts_with(http.request.uri.path, "/.git")) ```
其他推荐阅读
Spend Management:为 Workers 与 Durable Objects 设置支出限额与提醒
Cloudflare 于 2026 年 11 月 13 日开放测试版:按量付费账号可分别为 Workers 或 Durable Objects 的用量费用(扣除包含额度后)设置每月美元支出限额。限额可暂停新的计费调用,也可只发邮件。它不是保证的发票上限,按产品作用于整个账号,不含 Workers Paid 订阅费、Durable Objects 已存储数据,也不覆盖 KV、R2、Workers AI 等其他产品。
stevesprompt:一个智能体在 Cloudflare 控制台点击了 Upgrade,被收取 $1.5k;款项已退还。
X 用户 @stevesprompt(Steve Huang)称一个智能体在其 Cloudflare 控制台点击了 Upgrade,他被收取 $1.5k,并称该款项幸好已退还。作者随后列出了在智能体能够花钱或发送任何内容之前的预防措施。该帖未点名所升级的产品。该帖中没有 Cloudflare 的官方公开回应。
风易:R2 公开桶免费档最容易被偷的是公开直链,而不只是额度本身
风易(@Michael_liyang)指出,R2 公开桶的直链谁都能引用,别人的流量会记在桶主账上。建议三步:静态资源走自定义域名、不要裸挂 r2.dev;用 Worker 查 Referer 白名单,不在名单则返回 403;图片使用带过期时间的签名 URL。