perf(task-board): delete PR cache keys concurrently on invalidate - #6981
Open
pedrofrxncx wants to merge 1 commit into
Open
perf(task-board): delete PR cache keys concurrently on invalidate#6981pedrofrxncx wants to merge 1 commit into
pedrofrxncx wants to merge 1 commit into
Conversation
invalidate() collected each namespace's KV keys then awaited kv.delete() one at a time in a for-await loop. invalidatePrCards()/invalidatePrReadsForConnection() are awaited synchronously from PR link/merge tool handlers, so an org (or connection) with many cached PR entries pays one full NATS round-trip per key, serially, before the tool call returns. Deletes target independent, unrelated keys, so there's no ordering requirement — run them concurrently with Promise.all instead.
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.
Source: reduction/optimization found while hunting the mcp-tool-latency-profiling focus area (relates to #2995 "DB latency amplifies significantly across MCP tool calls") — read
pr-cache.ts(added by #6966, the JetStream KV cache fronting task-board PR reads/cards).The problem:
JetStreamKVPrCache.invalidate(namespace)lists every KV key under a namespace, then deletes them one at a time in afor awaitloop — eachkv.delete()is its own NATS round-trip, awaited sequentially.invalidatePrCards(organizationId)andinvalidatePrReadsForConnection(connectionId)are both awaited synchronously from task-board tool handlers (e.g.TASK_BOARD_ITEM_PR_LINK, merge-pr) right before they return. An org or connection with many cached PR entries pays N sequential round-trips inline on that tool call — the busier the board, the slower every PR-link/merge call gets, which is exactly the tool-call latency amplification pattern #2995 describes.The fix: collect the matched keys, then
Promise.allthe deletes. They're independent, unrelated keys with no ordering requirement, and the existing per-key.catch(() => {})(best-effort, TTL is the backstop) is preserved — behavior is unchanged, just concurrent instead of serial.Verify:
cd apps/api && bun test src/tools/task-board/pr-cache.test.ts— added a test assertinginvalidatestill drops every key under a namespace (not just the first), which would have caught an incorrect early-return/short-circuit in a naive rewrite.Checks run locally:
bun run fmt,bunx tsc --noEmit(apps/api), the targeted test above, andbunx oxlinton both changed files — all clean. Full CI runs the rest.Summary by cubic
Speeds up task-board PR cache invalidation by deleting matched keys concurrently instead of one at a time, cutting the serial NATS round-trips that slow PR-link and merge tool calls on busy boards (the latency pattern in #2995). Behavior is unchanged — deletes are independent and best-effort with TTL as the backstop.
invalidate()from a sequentialfor awaitloop toPromise.allover collected keys.Written for commit 254a341. Summary will update on new commits.