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.

chore(ai-research): allow sixteen image fetch consumers per pod (#98582)

Robbieab51c5daeabf
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/IMPLEMENTATION_NOTES.md b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/IMPLEMENTATION_NOTES.md
index 3a46b97414be..d803c8ceadcc 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/IMPLEMENTATION_NOTES.md
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/IMPLEMENTATION_NOTES.md
@@ -40,7 +40,7 @@ The queue tracks the remaining request slots from active and waiting jobs. It ap
The frontier consumer uses cooperative rebalancing. Its revoke path drains active work before it releases assigned partitions.
-Each image-fetch worker creates two Kafka group members by default. A local joiner combines their batches before it starts a fetch pass. Group assignments do not overlap, so each ready member adds a different partition. The joiner processes available batches when its join window ends. A later group can run concurrently instead of spending its Kafka poll interval behind a full pass. Shared request limits still bound total network concurrency. The worker divides the existing Kafka prefetch memory budget across its group members. During a mixed-version rollout, pods with more group members can receive more partitions until the rollout finishes. `SESSION_RECORDING_ML_IMAGE_FETCH_TARGET_PARTITIONS_PER_BATCH` accepts targets from one to four for worker-count experiments.
+Each image-fetch worker creates two Kafka group members by default. A local joiner combines their batches before it starts a fetch pass. Group assignments do not overlap, and each member can own multiple partitions. The joiner processes available batches when its join window ends. A later group can run concurrently instead of spending its Kafka poll interval behind a full pass. Shared request limits still bound total network concurrency. The worker divides a 100 MiB Kafka prefetch budget across its group members, with a minimum of 25 MiB per member. This preserves the queue sizes for one to four members and allows a default maximum-sized record in each queue. Sixteen members have a combined prefetch budget of 400 MiB. During a mixed-version rollout, pods with more group members can receive more partitions until the rollout finishes. `SESSION_RECORDING_ML_IMAGE_FETCH_TARGET_PARTITIONS_PER_BATCH` accepts targets from one to sixteen for worker-count experiments.
Retry jobs use 1-minute, 10-minute, and 1-hour Kafka topics. The topics use broker append timestamps.
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/image-fetch-batch-joiner.test.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/image-fetch-batch-joiner.test.ts
index 0c21aec6c1f1..40e01c5fb5ef 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/image-fetch-batch-joiner.test.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/image-fetch-batch-joiner.test.ts
@@ -26,30 +26,35 @@ describe('ImageFetchBatchJoiner', () => {
beforeEach(() => jest.useFakeTimers())
afterEach(() => jest.useRealTimers())
- it('joins batches from two consumers before processing', async () => {
+ it.each([2, 16])('joins batches from %i consumers before processing', async (consumerCount) => {
const processBatch = jest.fn(() => Promise.resolve())
- const joiner = new ImageFetchBatchJoiner(2, processBatch)
+ const joiner = new ImageFetchBatchJoiner(consumerCount, processBatch)
- const first = joiner.handleBatch([message(0)])
+ const messages = Array.from({ length: consumerCount }, (_, partition) => message(partition))
+ const pending = messages.slice(0, -1).map((item) => joiner.handleBatch([item]))
expect(processBatch).not.toHaveBeenCalled()
- const second = joiner.handleBatch([message(1)])
+ pending.push(joiner.handleBatch([messages[consumerCount - 1]]))
- await Promise.all([first, second])
- expect(processBatch).toHaveBeenCalledWith([message(0), message(1)])
+ await Promise.all(pending)
+ expect(processBatch).toHaveBeenCalledTimes(1)
+ expect(processBatch).toHaveBeenCalledWith(messages)
})
- it('processes one available batch when the join window ends', async () => {
- const processBatch = jest.fn(() => Promise.resolve())
- const joiner = new ImageFetchBatchJoiner(2, processBatch)
-
- const pending = joiner.handleBatch([message(0)])
- await jest.advanceTimersByTimeAsync(IMAGE_FETCH_BATCH_JOIN_TIMEOUT_MS - 1)
- expect(processBatch).not.toHaveBeenCalled()
- await jest.advanceTimersByTimeAsync(1)
-
- await pending
- expect(processBatch).toHaveBeenCalledWith([message(0)])
- })
+ it.each([2, 16])(
+ 'processes a partial batch with a target of %i when the join window ends',
+ async (consumerCount) => {
+ const processBatch = jest.fn(() => Promise.resolve())
+ const joiner = new ImageFetchBatchJoiner(consumerCount, processBatch)
+
+ const pending = joiner.handleBatch([message(0)])
+ await jest.advanceTimersByTimeAsync(IMAGE_FETCH_BATCH_JOIN_TIMEOUT_MS - 1)
+ expect(processBatch).not.toHaveBeenCalled()
+ await jest.advanceTimersByTimeAsync(1)
+
+ await pending
+ expect(processBatch).toHaveBeenCalledWith([message(0)])
+ }
+ )
it('rejects active and pending consumers when processing fails', async () => {
const error = new Error('fetch pass failed')
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/image-fetch-batch-joiner.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/image-fetch-batch-joiner.ts
index 8e8250619d1d..9f89c1a861ab 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/image-fetch-batch-joiner.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/image-fetch-batch-joiner.ts
@@ -1,7 +1,7 @@
import { Message } from 'node-rdkafka'
export const IMAGE_FETCH_BATCH_JOIN_TIMEOUT_MS = 500
-export const MAX_IMAGE_FETCH_BATCHES_PER_PASS = 4
+export const MAX_IMAGE_FETCH_BATCHES_PER_PASS = 16
type BatchProcessor = (messages: Message[]) => Promise<void>
diff --git a/nodejs/src/servers/ingestion-session-replay-ml-image-fetch-server.test.ts b/nodejs/src/servers/ingestion-session-replay-ml-image-fetch-server.test.ts
index 1a083a870a19..764316e69a24 100644
--- a/nodejs/src/servers/ingestion-session-replay-ml-image-fetch-server.test.ts
+++ b/nodejs/src/servers/ingestion-session-replay-ml-image-fetch-server.test.ts
@@ -6,9 +6,11 @@ import { buildMlMirrorServerConfig } from './ingestion-session-replay-ml-mirror-
describe('image fetch consumer wiring', () => {
it.each([
- ['the default', {}, 2],
- ['an explicit override', { SESSION_RECORDING_ML_IMAGE_FETCH_TARGET_PARTITIONS_PER_BATCH: 4 }, 4],
- ])('creates %s number of Kafka group members', (_name, overrides, expectedConsumers) => {
+ ['the default', {}, 2, 102_400],
+ ['four consumers', { SESSION_RECORDING_ML_IMAGE_FETCH_TARGET_PARTITIONS_PER_BATCH: 4 }, 4, 102_400],
+ ['eight consumers', { SESSION_RECORDING_ML_IMAGE_FETCH_TARGET_PARTITIONS_PER_BATCH: 8 }, 8, 204_800],
+ ['sixteen consumers', { SESSION_RECORDING_ML_IMAGE_FETCH_TARGET_PARTITIONS_PER_BATCH: 16 }, 16, 409_600],
+ ])('creates %s number of Kafka group members', (_name, overrides, expectedConsumers, expectedQueueBudget) => {
const serverConfig = buildMlMirrorServerConfig(overrides)
const consumerConfigs = buildImageFetchConsumerConfigs(serverConfig)
const consumerOverrides = buildImageFetchConsumerOverrides(serverConfig, consumerConfigs.length)
@@ -17,16 +19,18 @@ describe('image fetch consumer wiring', () => {
expect(consumerConfigs.map((config) => config.groupId)).toEqual(
Array(expectedConsumers).fill(serverConfig.SESSION_RECORDING_ML_IMAGE_FETCH_GROUP_ID)
)
- expect(Number(consumerOverrides['queued.max.messages.kbytes']) * expectedConsumers).toBeLessThanOrEqual(102_400)
+ expect(Number(consumerOverrides['queued.max.messages.kbytes']) * expectedConsumers).toBeLessThanOrEqual(
+ expectedQueueBudget
+ )
expect(Number(consumerOverrides['queued.max.messages.kbytes']) * expectedConsumers).toBeGreaterThanOrEqual(
- 102_400 - expectedConsumers
+ expectedQueueBudget - expectedConsumers
)
expect(Number(consumerOverrides['queued.max.messages.kbytes']) * 1024).toBeGreaterThanOrEqual(
serverConfig.SESSION_RECORDING_ML_IMAGE_FETCH_MAX_IMAGE_BYTES + 64 * 1024
)
})
- it.each([0, -1, 1.5, 5, Number.NaN, Number.MAX_SAFE_INTEGER + 1])(
+ it.each([0, -1, 1.5, 17, Number.NaN, Number.MAX_SAFE_INTEGER + 1])(
'refuses invalid target partition count %p',
(targetPartitionsPerBatch) => {
const serverConfig = buildMlMirrorServerConfig({
@@ -34,7 +38,7 @@ describe('image fetch consumer wiring', () => {
})
expect(() => buildImageFetchConsumerConfigs(serverConfig)).toThrow(
- 'image fetch batch target must be an integer between 1 and 4'
+ 'image fetch batch target must be an integer between 1 and 16'
)
}
)
diff --git a/nodejs/src/servers/ingestion-session-replay-ml-image-fetch-server.ts b/nodejs/src/servers/ingestion-session-replay-ml-image-fetch-server.ts
index 4522f97d1146..1359209bf175 100644
--- a/nodejs/src/servers/ingestion-session-replay-ml-image-fetch-server.ts
+++ b/nodejs/src/servers/ingestion-session-replay-ml-image-fetch-server.ts
@@ -54,6 +54,7 @@ import {
*/
const STORE_BATCH_BUDGET_MS = 50_000
const IMAGE_FETCH_KAFKA_QUEUE_BUDGET_KBYTES = 102_400
+const IMAGE_FETCH_MIN_CONSUMER_QUEUE_KBYTES = 25_600
/** Matches MAX_URL_LEN in the crate, which is what the collector applied to the first candidate. */
const MAX_REDIRECT_URL_LENGTH = 2048
@@ -174,15 +175,17 @@ export function buildImageFetchConsumerOverrides(
return {
'fetch.message.max.bytes': maximumRecordBytes,
'max.partition.fetch.bytes': maximumRecordBytes,
- 'queued.max.messages.kbytes': Math.floor(IMAGE_FETCH_KAFKA_QUEUE_BUDGET_KBYTES / consumerCount),
+ 'queued.max.messages.kbytes': Math.max(
+ IMAGE_FETCH_MIN_CONSUMER_QUEUE_KBYTES,
+ Math.floor(IMAGE_FETCH_KAFKA_QUEUE_BUDGET_KBYTES / consumerCount)
+ ),
}
}
/**
* The image fetch lane.
*
- * It has its own deployment because it waits on network IO and wants many small pods, where the
- * scrub sidecar it feeds uses CPU and ML models and wants few large ones.
+ * It scales separately because fetching waits on network IO while scrubbing needs CPU and ML models.
*/
export class IngestionSessionReplayMlImageFetchServer implements NodeServer {
readonly lifecycle: ServerLifecycle