Skip to content

Share room lookups between a process's clients (CHOO-3667) - #1248

Closed
abeldantas wants to merge 1 commit into
mainfrom
shared-room-ids
Closed

abeldantas wants to merge 1 commit into
mainfrom
shared-room-ids

Conversation

@abeldantas

Copy link
Copy Markdown
Collaborator

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:

  • share those answers between a process's clients (RoomIds); a room's transport id never changes, so the map only grows and never goes stale
  • read every open room of the workspace the first time a process misses (TenantRoomIds.read_all_once), rather than one room at a time as its clients first write to them

In 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.

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.

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.

🟡 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 RoomIds and 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:
@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