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.

feat(replay): make the image-fetch crawl history TTL configurable, default 7 days (#82975)

Danielcaf1e57dcf25
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/crawl-history.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/crawl-history.ts
index 0520f49a36b2..cac19004df0a 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/crawl-history.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/crawl-history.ts
@@ -10,13 +10,6 @@ import { Pool } from 'generic-pool'
*/
const CRAWL_HISTORY_PREFIX = 'imgfetch:seen'
-/**
- * The recrawl interval. Long enough that the key count converges on the distinct-URL count of the
- * window, which is the number that sizes the Redis. Short enough that an entry cannot outlive the
- * phase that wrote it.
- */
-export const CRAWL_HISTORY_TTL_SECONDS = 30 * 24 * 60 * 60
-
/** Bounds one round trip. A poll batch can carry thousands of URLs, and one pipeline holding all of them times out as a unit. */
const MAX_KEYS_PER_ROUND_TRIP = 256
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/url-fetch-consumer.test.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/url-fetch-consumer.test.ts
index fbbc2a530226..5284fdbfd7c4 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/url-fetch-consumer.test.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/url-fetch-consumer.test.ts
@@ -99,6 +99,7 @@ describe('UrlFetchConsumer', () => {
new UrlFetchConsumer(crawlHistory, publisher, {
maxAgeMs: 6 * 60 * 60 * 1000,
dedupMaxRefs,
+ seenTtlSeconds: 7 * 24 * 60 * 60,
dryRun: true,
})
@@ -120,10 +121,31 @@ describe('UrlFetchConsumer', () => {
// The limit arrives from env, where a typo parses to NaN. A NaN limit makes every comparison
// false, so the lane stops shedding a backlog rather than fails.
expect(
- () => new UrlFetchConsumer(crawlHistory, publisher, { maxAgeMs, dedupMaxRefs: 10, dryRun: true })
+ () =>
+ new UrlFetchConsumer(crawlHistory, publisher, {
+ maxAgeMs,
+ dedupMaxRefs: 10,
+ seenTtlSeconds: 604_800,
+ dryRun: true,
+ })
).toThrow('SESSION_RECORDING_ML_IMAGE_FETCH_MAX_AGE_MS')
})
+ it.each([NaN, 0, -1, 1.5, 7, 3599])('refuses to start with a seen TTL of %p', (seenTtlSeconds) => {
+ // The TTL arrives from env, where a typo parses to NaN and a unit suffix truncates: "7d"
+ // parses to 7, and a 7-second TTL empties the ledger as fast as it fills. SET with a NaN
+ // expiry fails per command, so every ledger write would fail while the lane looks healthy.
+ expect(
+ () =>
+ new UrlFetchConsumer(crawlHistory, publisher, {
+ maxAgeMs: 1000,
+ dedupMaxRefs: 10,
+ seenTtlSeconds,
+ dryRun: true,
+ })
+ ).toThrow('SESSION_RECORDING_ML_IMAGE_FETCH_SEEN_TTL_SECONDS')
+ })
+
it('writes one ledger entry per URL it would fetch', async () => {
await consumer.handleBatch([record([url('a'), url('b')])], NOW)
@@ -332,7 +354,7 @@ describe('UrlFetchConsumer', () => {
const fetching = new UrlFetchConsumer(
crawlHistory,
publisher,
- { maxAgeMs: 6 * 60 * 60 * 1000, dedupMaxRefs: 1000, dryRun: false },
+ { maxAgeMs: 6 * 60 * 60 * 1000, dedupMaxRefs: 1000, seenTtlSeconds: 604_800, dryRun: false },
runner
)
@@ -343,7 +365,13 @@ describe('UrlFetchConsumer', () => {
it('refuses to leave dry run without a way to send the requests', () => {
expect(
- () => new UrlFetchConsumer(crawlHistory, publisher, { maxAgeMs: 1000, dedupMaxRefs: 10, dryRun: false })
+ () =>
+ new UrlFetchConsumer(crawlHistory, publisher, {
+ maxAgeMs: 1000,
+ dedupMaxRefs: 10,
+ seenTtlSeconds: 604_800,
+ dryRun: false,
+ })
).toThrow('fetch runner')
})
@@ -368,7 +396,7 @@ describe('UrlFetchConsumer', () => {
const fetching = new UrlFetchConsumer(
crawlHistory,
publisher,
- { maxAgeMs: 6 * 60 * 60 * 1000, dedupMaxRefs: 1000, dryRun: false },
+ { maxAgeMs: 6 * 60 * 60 * 1000, dedupMaxRefs: 1000, seenTtlSeconds: 604_800, dryRun: false },
runner
)
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/url-fetch-consumer.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/url-fetch-consumer.ts
index 861ffef64b9b..cbc870657d79 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/url-fetch-consumer.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/url-fetch-consumer.ts
@@ -4,7 +4,7 @@ import { logger } from '~/common/utils/logger'
import { RefDedupCache } from '~/ingestion/pipelines/sessionreplay/shared/ref-dedup-cache'
import { FetchCandidate, MAX_HOPS, parseCollectedUrlsRecord } from './collected-urls-record'
-import { CRAWL_HISTORY_TTL_SECONDS, CrawlHistoryStore, crawlHistoryKey } from './crawl-history'
+import { CrawlHistoryStore, crawlHistoryKey } from './crawl-history'
import { FetchPass, HOPS_EXHAUSTED } from './fetch-runner'
import { FrontierPublisher } from './frontier-publisher'
import { ImageFetchConsumerMetrics, ImageFetchRequestMetrics, ImageFetchTeamMetrics, UrlDropReason } from './metrics'
@@ -14,6 +14,8 @@ export interface UrlFetchConsumerOptions {
/** The lane drops a URL older than this, so a backlog sheds work instead of fetching stale URLs. */
maxAgeMs: number
dedupMaxRefs: number
+ /** TTL on each crawl history entry, which is both the recrawl interval and the ledger's Redis footprint. */
+ seenTtlSeconds: number
/** While true, no request leaves this process. Everything else still runs, including the crawl history write. */
dryRun: boolean
}
@@ -44,6 +46,16 @@ export class UrlFetchConsumer {
`SESSION_RECORDING_ML_IMAGE_FETCH_MAX_AGE_MS must be a positive number, got ${options.maxAgeMs}`
)
}
+ // A NaN TTL would not disable expiry: SET with EX NaN fails per command, so every ledger
+ // write fails and the lane stops recording while looking healthy. The hour floor catches
+ // unit suffixes, which the env parser truncates rather than rejects: "7d" parses to 7, and
+ // a 7-second TTL empties the ledger as fast as it fills, so nothing dedups.
+ if (!Number.isInteger(options.seenTtlSeconds) || options.seenTtlSeconds < 60 * 60) {
+ throw new Error(
+ `SESSION_RECORDING_ML_IMAGE_FETCH_SEEN_TTL_SECONDS must be an integer of at least 3600, got ` +
+ `${options.seenTtlSeconds} (a unit suffix like "7d" parses as 7: give the value in seconds)`
+ )
+ }
if (!options.dryRun && !runner) {
throw new Error('the image fetch lane needs a fetch runner once dry run is cleared')
}
@@ -265,7 +277,7 @@ export class UrlFetchConsumer {
const keys = candidates.map((candidate) => crawlHistoryKey(candidate.pseudoTeam, candidate.urlHash))
let failed: Set<number>
try {
- failed = (await this.crawlHistory.record(keys, nowMs, CRAWL_HISTORY_TTL_SECONDS)).failed
+ failed = (await this.crawlHistory.record(keys, nowMs, this.options.seenTtlSeconds)).failed
} catch (error) {
ImageFetchConsumerMetrics.incStoreError('write', keys.length)
logger.warn('🌐', 'ml_image_fetch_crawl_history_write_failed', { count: keys.length, error: String(error) })
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/config.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/config.ts
index 9008bbbcf840..93d4d4c55c3a 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/config.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/config.ts
@@ -84,6 +84,17 @@ export type MlMirrorConfig = {
SESSION_RECORDING_ML_IMAGE_FETCH_MAX_AGE_MS: number
/** Capacity of the per-pod seen-ref cache that sits in front of the Redis ledger. */
SESSION_RECORDING_ML_IMAGE_FETCH_DEDUP_MAX_REFS: number
+ /**
+ * TTL on each crawl history entry, so it is both the recrawl interval and the ledger's Redis
+ * footprint: the key count converges on the distinct-URL count of this window, which is the
+ * number that sizes the Redis. An env var because it is the main memory lever for that cluster,
+ * and memory pressure moves faster than a deploy.
+ *
+ * 7 days measures the dry-run dedup hit rate over a window the cluster can hold. Once fetching
+ * is on, this is also how often a recurring image is refetched from the customer's site, so
+ * raising it back up trades Redis memory for outbound politeness.
+ */
+ SESSION_RECORDING_ML_IMAGE_FETCH_SEEN_TTL_SECONDS: number
/** Bounds one round trip to the crawl history, including waiting for a pooled connection, so a Redis stall cannot hold the poll loop. */
SESSION_RECORDING_ML_IMAGE_FETCH_REDIS_TIMEOUT_MS: number
@@ -236,6 +247,7 @@ export function getDefaultMlMirrorConfig(): MlMirrorConfig {
SESSION_RECORDING_ML_IMAGE_FETCH_BATCH_SIZE: 500,
SESSION_RECORDING_ML_IMAGE_FETCH_MAX_AGE_MS: 6 * 60 * 60 * 1000,
SESSION_RECORDING_ML_IMAGE_FETCH_DEDUP_MAX_REFS: 500_000,
+ SESSION_RECORDING_ML_IMAGE_FETCH_SEEN_TTL_SECONDS: 7 * 24 * 60 * 60,
SESSION_RECORDING_ML_IMAGE_FETCH_REDIS_TIMEOUT_MS: 5_000,
SESSION_RECORDING_ML_IMAGE_FETCH_REQUESTS_PER_SECOND: 1,
SESSION_RECORDING_ML_IMAGE_FETCH_BURST: 5,
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 fe7f48724a99..135b2829f5dc 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
@@ -185,6 +185,7 @@ export class IngestionSessionReplayMlImageFetchServer implements NodeServer {
{
maxAgeMs: this.config.SESSION_RECORDING_ML_IMAGE_FETCH_MAX_AGE_MS,
dedupMaxRefs: this.config.SESSION_RECORDING_ML_IMAGE_FETCH_DEDUP_MAX_REFS,
+ seenTtlSeconds: this.config.SESSION_RECORDING_ML_IMAGE_FETCH_SEEN_TTL_SECONDS,
dryRun,
},
dryRun ? undefined : buildFetchRunner(this.config, publisher)