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.

perf(ai-research): read an ML key batch in one pass (#102321)

Robbiee69e5e3b2733
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/keys/key-store.test.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/keys/key-store.test.ts
index f091ae6474df..f2ce10288990 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/keys/key-store.test.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/keys/key-store.test.ts
@@ -48,6 +48,7 @@ function transientError(name: string): Error {
class DynamoBoundary {
public readonly items = new Map<string, DynamoItem>()
public readSizes: number[] = []
+ public readKeys: string[][] = []
public writes = 0
public writeRequests = 0
public deferWrites = 0
@@ -79,6 +80,7 @@ class DynamoBoundary {
throw new Error('DynamoDB sort key exceeds 1024 bytes')
}
this.readSizes.push(keys.length)
+ this.readKeys.push(keys.map((key) => JSON.stringify([key.pk.S, key.sk.S])))
return Promise.resolve({
Responses: {
[table]: keys.flatMap((key) => {
@@ -204,6 +206,22 @@ describe('ML session key batches', () => {
expect([...boundary.writeBatchSizes].sort((a, b) => b - a)).toEqual([25, 25, 11])
})
+ it('reads every row a batch needs in one pass', async () => {
+ const readsBefore = boundary.readSizes.length
+ await store.prepare([session])
+ // The team block, the session key and the team image key are known up front, so they go in one request.
+ expect(boundary.readSizes.length - readsBefore).toBe(1)
+ expect(new Set(boundary.readKeys.at(-1))).toEqual(
+ new Set(
+ [
+ teamBlockId(session.teamId),
+ sessionKeyId(session.teamId, session.sessionId),
+ imageKeyId(session.teamId, '2025-09'),
+ ].map(tableKeyString)
+ )
+ )
+ })
+
it('commits concurrent new sessions without conditional failures', async () => {
await (await store.prepare([session])).commit()
const identities = Array.from({ length: 120 }, (_, index) => ({
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/keys/key-store.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/keys/key-store.ts
index 39b1828aeae3..741682b32caa 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/keys/key-store.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/keys/key-store.ts
@@ -80,9 +80,11 @@ export class MlKeyBatch {
public async read(deadline?: AbortSignal): Promise<void> {
this.keys.clear()
+ // The image key is the session's start month, so every row this batch needs is known before the first read.
const initial = this.identities.flatMap((identity) => [
teamBlockId(identity.teamId),
sessionKeyId(identity.teamId, identity.sessionId),
+ imageKeyId(identity.teamId, sessionStartMonth(identity.sessionId)),
])
this.state = await this.db.read(initial, deadline)
const keyIdentities = new Map<string, MlKeyIdentity>()
@@ -102,10 +104,6 @@ export class MlKeyBatch {
keyIdentities.set(tableKeyString(storedKeyId(keyIdentity)), keyIdentity)
}
}
- const remaining = [...keyIdentities.values()].filter((identity) => !identity.sessionId).map(storedKeyId)
- for (const [id, item] of await this.db.read(remaining, deadline)) {
- this.state.set(id, item)
- }
const unusable: MlStoredKeyMismatch[] = []
await Promise.all(
[...keyIdentities].map(async ([id, identity]) => {
diff --git a/products/ai_training/docs/replay-data.md b/products/ai_training/docs/replay-data.md
index 7322c88b6095..643facad9e5a 100644
--- a/products/ai_training/docs/replay-data.md
+++ b/products/ai_training/docs/replay-data.md
@@ -48,7 +48,7 @@ The envelope is JSON with `v`, `context`, `nonce` (12 bytes, base64) and `cipher
Ingestion processes key state in batches:
-1. Bulk-read session keys, team blocks, and image keys.
+1. Bulk-read session keys, team blocks, and image keys in one pass. A session's start month names its image key, so the batch knows every row before it reads.
2. Resolve keys in memory while processing the batch.
3. Write each new key's month index entry, then the key with a conditional put.
4. Adopt the key a competing writer stored, which the refused put returns, then publish replay blocks or image messages.