Skip to content

Commit 6e72cf1

Browse files
committed
perf(webapp): throttle PAT + OAT lastAccessedAt writes to once per 5 min
Each successful PAT (`PersonalAccessToken`) or OAT (`OrganizationAccessToken`) authentication was issuing an unconditional `prisma.X.update({ lastAccessedAt: new Date() })` on the auth path. Prod observation (2026-05-01): - PersonalAccessToken: ~2.4 writes/sec, 19,617 lifetime autovacuums, vacuumed every ~5 minutes. - OrganizationAccessToken: ~0.9 writes/sec, similar shape. Same denormalization-on-the-hot-path pattern as TRI-8891 — a small narrow table getting hammered with per-event writes that drive frequent autovacuum churn. The `lastAccessedAt` field is only ever read on the /account/tokens settings page to show "last used X ago" so users can decide which tokens to revoke; UI granularity of "within the last 5 minutes" is more than sufficient. Replace each unconditional `update` with a conditional `updateMany` whose WHERE requires the existing `lastAccessedAt` to be NULL or strictly older than 5 minutes. The conditional runs inside the SQL UPDATE, so concurrent auths don't race into double-writes. Estimated impact: ~95% reduction in writes on these two tables. Each table's autovacuum cadence drifts from every ~5 min to every ~hour or longer. Mock-based unit tests verify the throttle WHERE clause is constructed correctly. Behavioral verification will happen post-deploy via DB stats (n_tup_upd rate + autovacuum_count drift).
1 parent 19c1675 commit 6e72cf1

5 files changed

Lines changed: 211 additions & 2 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
area: webapp
3+
type: improvement
4+
---
5+
6+
Throttle `PersonalAccessToken.lastAccessedAt` and `OrganizationAccessToken.lastAccessedAt` writes to at most once per 5 minutes per token. Eliminates ~95% of writes on two narrow hot tables that were autovacuuming every ~5 minutes — same denormalization-on-the-hot-path shape as the schedule engine fix in TRI-8891. The settings UI continues to display "last used" with at most 5-minute lag.

apps/webapp/app/services/organizationAccessToken.server.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ const tokenValueLength = 40;
88
//lowercase only, removed 0 and l to avoid confusion
99
const tokenGenerator = customAlphabet("123456789abcdefghijkmnopqrstuvwxyz", tokenValueLength);
1010

11+
// Skip the lastAccessedAt write if the existing value is already within this
12+
// window. Eliminates per-auth UPDATE churn on a small narrow hot table; the
13+
// settings UI reads this field at human granularity so a few-minute
14+
// staleness is fine.
15+
export const OAT_LAST_ACCESSED_THROTTLE_MS = 5 * 60 * 1000;
16+
1117
type CreateOrganizationAccessTokenOptions = {
1218
name: string;
1319
organizationId: string;
@@ -105,9 +111,16 @@ export async function authenticateOrganizationAccessToken(
105111
return;
106112
}
107113

108-
await prisma.organizationAccessToken.update({
114+
// Conditional updateMany — only writes if the existing lastAccessedAt is
115+
// null or older than the throttle window. The WHERE runs inside the UPDATE
116+
// so concurrent auths don't race into a double-write.
117+
await prisma.organizationAccessToken.updateMany({
109118
where: {
110119
id: organizationAccessToken.id,
120+
OR: [
121+
{ lastAccessedAt: null },
122+
{ lastAccessedAt: { lt: new Date(Date.now() - OAT_LAST_ACCESSED_THROTTLE_MS) } },
123+
],
111124
},
112125
data: {
113126
lastAccessedAt: new Date(),

apps/webapp/app/services/personalAccessToken.server.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ const tokenValueLength = 40;
1010
//lowercase only, removed 0 and l to avoid confusion
1111
const tokenGenerator = customAlphabet("123456789abcdefghijkmnopqrstuvwxyz", tokenValueLength);
1212

13+
// Skip the lastAccessedAt write if the existing value is already within this
14+
// window. Eliminates per-auth UPDATE churn on a small narrow hot table; the
15+
// /account/tokens UI reads this field at human granularity so a few-minute
16+
// staleness is fine.
17+
export const PAT_LAST_ACCESSED_THROTTLE_MS = 5 * 60 * 1000;
18+
1319
type CreatePersonalAccessTokenOptions = {
1420
name: string;
1521
userId: string;
@@ -205,9 +211,16 @@ export async function authenticatePersonalAccessToken(
205211
return;
206212
}
207213

208-
await prisma.personalAccessToken.update({
214+
// Conditional updateMany — only writes if the existing lastAccessedAt is
215+
// null or older than the throttle window. The WHERE runs inside the UPDATE
216+
// so concurrent auths don't race into a double-write.
217+
await prisma.personalAccessToken.updateMany({
209218
where: {
210219
id: personalAccessToken.id,
220+
OR: [
221+
{ lastAccessedAt: null },
222+
{ lastAccessedAt: { lt: new Date(Date.now() - PAT_LAST_ACCESSED_THROTTLE_MS) } },
223+
],
211224
},
212225
data: {
213226
lastAccessedAt: new Date(),
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { beforeEach, describe, expect, test, vi } from "vitest";
2+
3+
const { findFirstMock, updateManyMock } = vi.hoisted(() => ({
4+
findFirstMock: vi.fn(),
5+
updateManyMock: vi.fn(),
6+
}));
7+
8+
vi.mock("~/db.server", () => ({
9+
prisma: {
10+
organizationAccessToken: {
11+
findFirst: findFirstMock,
12+
updateMany: updateManyMock,
13+
},
14+
},
15+
$replica: {},
16+
}));
17+
18+
vi.mock("~/utils/tokens.server", () => ({
19+
hashToken: (t: string) => `hashed:${t}`,
20+
}));
21+
22+
vi.mock("./logger.server", () => ({
23+
logger: { warn: vi.fn(), error: vi.fn() },
24+
}));
25+
26+
import {
27+
authenticateOrganizationAccessToken,
28+
OAT_LAST_ACCESSED_THROTTLE_MS,
29+
} from "~/services/organizationAccessToken.server";
30+
31+
beforeEach(() => {
32+
findFirstMock.mockReset();
33+
updateManyMock.mockReset();
34+
updateManyMock.mockResolvedValue({ count: 1 });
35+
});
36+
37+
describe("authenticateOrganizationAccessToken — lastAccessedAt throttle", () => {
38+
test("issues a conditional updateMany that skips writes when lastAccessedAt is recent", async () => {
39+
findFirstMock.mockResolvedValueOnce({
40+
id: "oat_123",
41+
organizationId: "org_1",
42+
hashedToken: "hashed:tr_oat_validtoken",
43+
});
44+
45+
const before = Date.now();
46+
const result = await authenticateOrganizationAccessToken("tr_oat_validtoken");
47+
const after = Date.now();
48+
49+
expect(result).toEqual({ organizationId: "org_1" });
50+
expect(updateManyMock).toHaveBeenCalledTimes(1);
51+
52+
const call = updateManyMock.mock.calls[0][0];
53+
expect(call.where.id).toBe("oat_123");
54+
expect(call.data.lastAccessedAt).toBeInstanceOf(Date);
55+
56+
// The WHERE clause should require the existing lastAccessedAt to be null
57+
// or strictly older than the throttle window — that's the entire point.
58+
expect(call.where.OR).toEqual([
59+
{ lastAccessedAt: null },
60+
{ lastAccessedAt: { lt: expect.any(Date) } },
61+
]);
62+
63+
const cutoff = call.where.OR[1].lastAccessedAt.lt as Date;
64+
expect(cutoff.getTime()).toBeGreaterThanOrEqual(before - OAT_LAST_ACCESSED_THROTTLE_MS - 50);
65+
expect(cutoff.getTime()).toBeLessThanOrEqual(after - OAT_LAST_ACCESSED_THROTTLE_MS + 50);
66+
});
67+
68+
test("skips updateMany when token is not found", async () => {
69+
findFirstMock.mockResolvedValueOnce(null);
70+
71+
const result = await authenticateOrganizationAccessToken("tr_oat_validtoken");
72+
73+
expect(result).toBeUndefined();
74+
expect(updateManyMock).not.toHaveBeenCalled();
75+
});
76+
77+
test("skips updateMany when token doesn't start with prefix", async () => {
78+
const result = await authenticateOrganizationAccessToken("not_an_oat");
79+
80+
expect(result).toBeUndefined();
81+
expect(findFirstMock).not.toHaveBeenCalled();
82+
expect(updateManyMock).not.toHaveBeenCalled();
83+
});
84+
});
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { beforeEach, describe, expect, test, vi } from "vitest";
2+
3+
const { findFirstMock, updateManyMock } = vi.hoisted(() => ({
4+
findFirstMock: vi.fn(),
5+
updateManyMock: vi.fn(),
6+
}));
7+
8+
vi.mock("~/db.server", () => ({
9+
prisma: {
10+
personalAccessToken: {
11+
findFirst: findFirstMock,
12+
updateMany: updateManyMock,
13+
},
14+
},
15+
$replica: {},
16+
}));
17+
18+
vi.mock("~/env.server", () => ({
19+
env: { ENCRYPTION_KEY: "0".repeat(64) },
20+
}));
21+
22+
vi.mock("~/utils/tokens.server", () => ({
23+
hashToken: (t: string) => `hashed:${t}`,
24+
encryptToken: () => ({ nonce: "n", ciphertext: "c", tag: "t" }),
25+
decryptToken: () => "tr_pat_validtoken",
26+
}));
27+
28+
vi.mock("./logger.server", () => ({
29+
logger: { warn: vi.fn(), error: vi.fn() },
30+
}));
31+
32+
import {
33+
authenticatePersonalAccessToken,
34+
PAT_LAST_ACCESSED_THROTTLE_MS,
35+
} from "~/services/personalAccessToken.server";
36+
37+
beforeEach(() => {
38+
findFirstMock.mockReset();
39+
updateManyMock.mockReset();
40+
updateManyMock.mockResolvedValue({ count: 1 });
41+
});
42+
43+
describe("authenticatePersonalAccessToken — lastAccessedAt throttle", () => {
44+
test("issues a conditional updateMany that skips writes when lastAccessedAt is recent", async () => {
45+
findFirstMock.mockResolvedValueOnce({
46+
id: "pat_123",
47+
userId: "user_1",
48+
hashedToken: "hashed:tr_pat_validtoken",
49+
encryptedToken: { nonce: "n", ciphertext: "c", tag: "t" },
50+
});
51+
52+
const before = Date.now();
53+
const result = await authenticatePersonalAccessToken("tr_pat_validtoken");
54+
const after = Date.now();
55+
56+
expect(result).toEqual({ userId: "user_1" });
57+
expect(updateManyMock).toHaveBeenCalledTimes(1);
58+
59+
const call = updateManyMock.mock.calls[0][0];
60+
expect(call.where.id).toBe("pat_123");
61+
expect(call.data.lastAccessedAt).toBeInstanceOf(Date);
62+
63+
// The WHERE clause should require the existing lastAccessedAt to be null
64+
// or strictly older than the throttle window — that's the entire point.
65+
expect(call.where.OR).toEqual([
66+
{ lastAccessedAt: null },
67+
{ lastAccessedAt: { lt: expect.any(Date) } },
68+
]);
69+
70+
const cutoff = call.where.OR[1].lastAccessedAt.lt as Date;
71+
// Cutoff should be exactly throttle-ms before "now" (within the test
72+
// window). Confirms the throttle constant is wired through correctly.
73+
expect(cutoff.getTime()).toBeGreaterThanOrEqual(before - PAT_LAST_ACCESSED_THROTTLE_MS - 50);
74+
expect(cutoff.getTime()).toBeLessThanOrEqual(after - PAT_LAST_ACCESSED_THROTTLE_MS + 50);
75+
});
76+
77+
test("skips updateMany when token is not found", async () => {
78+
findFirstMock.mockResolvedValueOnce(null);
79+
80+
const result = await authenticatePersonalAccessToken("tr_pat_validtoken");
81+
82+
expect(result).toBeUndefined();
83+
expect(updateManyMock).not.toHaveBeenCalled();
84+
});
85+
86+
test("skips updateMany when token doesn't start with prefix", async () => {
87+
const result = await authenticatePersonalAccessToken("not_a_pat");
88+
89+
expect(result).toBeUndefined();
90+
expect(findFirstMock).not.toHaveBeenCalled();
91+
expect(updateManyMock).not.toHaveBeenCalled();
92+
});
93+
});

0 commit comments

Comments
 (0)