: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
feat(session-replay): let the ml mirror run on its own redis (#80728)
Robbie6c2002c795d5
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/config.test.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/config.test.tsindex 57878ebe5cfc..6d28b129dbdf 100644--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/config.test.ts+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/config.test.ts@@ -1,12 +1,37 @@-import { resolveMlAnonymizeMaxConcurrency } from './config'--describe('resolveMlAnonymizeMaxConcurrency', () => {- it.each([- ['explicit value passes through verbatim, even above the pool', 8, 3, 4, 8],- ['sentinel resolves to available CPUs when below the pool', 0, 3, 4, 3],- ['sentinel is capped by the threadpool size', 0, 16, 4, 4],- ['sentinel never resolves below 1', -1, 0, 0, 1],- ])('%s', (_name, configured, cpus, poolSize, expected) => {- expect(resolveMlAnonymizeMaxConcurrency(configured, cpus, poolSize)).toBe(expected)+import { resolveMlAnonymizeMaxConcurrency, resolveMlMirrorRedisConnection } from './config'++describe('ml-mirror config', () => {+ describe('resolveMlAnonymizeMaxConcurrency', () => {+ it.each([+ ['explicit value passes through verbatim, even above the pool', 8, 3, 4, 8],+ ['sentinel resolves to available CPUs when below the pool', 0, 3, 4, 3],+ ['sentinel is capped by the threadpool size', 0, 16, 4, 4],+ ['sentinel never resolves below 1', -1, 0, 0, 1],+ ])('%s', (_name, configured, cpus, poolSize, expected) => {+ expect(resolveMlAnonymizeMaxConcurrency(configured, cpus, poolSize)).toBe(expected)+ })+ })++ describe('resolveMlMirrorRedisConnection', () => {+ const config = (host: string) => ({+ SESSION_RECORDING_ML_REDIS_HOST: host,+ SESSION_RECORDING_ML_REDIS_PORT: 6380,+ SESSION_RECORDING_REDIS_TIMEOUT_MS: 1000,+ })++ it.each([+ ['unset host stays on the shared cluster', ''],+ ['whitespace-only host stays on the shared cluster', ' '],+ ])('%s', (_name, host) => {+ expect(resolveMlMirrorRedisConnection(config(host))).toBeNull()+ })++ it('a configured host moves the lane onto its own instance', () => {+ expect(resolveMlMirrorRedisConnection(config('ml.cache.example'))).toEqual({+ url: 'ml.cache.example',+ options: { port: 6380, commandTimeout: 1000 },+ name: 'session-recording-ml-redis',+ })+ })})})diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/config.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/config.tsindex 3178072c0307..4b83c853a2a8 100644--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/config.ts+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/config.ts@@ -1,6 +1,7 @@import os from 'node:os'import { KAFKA_SESSION_REPLAY_IMAGE_SCRUB_DLQ } from '~/common/config/kafka-topics'+import { RedisConnectionConfig } from '~/common/utils/db/redis'export type MlMirrorConfig = {/** S3 key prefix under the bucket for the block-metadata Parquet dataset (used by the sink). */@@ -25,6 +26,17 @@ export type MlMirrorConfig = {/** Row cap that forces a flush before the interval elapses (bounds the sink's memory). */SESSION_RECORDING_ML_PARQUET_MAX_ROWS: number+ /**+ * Host of a Redis dedicated to this lane. Empty sends the mirror to the shared session-replay+ * cluster instead, so clearing this value is the supported rollback.+ *+ * The mirror's per-session keys otherwise sit on the cluster that also serves the primary replay+ * lane, so memory pressure from the mirror reaches the lane that gates replay ingestion. Pointing+ * the mirror at its own instance removes that path.+ */+ SESSION_RECORDING_ML_REDIS_HOST: string+ SESSION_RECORDING_ML_REDIS_PORT: number+/*** Produce collected original images to the scrub topic. Enabling changes the mirrored JSONL* shape: image fields carry `image:<pseudoTeam>:<hash>` refs instead of blurred data URIs, so@@ -95,6 +107,8 @@ export function getDefaultMlMirrorConfig(): MlMirrorConfig {SESSION_RECORDING_ML_PARQUET_SINK_GROUP_ID: 'session-replay-ml-parquet-sink',SESSION_RECORDING_ML_PARQUET_FLUSH_INTERVAL_MS: 60 * 1000,SESSION_RECORDING_ML_PARQUET_MAX_ROWS: 250_000,+ SESSION_RECORDING_ML_REDIS_HOST: '',+ SESSION_RECORDING_ML_REDIS_PORT: 6379,SESSION_RECORDING_ML_IMAGE_SCRUB_PRODUCER_ENABLED: false,SESSION_RECORDING_ML_IMAGE_SCRUB_GROUP_ID: 'session-replay-ml-image-scrub',SESSION_RECORDING_ML_IMAGE_SCRUB_PREFIX: 'scrubbed-images',@@ -130,3 +144,26 @@ export function resolveMlAnonymizeMaxConcurrency(}return Math.max(1, Math.min(availableParallelism, uvThreadpoolSize))}++/**+ * A host of whitespace counts as unset: chart values render to an empty string when the underlying+ * secret key is missing, and connecting to `""` would fail every command rather than return `null`.+ */+export function resolveMlMirrorRedisConnection(+ config: Pick<MlMirrorConfig, 'SESSION_RECORDING_ML_REDIS_HOST' | 'SESSION_RECORDING_ML_REDIS_PORT'> & {+ SESSION_RECORDING_REDIS_TIMEOUT_MS: number+ }+): RedisConnectionConfig | null {+ const host = config.SESSION_RECORDING_ML_REDIS_HOST.trim()+ if (!host) {+ return null+ }+ return {+ url: host,+ options: {+ port: config.SESSION_RECORDING_ML_REDIS_PORT,+ commandTimeout: config.SESSION_RECORDING_REDIS_TIMEOUT_MS,+ },+ name: 'session-recording-ml-redis',+ }+}diff --git a/nodejs/src/servers/ingestion-session-replay-ml-mirror-server.ts b/nodejs/src/servers/ingestion-session-replay-ml-mirror-server.tsindex 08a11f7942d9..34c1cdf927bd 100644--- a/nodejs/src/servers/ingestion-session-replay-ml-mirror-server.ts+++ b/nodejs/src/servers/ingestion-session-replay-ml-mirror-server.ts@@ -23,6 +23,7 @@ import {MlMirrorConfig,getDefaultMlMirrorConfig,resolveMlAnonymizeMaxConcurrency,+ resolveMlMirrorRedisConnection,} from '~/ingestion/pipelines/sessionreplay/ml-mirror/config'import { MlBlockMetadataSink } from '~/ingestion/pipelines/sessionreplay/ml-mirror/ml-block-metadata-sink'import { createMlMirrorReplayPipeline } from '~/ingestion/pipelines/sessionreplay/ml-mirror/ml-mirror-pipeline'@@ -116,7 +117,9 @@ export class IngestionSessionReplayMlMirrorServer implements NodeServer {this.producerRegistry = await createProducerRegistry(this.config.KAFKA_CLIENT_RACK).build(this.config)const outputs = createOutputsRegistry().build(this.producerRegistry, this.config)- const pools = buildSessionReplayRedisPools(this.config)+ // The restriction pool is deliberately not overridden: the event restriction list is+ // configuration another system writes, so every lane has to read the same copy of it.+ const pools = buildSessionReplayRedisPools(this.config, resolveMlMirrorRedisConnection(this.config))this.redisPool = pools.redisPoolthis.restrictionRedisPool = pools.restrictionRedisPooldiff --git a/nodejs/src/servers/ingestion-session-replay-server.ts b/nodejs/src/servers/ingestion-session-replay-server.tsindex 0602d185745d..4083885938a5 100644--- a/nodejs/src/servers/ingestion-session-replay-server.ts+++ b/nodejs/src/servers/ingestion-session-replay-server.ts@@ -2,7 +2,7 @@ import { initializePrometheusLabels } from '~/common/api/router'import { defaultConfig, overrideConfigWithEnv } from '~/common/config/config'import { KafkaProducerRegistry } from '~/common/outputs/kafka-producer-registry'import { PostgresRouter, PostgresRouterConfig } from '~/common/utils/db/postgres'-import { createRedisPoolFromConfig } from '~/common/utils/db/redis'+import { RedisConnectionConfig, createRedisPoolFromConfig } from '~/common/utils/db/redis'import { logger } from '~/common/utils/logger'import {KafkaDownstreamProducerEnvConfig,@@ -54,26 +54,38 @@ export type IngestionSessionReplayServerConfig = BaseServerConfig &'LOG_LEVEL' | 'PLUGIN_SERVER_MODE' | 'HEALTHCHECK_MAX_STALE_SECONDS' | 'KAFKA_HEALTHCHECK_SECONDS'>-/** Builds the session-recording and restriction Redis pools a replay deployment needs. */-export function buildSessionReplayRedisPools(config: IngestionSessionReplayServerConfig): {+/**+ * Builds the session-recording and restriction Redis pools a replay deployment needs.+ *+ * `sessionRecordingConnection` overrides where the session-recording pool points, for a lane that+ * runs against its own instance. The override belongs here rather than at the call site because+ * these pools connect eagerly (`autostart`), so a caller that built both and then discarded one+ * would leave a connected pool that nothing owns and nothing drains on shutdown.+ */+export function buildSessionReplayRedisPools(+ config: IngestionSessionReplayServerConfig,+ sessionRecordingConnection?: RedisConnectionConfig | null+): {redisPool: RedisPoolrestrictionRedisPool: RedisPool} {const redisPool = createRedisPoolFromConfig({- connection: config.POSTHOG_SESSION_RECORDING_REDIS_HOST- ? {- url: config.POSTHOG_SESSION_RECORDING_REDIS_HOST,- options: {- port: config.POSTHOG_SESSION_RECORDING_REDIS_PORT ?? 6379,- commandTimeout: config.SESSION_RECORDING_REDIS_TIMEOUT_MS,- },- name: 'session-recording-redis',- }- : {- url: config.REDIS_URL,- options: { commandTimeout: config.SESSION_RECORDING_REDIS_TIMEOUT_MS },- name: 'session-recording-redis-fallback',- },+ connection:+ sessionRecordingConnection ??+ (config.POSTHOG_SESSION_RECORDING_REDIS_HOST+ ? {+ url: config.POSTHOG_SESSION_RECORDING_REDIS_HOST,+ options: {+ port: config.POSTHOG_SESSION_RECORDING_REDIS_PORT ?? 6379,+ commandTimeout: config.SESSION_RECORDING_REDIS_TIMEOUT_MS,+ },+ name: 'session-recording-redis',+ }+ : {+ url: config.REDIS_URL,+ options: { commandTimeout: config.SESSION_RECORDING_REDIS_TIMEOUT_MS },+ name: 'session-recording-redis-fallback',+ }),poolMinSize: config.REDIS_POOL_MIN_SIZE,poolMaxSize: config.REDIS_POOL_MAX_SIZE,})