Repository navigation
Share room lookups between a process's clients (CHOO-3667) - #1248
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 client kept its own map from transport room ids to room ids, so an agent writing to a room for the first time through a process read the room first. With thousands of agents behind a load balancer that was most sends. A room's transport id never changes, so a process's transports now share the map (RoomIds), and the first miss in a process reads every open room of the tenant at once rather than one room at a time as its clients first write to them.
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 01:38
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
A transient failure in the leader’s full read can wake all waiters into separate single-room queries instead of the intended coordinated retry, violating the retry contract and creating unnecessary load.
1 open issue
What changed in this PR
This pull request reduces room lookup database reads by sharing process-local, tenant-scoped transport-room mappings across clients and loading all open rooms on the first cache miss.
Changes:
- Added shared
RoomIdsand coordinated tenant-wide room loading. - Added an open-room lookup query and wired the cache through
ClientFactory. - Added caching, concurrency, routing, and transaction-cost tests.
- Documented the optimization in the changelog.
| File | Summary | Final review comments |
|---|---|---|
core/tests/switch_core/transport/test_room_ids.py |
Tests tenant isolation, concurrent reads, and retry behavior. | No final comments. |
core/tests/switch_core/delivery/test_router_sessions.py |
Verifies connection costs for cached, bulk-loaded, and newly created rooms. | Moderate, 1 vote: The archived-room exclusion and fallback behavior are not actually exercised because no room is archived before the first miss. |
core/switch_core/transport/postgres.py |
Implements shared tenant-scoped room ID caching and bulk lookup behavior. | Moderate, 2 votes: When the leader’s full read fails, waiters fall through to individual room queries instead of retrying the shared read. Moderate, 1 vote: Hard-deleted rooms are not invalidated from RoomIds, allowing stale mappings to persist. |
core/switch_core/db/stores/room_store.py |
Adds a query returning all open rooms and their transport IDs. | No final comments. |
core/switch_core/clients/client_factory.py |
Shares one RoomIds instance across process transports. |
No final comments. |
CHANGELOG.md |
Documents the room lookup optimization. | No final comments. |
Review effort: Lite
Comment on lines
+234
to
+239
| done = self._read.get(self._tenant_id) | ||
| if done is not None: | ||
| await done.wait() | ||
| return | ||
| done = self._read[self._tenant_id] = asyncio.Event() | ||
| try: |
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.

A client looks a room up before its first write there, and each client kept its own answers. With thousands of agents behind a load balancer, an agent seldom writes to the same room twice through the same process, so most sends started with a read of the room.
In this PR we:
RoomIds); a room's transport id never changes, so the map only grows and never goes staleTenantRoomIds.read_all_once), rather than one room at a time as its clients first write to themIn a local run of the load harness (200 agents, 2 processes, 5 msg/s), that read was 0.7 transactions per message, and it is gone.