: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
perf(ai-research): poll 150 messages per image-scrub batch (#103785)
Robbiea68a1b0fffd4
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/README.md b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/README.mdindex 5a5f6fd98f4a..9c4617d202e3 100644--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/README.md+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/README.md@@ -19,7 +19,9 @@ There is no batch time limit and no drop path: every message a poll batch takesThe waiting _is_ the backpressure. A batch that spends longer on a jammed sidecar calls `consume()` that much later, so the consumer paces itself to whatever the sidecar can execute without needing to pause partitions explicitly.Lag grows while that happens, which is correct and is what the drain-time panels on the dashboard are for.-Because the batch has no time limit, its duration is set by how many images it holds, so this lane runs a small `CONSUMER_BATCH_SIZE` (50, against a default of 500).+Because the batch has no time limit, its duration is set by how many images it holds, so this lane runs a small `CONSUMER_BATCH_SIZE` (150, against a default of 500).+It is not smaller than that because every batch ends with a window drain, where the last few images finish unevenly while the other scrub slots idle, and a larger batch spreads that fixed cost over more images.+The consumer caps the poll below the configured size so that every image can time out once at the sidecar and the batch still returns inside `max.poll.interval.ms`, with a fifth of the interval kept for the key read, the window drain and any wait on the write lane: with a 45s scrub timeout that is five waves of `SESSION_RECORDING_ML_IMAGE_SCRUB_SCRUB_CONCURRENCY` images, 70 at the production concurrency of 14.A batch that outlives `max.poll.interval.ms` (300s) gets the pod evicted mid-batch, and that is not a clean retry: the evicted pod loses the offsets for work it already did, and the partition lands on a pod whose sidecar is equally busy and redoes the same images, so offered load rises while throughput falls.Keeping batches far inside the interval is what stops ordinary saturation reaching that point.If a revoke does land mid-batch, the batch stops as soon as a write finds it no longer owns the partitions, rather than scrubbing on and writing a second shard for a span the new owner is already writing.diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/config.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/config.tsindex c1eeb1833f73..77e1c031f619 100644--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/config.ts+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/config.ts@@ -207,7 +207,14 @@ export type MlMirrorConfig = {SESSION_RECORDING_ML_IMAGE_SCRUB_SCRUB_TIMEOUT_MS: number/** Where images the sidecar cannot process are parked so they stop holding their partition. */SESSION_RECORDING_ML_IMAGE_SCRUB_DLQ_TOPIC: string- /** Messages per poll. Bounds batch wall time against Kafka's max.poll.interval.ms (300s). */+ /**+ * Messages per poll. Bounds batch wall time against Kafka's max.poll.interval.ms (300s), and sets+ * how much of a batch the window drain at its end costs: the last scrubConcurrency images finish+ * unevenly with slots idling, so a larger batch amortizes that tail over more images. A saturated+ * sidecar scrubs a 150-message batch in tens of seconds, far inside the interval. The server caps+ * the poll below this so that every image can time out once at the sidecar and the batch still+ * returns inside the interval (boundedImageScrubBatchSize).+ */SESSION_RECORDING_ML_IMAGE_SCRUB_BATCH_SIZE: number// Per-write timeout (the S3 client has no built-in one). A hand-off writes its shard groups concurrently, each as a shard, an index and per-image lookups, so a hand-off bounds at 3x this plus the lookup budget.SESSION_RECORDING_ML_IMAGE_SCRUB_S3_WRITE_TIMEOUT_MS: number@@ -283,7 +290,7 @@ export function getDefaultMlMirrorConfig(): MlMirrorConfig {SESSION_RECORDING_ML_IMAGE_SCRUB_PRODUCED_REF_CACHE_MAX: 500_000,SESSION_RECORDING_ML_IMAGE_SCRUB_SCRUB_TIMEOUT_MS: 45 * 1000,SESSION_RECORDING_ML_IMAGE_SCRUB_DLQ_TOPIC: KAFKA_SESSION_REPLAY_IMAGE_SCRUB_DLQ,- SESSION_RECORDING_ML_IMAGE_SCRUB_BATCH_SIZE: 50,+ SESSION_RECORDING_ML_IMAGE_SCRUB_BATCH_SIZE: 150,SESSION_RECORDING_ML_IMAGE_SCRUB_S3_WRITE_TIMEOUT_MS: 30 * 1000,}}diff --git a/nodejs/src/servers/ingestion-session-replay-ml-image-scrub-server.test.ts b/nodejs/src/servers/ingestion-session-replay-ml-image-scrub-server.test.tsindex bf94c51c03e5..db992f88e7b0 100644--- a/nodejs/src/servers/ingestion-session-replay-ml-image-scrub-server.test.ts+++ b/nodejs/src/servers/ingestion-session-replay-ml-image-scrub-server.test.ts@@ -1,4 +1,8 @@-import { IngestionSessionReplayMlImageScrubServer } from './ingestion-session-replay-ml-image-scrub-server'+import {+ IngestionSessionReplayMlImageScrubServer,+ buildImageScrubConsumerConfig,+} from './ingestion-session-replay-ml-image-scrub-server'+import { buildMlMirrorServerConfig } from './ml-mirror-server-config'class TestImageScrubServer extends IngestionSessionReplayMlImageScrubServer {public override startServices(): Promise<void> {@@ -19,6 +23,26 @@ describe('image scrub server startup', () => {})+ it.each([+ // A batch is ceil(size / concurrency) waves of at most one scrub timeout, and every image must+ // be able to time out once inside the 240s of Kafka's 300s max.poll.interval.ms that scrub+ // timeouts may use.+ { batchSize: 150, concurrency: 14, timeoutMs: 45_000, expected: 70 },+ { batchSize: 150, concurrency: 8, timeoutMs: 45_000, expected: 40 },+ { batchSize: 50, concurrency: 14, timeoutMs: 45_000, expected: 50 },+ { batchSize: 150, concurrency: 14, timeoutMs: 15_000, expected: 150 },+ ])(+ 'polls at most the messages that can each time out once inside the poll interval (%p)',+ ({ batchSize, concurrency, timeoutMs, expected }) => {+ const config = buildMlMirrorServerConfig({+ SESSION_RECORDING_ML_IMAGE_SCRUB_BATCH_SIZE: batchSize,+ SESSION_RECORDING_ML_IMAGE_SCRUB_SCRUB_CONCURRENCY: concurrency,+ SESSION_RECORDING_ML_IMAGE_SCRUB_SCRUB_TIMEOUT_MS: timeoutMs,+ })+ expect(buildImageScrubConsumerConfig(config).fetchBatchSize).toBe(expected)+ }+ )+it('allows a disabled DLQ without a key manager and continues to the S3 configuration check', async () => {const server = new TestImageScrubServer({AI_RESEARCH_REPLAY_KEY_TABLE: '',diff --git a/nodejs/src/servers/ingestion-session-replay-ml-image-scrub-server.ts b/nodejs/src/servers/ingestion-session-replay-ml-image-scrub-server.tsindex 1226326c1ca0..1be06a3cd669 100644--- a/nodejs/src/servers/ingestion-session-replay-ml-image-scrub-server.ts+++ b/nodejs/src/servers/ingestion-session-replay-ml-image-scrub-server.ts@@ -21,6 +21,27 @@ import { MlMirrorConsumerServer } from './ml-mirror-consumer-server'// we refresh the heartbeat this often during it. Must stay under CONSUMER_MAX_HEARTBEAT_INTERVAL_MS (30s).const BATCH_HEARTBEAT_INTERVAL_MS = 10_000+// librdkafka's max.poll.interval.ms as consumer-v1 sets it; a batch that outlives it gets the pod evicted mid-batch.+const KAFKA_MAX_POLL_INTERVAL_MS = 300_000+// The share of the poll interval that scrub timeouts may use. The rest covers the key read at the+// start of a batch, the window drain at its end and a wait for the write lane to have room.+const SCRUB_TIMEOUT_BUDGET_MS = 0.8 * KAFKA_MAX_POLL_INTERVAL_MS++/**+ * The most messages a poll may hold so that every image can time out once at the sidecar and the+ * batch still returns inside max.poll.interval.ms. The window scrubs scrubConcurrency images at a+ * time, so a batch is ceil(size / concurrency) waves of at most one scrub timeout each. The configured+ * size is what a healthy sidecar gets; this cap is what a degraded one is held to.+ */+export function boundedImageScrubBatchSize(config: IngestionSessionReplayMlMirrorServerConfig): number {+ const timeoutWavesInBudget = Math.floor(+ SCRUB_TIMEOUT_BUDGET_MS / config.SESSION_RECORDING_ML_IMAGE_SCRUB_SCRUB_TIMEOUT_MS+ )+ const imagesThatCanEachTimeOutOnce =+ timeoutWavesInBudget * config.SESSION_RECORDING_ML_IMAGE_SCRUB_SCRUB_CONCURRENCY+ return Math.max(1, Math.min(config.SESSION_RECORDING_ML_IMAGE_SCRUB_BATCH_SIZE, imagesThatCanEachTimeOutOnce))+}+export function requireS3Client(client: S3Client | null): S3Client {if (!client) {throw new Error('SESSION_RECORDING_V2_S3_* must be configured for the image-scrub consumer')@@ -42,7 +63,7 @@ export function buildImageScrubConsumerConfig(config: IngestionSessionReplayMlMi// partition lands on a pod whose sidecar is just as busy and redoes the same images, so// offered load rises while throughput falls. Set here rather than as a deployment value so// the bound cannot drift away from the design that needs it.- fetchBatchSize: config.SESSION_RECORDING_ML_IMAGE_SCRUB_BATCH_SIZE,+ fetchBatchSize: boundedImageScrubBatchSize(config),}}