: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
fix(replay): scope session retention cache key by team (#68119)
Paweł Ledwońff7999a3b5ca
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/shared/retention/retention-service.test.ts b/nodejs/src/ingestion/pipelines/sessionreplay/shared/retention/retention-service.test.tsindex 7cebc75e9191..316ec153fd27 100644--- a/nodejs/src/ingestion/pipelines/sessionreplay/shared/retention/retention-service.test.ts+++ b/nodejs/src/ingestion/pipelines/sessionreplay/shared/retention/retention-service.test.ts@@ -79,8 +79,8 @@ describe('RetentionService', () => {expect(results.get(2, 'b')).toEqual({ resolved: true, retentionPeriod: '1y' })expect(mockRedisClient.mget).toHaveBeenCalledTimes(1)expect(mockRedisClient.mget).toHaveBeenCalledWith([- '@posthog/replay/session-retention-a',- '@posthog/replay/session-retention-b',+ '@posthog/replay/session-retention-1-a',+ '@posthog/replay/session-retention-2-b',])expect(mockTeamService.getRetentionPeriodByTeamId).not.toHaveBeenCalled()expect(mockPipeline.set).not.toHaveBeenCalled()@@ -102,13 +102,13 @@ describe('RetentionService', () => {// Each resolved value is written back to its own key with a TTL.expect(mockPipeline.set).toHaveBeenCalledTimes(3)expect(mockPipeline.set).toHaveBeenCalledWith(- '@posthog/replay/session-retention-a',+ '@posthog/replay/session-retention-1-a','30d','EX',24 * 60 * 60)expect(mockPipeline.set).toHaveBeenCalledWith(- '@posthog/replay/session-retention-c',+ '@posthog/replay/session-retention-2-c','1y','EX',24 * 60 * 60@@ -151,13 +151,37 @@ describe('RetentionService', () => {// Only the miss is written back to Redis.expect(mockPipeline.set).toHaveBeenCalledTimes(1)expect(mockPipeline.set).toHaveBeenCalledWith(- '@posthog/replay/session-retention-miss',+ '@posthog/replay/session-retention-1-miss','30d','EX',24 * 60 * 60)})+ it('scopes the cache key by team so the same session id across teams cannot collide', async () => {+ // Same session id under two teams: team 1 hits the cache (30d), team 2 misses (→ 1y).+ mockRedisClient.mget = jest.fn().mockResolvedValue(['30d', null])++ const results = await retentionService.resolveSessionRetentions(sessionSet([1, 's'], [2, 's']))++ // The shared session id is read under two distinct, team-scoped keys.+ expect(mockRedisClient.mget).toHaveBeenCalledWith([+ '@posthog/replay/session-retention-1-s',+ '@posthog/replay/session-retention-2-s',+ ])+ // Each team resolves to its own retention — no cross-team bleed from the cache.+ expect(results.get(1, 's')).toEqual({ resolved: true, retentionPeriod: '30d' })+ expect(results.get(2, 's')).toEqual({ resolved: true, retentionPeriod: '1y' })+ // Only team 2's miss is written back, under its own team-scoped key.+ expect(mockPipeline.set).toHaveBeenCalledTimes(1)+ expect(mockPipeline.set).toHaveBeenCalledWith(+ '@posthog/replay/session-retention-2-s',+ '1y',+ 'EX',+ 24 * 60 * 60+ )+ })+it('propagates a Redis read failure (no team-service fallback, so the retry wrapper re-runs)', async () => {// Redis holds each session's locked-in retention, so we must not resolve from the// (current) team value on a Redis failure — fail fast and let the caller retry/crash.diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/shared/retention/retention-service.ts b/nodejs/src/ingestion/pipelines/sessionreplay/shared/retention/retention-service.tsindex 6aee9fa8e513..a4d664bf8c52 100644--- a/nodejs/src/ingestion/pipelines/sessionreplay/shared/retention/retention-service.ts+++ b/nodejs/src/ingestion/pipelines/sessionreplay/shared/retention/retention-service.ts@@ -21,8 +21,10 @@ export class RetentionService {private keyPrefix = '@posthog/replay/') {}- private generateRedisKey(sessionId: string): string {- return `${this.keyPrefix}session-retention-${sessionId}`+ private generateRedisKey(teamId: TeamId, sessionId: string): string {+ // Retention is a per-team property, so the cache key must be scoped by team — a session id is+ // only unique within a team and can collide across teams.+ return `${this.keyPrefix}session-retention-${teamId}-${sessionId}`}/**@@ -44,7 +46,7 @@ export class RetentionService {const startTime = performance.now()const client = await this.redisPool.acquire()try {- const redisKeys = unique.map(({ sessionId }) => this.generateRedisKey(sessionId))+ const redisKeys = unique.map(({ teamId, sessionId }) => this.generateRedisKey(teamId, sessionId))const cached = await client.mget(redisKeys)const missIndexes: number[] = []