Skip to content

perf(log-stream): batch live-globe posts per isolate - #288

Draft
alukach wants to merge 2 commits into
source-cooperative:mainfrom
alukach:worktree-log-stream-batching
Draft

alukach wants to merge 2 commits into
source-cooperative:mainfrom
alukach:worktree-log-stream-batching

Conversation

@alukach

@alukach alukach commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

What

The proxy now batches live-globe posts per isolate instead of posting once per public read.

  • An isolate runs in one datacenter. It gathers its public-product reads for 3 s and posts one {colo, products: {"account/product": count}} message to public-log-stream. Counts are exact; nothing is sampled.
  • A busy datacenter collapses into a few posts. A quiet one still reports its first request, at most 3 s later.
  • Only the datacenter and per-product counts leave the proxy. Coordinates, city, country and key are no longer posted.
  • The hub adds a batch's counts as posted, so the tick it emits is unchanged and the frontend needs no change. It still accepts the old one-request-per-post form.
  • A batch the hub rejects, or a post that fails, is logged at info level (live-globe batch not delivered). It is not a WARN, so a failing globe never counts toward the WARN alerts.
  • ADR-008 describes the new contract.

Why

Before this change the proxy sent about 370 posts/second at peak to one global Durable Object (about 213M requests in a week):

  • Billing: about $145/month ($132 of Durable Object requests, about $14 of CPU). Service-binding calls are not billed as Worker requests.
  • Reliability: about 40M exceptions in the same week. The Worker saw 254M requests, the Durable Object 213.6M, and the 40.4M difference never reached it. That fits the Durable Object's documented overload behaviour (soft limit 1,000 requests/second per object).
  • Viewers are about 1% of the cost; nearly all of it is the proxy's posts.

Expected effect, and why 3 s

Modelled monthly cost of the live globe (Durable Object requests plus CPU), against about $150 today:

Window Optimistic (about one isolate per datacenter) Pessimistic (about 2 requests/s per isolate)
1 s about $15 about $61
3 s about $7 about $29
5 s about $5 about $19

The model takes the per-datacenter request rates from the last 7 days (254 datacenters; PDX alone is about a third and the top four about 60%), scales them to the 353 posts/second the Durable Object serves today, and lets each isolate post once per window plus the wait for its next request. The unbatched baseline lands at $152 against about $146 billed. The isolate count per datacenter is a guess, which is why the range is wide, and CPU is assumed to fall in proportion to posts. The window also delays activity: with the hub spreading each batch over a matching window (#289), end-to-end delay is about twice the window, and the frontend fades dots over 10 s, so 5 s is the most I would use. WINDOW_MS in src/location_batch.rs and the hub's SPREAD_SLOTS should be changed together.

How it works

src/location_batch.rs holds the pure rule and is covered by tests/location_batch.rs. src/location.rs keeps the batch in a thread_local!. The request that opens a batch becomes its leader: it sleeps 3 s in wait_until (the limit is 30 s) and posts the whole batch once. A batch older than 6 s is taken over by the next request, so an evicted leader cannot strand it.

Rollout

  • Deploy order is already enforced in CI: the hub deploys before the proxy (needs: deploy-public-log-stream in production.yml and staging.yml).
  • A new hub accepts old proxies. A new proxy against an old hub would count each batch as one request under the product /, so avoid that order; roll back the proxy first, or both together.

Not checked

  • The real reduction in calls to public-log-stream. It depends on how many isolates serve each datacenter. After deploying, compare workersInvocationsAdaptive (script public-log-stream) and durableObjectsInvocationsAdaptiveGroups (the LocationHub namespace) for the same weekday and hour before and after.
  • How the globe looks. Activity now arrives up to 3 s late and in bursts; I did not render it, so please look at the staging globe. I have no before/after images. LiveGlobe.tsx (source.coop origin/main) ignores unknown fields, never reads requestsPerWindow, and fades dots over 10 s, so no frontend change should be needed.
  • Counts held by an isolate that is evicted before its window ends are lost.

Follow-up (separate PR): spread each batch across ticks at the hub and replay the last few seconds to new viewers.

🤖 Generated with Claude Code

alukach and others added 2 commits November 19, 2026 22:00
The proxy posted one event per public read to a single global Durable Object
(~370 req/s at peak): about $145/month of Durable Object requests and CPU, plus
overload exceptions in the busiest hours.

An isolate runs in one datacenter, so each isolate now gathers its public reads
for 3 s and posts one batch of exact per-product counts. A busy datacenter
collapses into few posts; a quiet one still reports its first request. The hub
adds the counts as posted, so the tick payload is unchanged. Only the colo and
product counts leave the proxy now (no coordinates, city, country or key), and
the hub still accepts the old one-request-per-post form for rollout.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A failed or rejected post dropped its whole batch without a trace. Say so at
info level (not WARN, so a failing globe never counts toward the WARN alerts);
a Durable Object past its request limit now shows up in the logs.

Also list location_batch.rs in ADR-008 and note the up-to-3-second delay.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

This branch had an error being deployed

1 failed deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant