Repository navigation
perf(api): Batch cost cache reads and share Slack team domains - #2821
Merged
Merged
Conversation
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]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ 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.
Contributor
|
13 screenshot changes — 13 changed · 0 added · 0 removed Review screenshots in Frameshift Show 7 more changed screenshots |
BYK
approved these changes
Nov 7, 2026
23 tasks done
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
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.

In prod, each
GET /api/conversationssends about 76 Redis GETs for the subtree cost cache (130,851 GETs across 1,716 list requests).team.inforuns 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
readOperationsByConversationreads all cache entries for a request with oneMGET, and writes the changed entries in one pipeline.StateAdaptercomes from the Chat SDK and has no multi-get. SogetStateValuesandsetStateValuesinchat/state/adapter.tstake aStateAdapter. With the default Redis adapter they useMGETandMULTI … execAsPipeline(). With any other adapter, including memory in tests, they callget/setonce per key.RedisStateAdapter(chat-sdk:cache:{JUNIOR_STATE_KEY_PREFIX:}{key}).createRedisStatenow getskeyPrefix: "chat-sdk"explicitly, which is the value it already used by default, so existing entries keep working. Locally,MGETread the entries the old code wrote.resolveSlackTeamDomainkeeps its per-process cache and adds a state entryslack-team-domain:{teamId}with the same 24 h TTL. An instance that has no process entry reads the state entry before it callsteam.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.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.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:
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 fromteam.info.state-batch-redis.test.ts: runs against real Redis, likequeue-bullmq.test.ts. It checks that a batched read returns values written withset, that a missing key reads asnull, and that a value from a batched write reads back withget. Changing the key format inredisCacheKeymakes it fail. Vitest pins the memory adapter for the other tests, so no other test covers the Redis path.Checks
tsc --noEmitandpnpm lintpass.tests/integration/api/conversations/plus the Redis test: 20 files, 76 tests, all pass.tests/integration/slack/plus theslack-*files): 32 files, 226 tests, all pass.via @BYK.
--
View Session in Sentry