Skip to content

perf(api): Batch cost cache reads and share Slack team domains - #2821

Merged
BYK merged 1 commit into
mainfrom
perf/redis-mget-team-cache
Nov 7, 2026
Merged

BYK merged 1 commit into
mainfrom
perf/redis-mget-team-cache

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

In prod, each GET /api/conversations sends about 76 Redis GETs for the subtree cost cache (130,851 GETs across 1,716 list requests). team.info runs on 11% of list requests (p50 107 / p95 322 ms), because its cache is per process and every new Fluid instance calls Slack again.

Changes

  • Cost cache: readOperationsByConversation reads all cache entries for a request with one MGET, and writes the changed entries in one pipeline.
    • StateAdapter comes from the Chat SDK and has no multi-get. So getStateValues and setStateValues in chat/state/adapter.ts take a StateAdapter. With the default Redis adapter they use MGET and MULTI … execAsPipeline(). With any other adapter, including memory in tests, they call get/set once per key.
    • They build the same keys as RedisStateAdapter (chat-sdk:cache:{JUNIOR_STATE_KEY_PREFIX:}{key}). createRedisState now gets keyPrefix: "chat-sdk" explicitly, which is the value it already used by default, so existing entries keep working. Locally, MGET read the entries the old code wrote.
  • team.info: resolveSlackTeamDomain keeps its per-process cache and adds a state entry slack-team-domain:{teamId} with the same 24 h TTL. An instance that has no process entry reads the state entry before it calls team.info. Only found domains go into the state entry. A failed lookup stays in the 60 s per-process negative cache, so one transient Slack error doesn't reach other instances.
    • The code stays in core chat/slack/team-domain.ts, which owns this lookup.

Measurements (local)

Postgres 17 and Redis 7 in Docker. The list has 50 roots and 76 Conversations in their subtrees (26 roots with one child agent each), with 20 cost events per Conversation. The cache was warm, n = 40. Redis commands were counted with INFO commandstats. To add latency, I put a TCP proxy that delays each chunk by 2 ms in each direction (about 4 ms round trip) in front of Redis.

Redis commands / list cost read p50 / p95 list p50 / p95
Before, no added latency 76 6.4 / 9.4 ms 12.0 / 16.9 ms
After, no added latency 1 5.9 / 7.9 ms 11.8 / 16.3 ms
Before, ~4 ms Redis round trip 76 8.3 / 10.4 ms 13.9 / 17.5 ms
After, ~4 ms Redis round trip 1 8.1 / 10.4 ms 14.0 / 18.0 ms

Wall time hardly changes. node-redis already pipelines the 76 concurrent GETs on one connection, so before this change they also cost about one round trip. The gain is 75 fewer commands per list request: less Redis CPU, fewer per-command charges, and fewer client spans. It is not latency. Both versions return the same costs: total $0.152 and child agents $0.052 on the seeded data.

team.info, new process:

  • With the state entry present, a cold process resolved the domain in 2.8 ms: one Redis GET, including the connect.
  • Without the entry, it calls Slack: 107 ms p50 in prod, and 55 ms locally for a call that failed with an invalid token.

Tests

  • location-url.test.ts (list scenario): checks that a found domain is written to state, and that a team with no process cache entry gets its domain from state instead of from team.info.
  • New state-batch-redis.test.ts: runs against real Redis, like queue-bullmq.test.ts. It checks that a batched read returns values written with set, that a missing key reads as null, and that a value from a batched write reads back with get. Changing the key format in redisCacheKey makes it fail. Vitest pins the memory adapter for the other tests, so no other test covers the Redis path.

Checks

  • tsc --noEmit and pnpm lint pass.
  • Postgres-backed tests/integration/api/conversations/ plus the Redis test: 20 files, 76 tests, all pass.
  • Slack integration tests (tests/integration/slack/ plus the slack-* files): 32 files, 226 tests, all pass.
  • test-architecture and file-length checks pass.

via @BYK.

--

View Session in Sentry

Read the list's auxiliary cost cache entries with one MGET and write the
changed ones in one pipeline, instead of one GET and SET per Conversation.
Store found Slack team domains in the state adapter, so a new instance
reads one key instead of calling team.info.

Co-Authored-By: BYK <[email protected]>
@vercel

vercel Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
junior-docs Ready Ready Preview Nov 6, 2026 2:21pm UTC

Request Review

@blacksmith-sh

This comment has been minimized.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 8dc9e15. Configure here.

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

13 screenshot changes — 13 changed · 0 added · 0 removed

Review screenshots in Frameshift

Conversation Attachment · Desktop
Conversation Attachment · Desktop
Changed
Conversation Attachment · Mobile
Conversation Attachment · Mobile
Changed
Conversation Attachment Modal · Desktop
Conversation Attachment Modal · Desktop
Changed
Conversation Attachment Zoom · Desktop
Conversation Attachment Zoom · Desktop
Changed
Conversation Captured Memories Popover · Desktop
Conversation Captured Memories Popover · Desktop
Changed
Conversation Captured Memories Popover · Mobile
Conversation Captured Memories Popover · Mobile
Changed
Show 7 more changed screenshots
Conversation Child Agents · Desktop
Conversation Child Agents · Desktop
Changed
Conversation Details Watched Linked Work · Desktop
Conversation Details Watched Linked Work · Desktop
Changed
Conversation Details Watched Linked Work · Mobile
Conversation Details Watched Linked Work · Mobile
Changed
Conversation Long Transcript Touch · Mobile
Conversation Long Transcript Touch · Mobile
Changed
Conversation Share Details · Desktop
Conversation Share Details · Desktop
Changed
Conversation Stop Active · Mobile
Conversation Stop Active · Mobile
Changed
Settings Mcp · Desktop
Settings Mcp · Desktop
Changed

@BYK
BYK merged commit 4b801c5 into main Nov 7, 2026
63 of 65 checks passed
@BYK
BYK deleted the perf/redis-mget-team-cache branch November 7, 2026 00:55
BYK added a commit that referenced this pull request Nov 8, 2026
In prod, 19% of `GET /api/conversations` requests wait on Slack
`users.info` after the main queries finish. That wait happens when an
activity preview mentions a Slack user who has no name in Junior
(`readMentionedPeople` in `api/mentions.ts`). This PR stores those names
in shared state for 24 hours, so a list read on a new instance doesn't
call Slack.

## Why the cache missed

`lookupSlackUser` caches answers only in process memory, for 5 minutes.
Prod data for the last 24h:

- 1,586 list requests ran on 237 instances, about 7 requests per
instance per day.
- 305 of them called `users.info`, on 164 instances.

So the per-instance cache was nearly always cold. A longer TTL alone
wouldn't help, but a shared cache does.

Also, 36 lookups repeated on the same instance within the 5-minute TTL.
Those were either different users or lookups that Slack didn't answer. A
failed lookup isn't cached anywhere, before or after this change. I
can't tell the two cases apart from telemetry (inference).

## Change

- **`lookupSlackUserNames`** (in `chat/slack/user.ts`): reads all shared
entries `slack-user-names:{team}:{user}` with one MGET (`getStateValues`
from #2821). It calls `users.info` only for the users that are missing,
then writes the names it found in one pipeline.
- **Failed lookups stay local:** only answers from Slack are shared, so
a transient Slack failure affects one request, the same as before.
- **Callers:** the mention lookup now uses `lookupSlackUserNames`. That
covers the list previews, detail events, the Brief, pending messages and
Automations. Other `lookupSlackUser` callers (Turns, avatars, timezones)
are unchanged.
- **No background work:** I didn't use the background-resolve approach.
With a shared cache, only the first read of a new mention in a workspace
each day waits on Slack. The list never shows a placeholder, so nothing
visible changes and there are no screenshots.

## Numbers

Local benchmark: 30 conversations whose previews mention 3 unnamed
users. Each run is 40 list reads, and each read is on a cold instance
(fresh modules, so the process cache is empty), with Redis state.
`users.info` is delayed to match Slack.

| `users.info` delay | | p50 | p95 | Reads that waited on Slack |
`users.info` calls |
| --- | --- | --- | --- | --- | --- |
| 57 ms (prod p50) | before | 103 ms | 112 ms | 40/40 | 120 |
| | after | 37 ms | 60 ms | 1/40 | 3 |
| 300 ms (slow) | before | 345 ms | 362 ms | 40/40 | 120 |
| | after | 42 ms | 59 ms | 1/40 | 3 |

## Checks

- `pnpm typecheck` and `pnpm lint` pass.
- Postgres-backed tests: `tests/integration/api/conversations/`,
`user-lookup`, `slack/actor-avatar`, `api/automations`,
`state-batch-redis` and `unit/slack/user` (28 files, 133 tests).
- **Owning scenario extended:** "resolves mentioned Slack users with
current names" in `detail.test.ts` now checks two things. A name already
in shared state resolves without `users.info`, and a name from Slack is
written to shared state. The test fails without the fix.
- `file-length:check`, `test-architecture:check` and diff-quality: no
findings to fix.

<!-- junior-request-attribution:start -->
via @BYK.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A57f19e3bf526c322da5782cc885008fc -->

--

[View Session in
Sentry](https://sentry.sentry.io/explore/conversations/agent%3A57f19e3bf526c322da5782cc885008fc/?project=4510944073809921)

<!-- junior-session-footer:end -->

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <[email protected]>

This branch was successfully deployed

1 active deployment
Preview – junior-docs — 8dc9e158 Deployed Nov 6, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant