Repository navigation
Store a message's own events together, in two statements (CHOO-3667) - #1252
Closed
abeldantas wants to merge 1 commit into
Closed
abeldantas wants to merge 1 commit into
abeldantas wants to merge 1 commit into
Conversation
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.
abeldantas
requested review from
agent-runner353,
amaudruz,
christian-mcdermott,
lbangalosbt,
massimo-sandboxaq,
niall-sandboxaq,
petr-sandbox,
raullopez-sandbox and
simonflk-sb
as code owners
November 1, 2026 02:14
Contributor
There was a problem hiding this comment.
🔵 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_togetherfor batched event-head locking and insertion. - Updated
MessageRouterto 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
This was referenced Nov 1, 2026
Collaborator
Author
|
Combined into #1307 with the other scaling PRs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:agent_event_heads, in the order of their ids, asstoreasks of a caller storing severalAn 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)