Skip to content

Commit 04db191

Browse files
perf(relay): lighter Postgres load and PS-160 cluster
- Agent-activity publishes list the user's targets first and only build the Live Activity aggregate when an iOS target has a running Live Activity (3.4% of users), skipping the top CPU query everywhere else. - Delivery attempts are kept 1 day instead of 7. They only guard queue job retries, and the 13 GB table dominates disk reads. - clusterSize matches the PS-160 resize queued in the dashboard. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent b537916 commit 04db191

3 files changed

Lines changed: 13 additions & 12 deletions

File tree

‎infra/relay/src/agentActivity/AgentActivityPublisher.ts‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,18 @@ const make = Effect.gen(function* () {
6363
readonly state: RelayAgentActivityState | null;
6464
readonly nowMs: number;
6565
}) {
66-
const activeStates = input.deliveryUser.liveActivitiesEnabled
67-
? yield* rows.listForUser({ userId: input.deliveryUser.userId })
68-
: [];
69-
const liveActivityAggregate = input.deliveryUser.liveActivitiesEnabled
70-
? makeAggregateState({
71-
activeStates,
72-
terminalState: input.state && isTerminalPhase(input.state) ? input.state : null,
73-
nowMs: input.nowMs,
74-
})
75-
: null;
7666
const targets = yield* liveActivities.listTargets({ userId: input.deliveryUser.userId });
67+
// Only iOS targets with a running Live Activity read the aggregate, and few users have one,
68+
// so skip the per-user activity query otherwise.
69+
const liveActivityAggregate =
70+
input.deliveryUser.liveActivitiesEnabled &&
71+
targets.some((target) => target.platform !== "android" && target.activity_push_token)
72+
? makeAggregateState({
73+
activeStates: yield* rows.listForUser({ userId: input.deliveryUser.userId }),
74+
terminalState: input.state && isTerminalPhase(input.state) ? input.state : null,
75+
nowMs: input.nowMs,
76+
})
77+
: null;
7778
return yield* Effect.forEach(
7879
targets,
7980
Effect.fnUntraced(function* (target) {

‎infra/relay/src/agentActivity/DeliveryAttempts.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class DeliveryAttempts extends Context.Service<
6868
>()("t3code-relay/agentActivity/DeliveryAttempts") {}
6969

7070
const SOURCE_JOB_CLAIM_LEASE_MINUTES = 10;
71-
export const DELIVERY_ATTEMPT_RETENTION_DAYS = 7;
71+
export const DELIVERY_ATTEMPT_RETENTION_DAYS = 1;
7272
export const DELIVERY_ATTEMPT_PRUNE_BATCH_SIZE = 5_000;
7373
// Caps each cron run so draining the pre-retention backlog cannot monopolize the database.
7474
export const DELIVERY_ATTEMPT_PRUNE_MAX_BATCHES = 4;

‎infra/relay/src/db.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const PlanetscaleDatabase = Effect.gen(function* () {
4949
? yield* Planetscale.PostgresDatabase("RelayPostgresDatabase", {
5050
name: "t3coderelay",
5151
region: { slug: "us-west" },
52-
clusterSize: "PS_80",
52+
clusterSize: "PS_160",
5353
migrations: { dir: schema.out, table: "relay_migrations" },
5454
replicas: 2,
5555
}).pipe(RemovalPolicy.retain())

0 commit comments

Comments
 (0)