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(replay): throttle rasterizer pickup on pod memory and cpu (#108389)

Tue Haulund6749e278e3d2
diff --git a/nodejs/src/session-replay/recording-rasterizer/README.md b/nodejs/src/session-replay/recording-rasterizer/README.md
index f97827077ff5..ff1ed475656e 100644
--- a/nodejs/src/session-replay/recording-rasterizer/README.md
+++ b/nodejs/src/session-replay/recording-rasterizer/README.md
@@ -70,15 +70,18 @@ and starts the container with volume mounts for fast iteration.
Key environment variables (see `config.ts` for full list):
-| Variable | Default | Description |
-| --------------------------- | ------- | ----------------------------------------------------- |
-| `SCREENSHOT_FORMAT` | `jpeg` | Screenshot format for frame capture (`jpeg` or `png`) |
-| `SCREENSHOT_JPEG_QUALITY` | `80` | JPEG quality (1-100), only used when format is `jpeg` |
-| `MAX_CONCURRENT_ACTIVITIES` | `4` | Max parallel recording activities |
-| `BROWSER_RECYCLE_AFTER` | `100` | Recycle Chromium after N page uses |
-| `MAX_IDLE_BROWSERS` | `2` | Warm Chromiums kept alive; extras close on release |
-| `CAPTURE_BROWSER_LOGS` | `0` | Forward browser console/error logs to worker logger |
-| `ENABLE_PLAYER_CSP` | `1` | Script-locking CSP on the player page (`0` disables) |
+| Variable | Default | Description |
+| --------------------------- | ------- | ------------------------------------------------------------------ |
+| `SCREENSHOT_FORMAT` | `jpeg` | Screenshot format for frame capture (`jpeg` or `png`) |
+| `SCREENSHOT_JPEG_QUALITY` | `80` | JPEG quality (1-100), only used when format is `jpeg` |
+| `MAX_CONCURRENT_ACTIVITIES` | `4` | Max parallel activities; fewer while memory or CPU is above target |
+| `TUNER_TARGET_MEMORY_USAGE` | `0.7` | Above this fraction of pod memory, take only the first activity |
+| `TUNER_TARGET_CPU_USAGE` | `0.9` | Above this fraction of pod CPU, take only the first activity |
+| `TUNER_RAMP_THROTTLE_MS` | `10000` | Minimum wait between new activity slots above the first |
+| `BROWSER_RECYCLE_AFTER` | `100` | Recycle Chromium after N page uses |
+| `MAX_IDLE_BROWSERS` | `2` | Warm Chromiums kept alive; extras close on release |
+| `CAPTURE_BROWSER_LOGS` | `0` | Forward browser console/error logs to worker logger |
+| `ENABLE_PLAYER_CSP` | `1` | Script-locking CSP on the player page (`0` disables) |
Egress from the browser and the S3 client is routed through the proxy in `HTTPS_PROXY`/`HTTP_PROXY`.
In production a missing proxy URL is a startup error; `RASTERIZER_USE_PROXY=false` explicitly disables containment.
diff --git a/nodejs/src/session-replay/recording-rasterizer/config.ts b/nodejs/src/session-replay/recording-rasterizer/config.ts
index 17a4a9cb2009..14e276ab678d 100644
--- a/nodejs/src/session-replay/recording-rasterizer/config.ts
+++ b/nodejs/src/session-replay/recording-rasterizer/config.ts
@@ -12,6 +12,11 @@ export function parsePositiveInt(raw: string | undefined, fallback: number): num
return Number.isFinite(n) && n > 0 ? n : fallback
}
+export function parseFraction(raw: string | undefined, fallback: number): number {
+ const n = parseFloat(raw ?? '')
+ return Number.isFinite(n) && n > 0 && n <= 1 ? n : fallback
+}
+
export const config = {
// Temporal
temporalHost: process.env.TEMPORAL_HOST || '127.0.0.1',
@@ -25,6 +30,10 @@ export const config = {
// Worker
logLevel: (process.env.LOG_LEVEL || 'info') as 'debug' | 'info' | 'warn' | 'error',
maxConcurrentActivities: parsePositiveInt(process.env.MAX_CONCURRENT_ACTIVITIES, 4),
+ // Above these fractions of the cgroup limit, the worker takes no activity beyond its first slot.
+ tunerTargetMemoryUsage: parseFraction(process.env.TUNER_TARGET_MEMORY_USAGE, 0.7),
+ tunerTargetCpuUsage: parseFraction(process.env.TUNER_TARGET_CPU_USAGE, 0.9),
+ tunerRampThrottleMs: parsePositiveInt(process.env.TUNER_RAMP_THROTTLE_MS, 10_000),
browserRecycleAfter: parsePositiveInt(process.env.BROWSER_RECYCLE_AFTER, 100),
// Browsers held warm beyond this are closed on release: each idle Chromium pins 100-250MB RSS,
// and a pod that once ran at full concurrency would otherwise keep that footprint forever.
diff --git a/nodejs/src/session-replay/recording-rasterizer/temporal/worker.ts b/nodejs/src/session-replay/recording-rasterizer/temporal/worker.ts
index 6635d2e96178..fa95545886b6 100644
--- a/nodejs/src/session-replay/recording-rasterizer/temporal/worker.ts
+++ b/nodejs/src/session-replay/recording-rasterizer/temporal/worker.ts
@@ -133,7 +133,20 @@ async function main(): Promise<void> {
namespace: config.temporalNamespace,
taskQueue: config.taskQueue,
activities: createActivities(pool, playerHtml),
- maxConcurrentActivityTaskExecutions: config.maxConcurrentActivities,
+ // A render's memory grows for minutes after pickup, so a fixed slot count keeps taking work on a
+ // pod that is already near its limit. The tuner stops taking work above these targets, and the
+ // ramp throttle gives each new render time to show its memory before the next slot opens.
+ tuner: {
+ tunerOptions: {
+ targetMemoryUsage: config.tunerTargetMemoryUsage,
+ targetCpuUsage: config.tunerTargetCpuUsage,
+ },
+ activityTaskSlotOptions: {
+ minimumSlots: 1,
+ maximumSlots: config.maxConcurrentActivities,
+ rampThrottle: config.tunerRampThrottleMs,
+ },
+ },
dataConverter: config.secretKey
? { payloadCodecs: [new EncryptionCodec(config.secretKey, config.fallbackKeys)] }
: undefined,