Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(ai-research): purge expired ML key rows at most once a minute (#105157)

Robbie0c4eb6371858
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/keys/dynamodb.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/keys/dynamodb.ts
index 96ac027dcde0..13d272db0058 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/keys/dynamodb.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/keys/dynamodb.ts
@@ -21,6 +21,8 @@ const SESSION_ROW_MAX_LIFETIME_MS = 300_000
const TEAM_ROW_LIFETIME_MS = 3_600_000
// Neither caller passes a deadline, and max.poll.interval.ms is 300s, so the retry loop needs a bound of its own.
const READ_BUDGET_MS = 30_000
+// A purge visits every cached row, so a purge on every read makes the cost of a read grow with the size of the cache.
+const STALE_ROW_PURGE_INTERVAL_MS = 60_000
export type DynamoItem = Record<string, AttributeValue>
@@ -58,6 +60,7 @@ export class MlKeyDynamoDB {
private readonly writeConcurrency = pLimit(32)
private readonly rows: LRUCache<string, DynamoItem>
private readonly sessionRowLifetimeMs: number
+ private lastStaleRowPurgeMs = 0
constructor(
private readonly client: Pick<DynamoDBClient, 'send'>,
@@ -175,11 +178,20 @@ export class MlKeyDynamoDB {
})
)
)
- this.rows.purgeStale()
+ this.purgeStaleRowsAtMostOncePerInterval()
MlMirrorMetrics.setMlKeyRowCacheEntries(this.rows.size)
return result
}
+ private purgeStaleRowsAtMostOncePerInterval(): void {
+ const nowMs = Date.now()
+ if (nowMs - this.lastStaleRowPurgeMs < STALE_ROW_PURGE_INTERVAL_MS) {
+ return
+ }
+ this.lastStaleRowPurgeMs = nowMs
+ this.rows.purgeStale()
+ }
+
public clear(): void {
this.rows.clear()
MlMirrorMetrics.setMlKeyRowCacheEntries(0)
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/metrics.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/metrics.ts
index f0b52d4eb6be..66e572aae98c 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/metrics.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/metrics.ts
@@ -149,7 +149,7 @@ export class MlMirrorMetrics {
})
private static readonly mlKeyRowCacheEntries = new Gauge({
name: 'recording_blob_ingestion_v2_ml_key_row_cache_entries',
- help: 'Stored ML key rows the per-process cache holds, counted after a read. Expired rows stay counted until a read or an eviction removes them, so this tracks the memory held rather than the rows still usable, and it stands still on an idle lane',
+ help: 'Stored ML key rows the per-process cache holds, counted after a read. A read purges expired rows at most once a minute, and a lookup of an expired row or an eviction removes it between purges, so this tracks the memory held rather than the rows still usable, and it stands still on an idle lane',
})
private static readonly mlKeyReadRetries = new Counter({
name: 'recording_blob_ingestion_v2_ml_key_read_retries_total',