Skip to content

Store a message's own events together, in two statements (CHOO-3667) - #1252

Closed
abeldantas wants to merge 1 commit into
fused-delivery-readsfrom
batch-own-events
Closed

abeldantas wants to merge 1 commit into
fused-delivery-readsfrom
batch-own-events

Conversation

@abeldantas

Copy link
Copy Markdown
Collaborator

Every agent a message was for cost four statements in the writer's transaction: a savepoint, its number claimed, its event inserted, the savepoint released. A message for three agents spent twelve statements on their events, and building the insert for each one was a large part of what writing a message cost.

In this PR we store the agents' events together (EventBuffer.store_together), in one savepoint:

  • one statement locks their rows in agent_event_heads, in the order of their ids, as store asks of a caller storing several
  • one inserts each event for an agent still in the room and not already holding it, and moves that agent's head to it

An event not stored takes no number, so each agent's numbers stay contiguous. If the two statements fail, the events are stored one at a time as before (MessageRouter._store_own_events), so one agent's failure still costs that agent alone.

In a local run of the load harness (400 agents behind 20 controllers, 2 processes, 20 msg/s, rooms of 4, each message for 3 agents), CPU per message went from 17.8 to 14.7 ms, and delivery p99 from 74 to 60 ms.

Stack (merge order)

  1. Make idle controller agents and startup cheap on the database as deployments grow (CHOO-3667) #1200
  2. Read a controller's agents' heads once per delivery, not three times (CHOO-3667) #1220
  3. Try a controller's delivery pass again when the pool has no connection, instead of closing its socket (CHOO-3667) #1221
  4. Take an agent's head from the notification of its new event (CHOO-3667) #1246
  5. Deliver an agent's event in three statements, not seven (CHOO-3667) #1251
  6. This PR ← you are here

Each agent a message was for cost four statements in the writer's
transaction: a savepoint, its number claimed, its event inserted, the
savepoint released. The agents' events are now stored together
(EventBuffer.store_together): one statement locks their rows in
agent_event_heads in the order of their ids, and one inserts each event
for an agent still in the room and not already holding it, and moves
that agent's head, all in one savepoint. An event not stored takes no
number. If the two statements fail, the events are stored one at a time
as before, so one agent's failure still costs that agent alone.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The PostgreSQL transaction-path rewrite introduces lock-ordering, savepoint-fallback, concurrency, and duplicate-source risks that require human-reviewed production-scale validation.

No open issues 🎉

What changed in this PR

This pull request reduces database overhead when storing events for messages addressed to multiple agents by batching event persistence into two statements within one savepoint, while retaining per-agent numbering, membership checks, deduplication, and fallback behavior.

Changes:

  • Added EventBuffer.store_together for batched event-head locking and insertion.
  • Updated MessageRouter to batch own-event writes and fall back to individual storage on failure.
  • Added tests for numbering, membership, deduplication, fallback behavior, and statement counts.
  • Documented the performance improvement in the changelog.
File Summary Review notes
core/​tests/​switch_core/​delivery/​test_router.py Verifies batched routing costs and fallback behavior. No final comment.
core/​tests/​switch_core/​bridges/​agent/​protocol/​test_store_together.py Covers bulk numbering, membership filtering, deduplication, and initial heads. No final comment.
core/​switch_core/​delivery/​router.py Builds batched events and integrates per-agent fallback storage. Nit (1 vote): Update the transaction walkthrough to describe the new batch lifecycle.
core/​switch_core/​bridges/​agent/​protocol/​event_buffer.py Implements batched event-head locking and persistence SQL. Moderate (1 vote): Roll back the nested savepoint when cancellation occurs, then re-raise the cancellation.
CHANGELOG.md Documents the reduced write cost for multi-agent messages. No final comment.

Review effort: Lite

@abeldantas

Copy link
Copy Markdown
Collaborator Author

Combined into #1307 with the other scaling PRs.

@abeldantas abeldantas closed this Nov 2, 2026
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.

2 participants