Skip to content

Commit 2707058

Browse files
committed
perf(rpc): fire-and-forget cache writes in links router
Link create / update / delete were awaiting cache work — setCachedLink, invalidateLinkCache, invalidateAgentContextSnapshotsForOwner — on the response path. The agent-context invalidation is a Redis SCAN + per-key Lua-CAS that can fan out to hundreds of ops for active orgs, adding seconds before the response returns. /links/create p50 was 13.5s in prod for the busiest API-key caller; this is one of the contributors. Detach the cache calls and log failures via .catch. The pre-delete invalidateLinkCache stays awaited because it is load-bearing for cache consistency (must clear before the row disappears).
1 parent 7c56dd1 commit 2707058

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

packages/rpc/src/routers/links.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,19 @@ export const linksRouter = {
389389
throw rpcError.internal("Failed to create link");
390390
}
391391

392-
await setCachedLink(slug, toCachedLink(newLink)).catch((err) =>
392+
setCachedLink(slug, toCachedLink(newLink)).catch((err) =>
393393
logger.error(
394394
{ slug, linkId: newLink.id, error: String(err) },
395395
"Failed to cache link after create"
396396
)
397397
);
398-
await invalidateAgentContextSnapshotsForOwner(organizationId);
398+
invalidateAgentContextSnapshotsForOwner(organizationId).catch(
399+
(err) =>
400+
logger.error(
401+
{ organizationId, error: String(err) },
402+
"Failed to invalidate agent context snapshots after link create"
403+
)
404+
);
399405

400406
return newLink;
401407
} catch (error) {
@@ -511,7 +517,7 @@ export const linksRouter = {
511517
throw rpcError.notFound("link", input.id);
512518
}
513519

514-
await Promise.all([
520+
Promise.all([
515521
oldSlug === updatedLink.slug
516522
? Promise.resolve()
517523
: invalidateLinkCache(oldSlug),
@@ -585,9 +591,14 @@ export const linksRouter = {
585591
);
586592
}
587593

588-
// Hard delete the link
589594
await context.db.delete(links).where(eq(links.id, input.id));
590-
await invalidateAgentContextSnapshotsForOwner(link.organizationId);
595+
invalidateAgentContextSnapshotsForOwner(link.organizationId).catch(
596+
(err) =>
597+
logger.error(
598+
{ organizationId: link.organizationId, error: String(err) },
599+
"Failed to invalidate agent context snapshots after link delete"
600+
)
601+
);
591602

592603
return { success: true };
593604
}),

0 commit comments

Comments
 (0)