Skip to content

perf: write each event batch to a TCP subscriber in one call - #173

Merged
egeominotti merged 1 commit into
mainfrom
perf/coalesce-event-writes
Oct 25, 2026
Merged

egeominotti merged 1 commit into
mainfrom
perf/coalesce-event-writes

Conversation

@egeominotti

Copy link
Copy Markdown
Owner

Summary

Every TCP Worker subscribes to its queue's events (for stalled), and pull/ack batches emit one event per job. The server wrote each event to each subscriber with its own socket write: a CPU profile of a 4-process drain showed ~64% of server CPU in native write under broadcastBatch → broadcastEvent, and throughput fell as worker processes were added.

The TCP bridge now uses the events manager's existing batch-subscriber path: frames are encoded once, concatenated per queue in batch order, and written once per subscriber socket. Same bytes and per-socket order; single events unchanged; an unencodable event is skipped alone; the over-budget drop decision is unchanged (only the logged byte count can be larger).

Drain, memory mode, 50 concurrency/process 2.9.19 This PR
1 worker process 33,080 jobs/s 39,735 jobs/s
4 worker processes 18,152 jobs/s 53,102 jobs/s
8 worker processes 10,151 jobs/s 40,940 jobs/s

Native Linux (Debian 13, Ryzen 9 7950X3D, Bun 1.4.4), server pinned to 2 cores, workers on the other CCD, pollTimeout: 5000, median of 3.

Verification

  • bun run test:sandbox (before the final guard edit): unit 11,471/0 (347 PG skips), TCP 499/0, embedded 342/0; rerun on the final commit in progress
  • 91 targeted event/write-queue tests pass on the final commit; typecheck, lint, format clean
  • Skeptic review: no ordering, completeness or overflow regressions (960-case old-vs-new write comparison, end-to-end batch check across queues); its doc and guard findings are applied

No test changes.

🤖 Generated with Claude Code

Every TCP Worker subscribes to its queue's events (to report stalled jobs) and
a pull or ack batch emits one event per job, so the server wrote events x
subscribers frames, one socket write each: about 64% of the server's CPU with
four worker processes, and drain throughput fell from 33k jobs/s with one
worker process to 18k with four and 10k with eight.

The TCP bridge now registers the events manager's batch callback: it encodes
each event once, concatenates a batch's frames per queue in batch order and
writes them to each subscriber socket in one call. Same bytes and per-socket
order; an event that cannot be encoded is skipped alone; the over-budget drop
decision is unchanged. Drain with 1/4/8 worker processes: 39.7k/53.1k/40.9k
jobs/s.

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

vercel Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
docs Ready Ready Preview Oct 25, 2026 3:30pm UTC

@claude

claude Bot commented Oct 25, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@egeominotti
egeominotti merged commit 04953c6 into main Oct 25, 2026
40 checks passed
@egeominotti
egeominotti deleted the perf/coalesce-event-writes branch October 29, 2026 21:59

This branch was successfully deployed

1 active deployment
Preview — 98e78033 Deployed Oct 25, 2026 by vercel[bot]
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