: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(ingestion): stop pinning error tracking overflow to one partition (#86877)
Eli Reisman2b1aca24bbea
diff --git a/.claude/agents/ingestion/pipeline-composition-doctor.md b/.claude/agents/ingestion/pipeline-composition-doctor.mdindex 18e377d83491..a5b58030cab8 100644--- a/.claude/agents/ingestion/pipeline-composition-doctor.md+++ b/.claude/agents/ingestion/pipeline-composition-doctor.md@@ -221,6 +221,30 @@ function createOffloadStep(): ProcessingStep<Input, Input> {}```+### 11. Overflow routing config states whether the pipeline writes persons++`createApplyEventRestrictionsStep` takes a `RoutingConfig` whose `pipelineWritesPersons` is+required. Answer it from the repository the pipeline holds, not from what looks safe:+`PersonRepository` writes, `PersonReadRepository` only reads, and a pipeline that takes neither+does not touch persons.++True forces a force-overflow redirect to keep its partition key, so the overflow consumer cannot+write one person row from two partitions at once. That protection costs load spreading, so a+read-only pipeline that answers true silently pins a force-overflowed team to one partition.++```typescript+// GOOD - analytics writes persons through a PersonRepository+createApplyEventRestrictionsStep(manager, { overflowMode, preservePartitionLocality, pipelineWritesPersons: true })++// GOOD - error tracking and AI hold a PersonReadRepository+createApplyEventRestrictionsStep(manager, { overflowMode, preservePartitionLocality, pipelineWritesPersons: false })+```++- Changing a pipeline's person repository means revisiting this field. Nothing checks that the+ answer still matches.+- `createApplyBasicEventRestrictionsStep` takes no `RoutingConfig`. Use it for a pipeline with no+ overflow topic, and the question does not arise.+## Output format### When reviewing code@@ -256,6 +280,10 @@ Produce a checklist grouped by rule:### Factory functions- [x] Pipeline created via factory function++### Overflow routing++- [x] pipelineWritesPersons matches the pipeline's person repository (Rule 11)```### When implementing codediff --git a/nodejs/src/ingestion/common/steps/event-preprocessing/apply-event-restrictions.test.ts b/nodejs/src/ingestion/common/steps/event-preprocessing/apply-event-restrictions.test.tsindex 1f2e426642af..aa441c3f8ca9 100644--- a/nodejs/src/ingestion/common/steps/event-preprocessing/apply-event-restrictions.test.ts+++ b/nodejs/src/ingestion/common/steps/event-preprocessing/apply-event-restrictions.test.ts@@ -20,9 +20,12 @@ describe('createApplyEventRestrictionsStep', () => {getAppliedRestrictions: jest.fn().mockReturnValue(new Set()),} as unknown as EventIngestionRestrictionManager+ // The shared config mirrors a person-writing pipeline like analytics.+ // The locality table overrides both fields per case.routingConfig = {preservePartitionLocality: true,overflowMode: 'redirect',+ pipelineWritesPersons: true,}step = createApplyEventRestrictionsStep(eventIngestionRestrictionManager, routingConfig)@@ -189,123 +192,49 @@ describe('createApplyEventRestrictionsStep', () => {)})- it('partition locality: config=true, skipPerson=false -> preserves locality', async () => {- const input = {- message: {} as any,- headers: createTestEventHeaders({- token: 'test-token',- distinct_id: 'test-user',- }),- }-- jest.mocked(eventIngestionRestrictionManager.getAppliedRestrictions).mockReturnValue(- new Set([Restriction.FORCE_OVERFLOW])- )-- const result = await step(input)-- expect(result).toEqual(- redirect(- 'Event redirected to overflow due to force overflow restrictions',- OVERFLOW_OUTPUT,- true,- false- )- )- })-- it('partition locality: config=false, skipPerson=false -> preserves locality', async () => {- const configWithNoLocality: RoutingConfig = {- ...routingConfig,- preservePartitionLocality: false,- }- const stepWithNoLocality = createApplyEventRestrictionsStep(- eventIngestionRestrictionManager,- configWithNoLocality- )-- const input = {- message: {} as any,- headers: createTestEventHeaders({- token: 'test-token',- distinct_id: 'test-user',- }),- }-- jest.mocked(eventIngestionRestrictionManager.getAppliedRestrictions).mockReturnValue(- new Set([Restriction.FORCE_OVERFLOW])- )-- const result = await stepWithNoLocality(input)+ // The force-overflow redirect keeps the partition key only when person+ // processing is on AND the pipeline writes persons, because that is the+ // only case where a spread key could put two writers on one person row.+ // Every other case follows preservePartitionLocality.+ it.each([+ { config: true, skipPerson: false, writesPersons: true, expected: true },+ { config: false, skipPerson: false, writesPersons: true, expected: true },+ { config: true, skipPerson: true, writesPersons: true, expected: true },+ { config: false, skipPerson: true, writesPersons: true, expected: false },+ { config: true, skipPerson: false, writesPersons: false, expected: true },+ { config: false, skipPerson: false, writesPersons: false, expected: false },+ ])(+ 'partition locality: config=$config, skipPerson=$skipPerson, writesPersons=$writesPersons -> preserveKey=$expected',+ async ({ config, skipPerson, writesPersons, expected }) => {+ const localityStep = createApplyEventRestrictionsStep(eventIngestionRestrictionManager, {+ ...routingConfig,+ preservePartitionLocality: config,+ pipelineWritesPersons: writesPersons,+ })- expect(result).toEqual(- redirect(- 'Event redirected to overflow due to force overflow restrictions',- OVERFLOW_OUTPUT,- true, // Always true when not skipping person, ignores config- false+ jest.mocked(eventIngestionRestrictionManager.getAppliedRestrictions).mockReturnValue(+ skipPerson+ ? new Set([Restriction.FORCE_OVERFLOW, Restriction.SKIP_PERSON_PROCESSING])+ : new Set([Restriction.FORCE_OVERFLOW]))- )- })- it('partition locality: config=true, skipPerson=true -> preserves locality', async () => {- const input = {- message: {} as any,- headers: createTestEventHeaders({- token: 'test-token',- distinct_id: 'test-user',- }),- }+ const input = {+ message: {} as any,+ headers: createTestEventHeaders({ token: 'test-token', distinct_id: 'test-user' }),+ }- jest.mocked(eventIngestionRestrictionManager.getAppliedRestrictions).mockReturnValue(- new Set([Restriction.FORCE_OVERFLOW, Restriction.SKIP_PERSON_PROCESSING])- )+ const result = await localityStep(input)- const result = await step(input)-- expect(result).toEqual(- redirect(- 'Event redirected to overflow due to force overflow restrictions',- OVERFLOW_OUTPUT,- true, // Uses config value- false+ expect(result).toEqual(+ redirect(+ 'Event redirected to overflow due to force overflow restrictions',+ OVERFLOW_OUTPUT,+ expected,+ false+ ))- )- })-- it('partition locality: config=false, skipPerson=true -> does not preserve locality', async () => {- const configWithNoLocality: RoutingConfig = {- ...routingConfig,- preservePartitionLocality: false,}- const stepWithNoLocality = createApplyEventRestrictionsStep(- eventIngestionRestrictionManager,- configWithNoLocality- )-- const input = {- message: {} as any,- headers: createTestEventHeaders({- token: 'test-token',- distinct_id: 'test-user',- }),- }-- jest.mocked(eventIngestionRestrictionManager.getAppliedRestrictions).mockReturnValue(- new Set([Restriction.FORCE_OVERFLOW, Restriction.SKIP_PERSON_PROCESSING])- )-- const result = await stepWithNoLocality(input)-- expect(result).toEqual(- redirect(- 'Event redirected to overflow due to force overflow restrictions',- OVERFLOW_OUTPUT,- false, // Uses config value- false- )- )- })+ )it('returns success when overflow is disabled', async () => {const disabledConfig: RoutingConfig = {diff --git a/nodejs/src/ingestion/common/steps/event-preprocessing/apply-event-restrictions.ts b/nodejs/src/ingestion/common/steps/event-preprocessing/apply-event-restrictions.tsindex 423bf1976073..7ae1f2977da6 100644--- a/nodejs/src/ingestion/common/steps/event-preprocessing/apply-event-restrictions.ts+++ b/nodejs/src/ingestion/common/steps/event-preprocessing/apply-event-restrictions.ts@@ -15,6 +15,22 @@ export const ingestionOverflowingMessagesTotal = new Counter({export type RoutingConfig = {overflowMode: IngestionOverflowModepreservePartitionLocality: boolean+ /**+ * Whether this pipeline writes persons (creates or updates rows), as+ * opposed to only reading them or not touching persons at all.+ *+ * True keeps the partition key on a force-overflow redirect while person+ * processing is on, because the overflow consumer would otherwise write+ * the same person row from several partitions at once. False lets those+ * redirects follow `preservePartitionLocality`, so the overflow lane can+ * spread the load.+ *+ * Required, so that a new pipeline has to answer it rather than inherit+ * a default. The answer is the repository the pipeline holds: a+ * `PersonRepository` writes, a `PersonReadRepository` does not, and a+ * pipeline that takes neither does not touch persons at all.+ */+ pipelineWritesPersons: boolean}/**@@ -70,7 +86,10 @@ export function createApplyEventRestrictionsStep<T extends { headers: EventHeadeif (routingConfig.overflowMode === 'redirect' && restrictions.has(RestrictionType.FORCE_OVERFLOW)) {ingestionOverflowingMessagesTotal.inc()const shouldProcessPerson = !restrictions.has(RestrictionType.SKIP_PERSON_PROCESSING)- const preservePartitionLocality = shouldProcessPerson ? true : routingConfig.preservePartitionLocality+ const preservePartitionLocality =+ shouldProcessPerson && routingConfig.pipelineWritesPersons+ ? true+ : routingConfig.preservePartitionLocalityreturn redirect('Event redirected to overflow due to force overflow restrictions',OVERFLOW_OUTPUT,diff --git a/nodejs/src/ingestion/pipelines/ai/pipeline.ts b/nodejs/src/ingestion/pipelines/ai/pipeline.tsindex 797676eeb520..68145245e781 100644--- a/nodejs/src/ingestion/pipelines/ai/pipeline.ts+++ b/nodejs/src/ingestion/pipelines/ai/pipeline.ts@@ -149,6 +149,8 @@ export function createAiIngestionPipeline<createApplyEventRestrictionsStep(eventIngestionRestrictionManager, {overflowMode,preservePartitionLocality,+ // createFetchPersonChunkStep below only reads persons.+ pipelineWritesPersons: false,}))// Rate-limit non-cookieless events to overflow before parsing the body.diff --git a/nodejs/src/ingestion/pipelines/analytics/joined-ingestion-pipeline.ts b/nodejs/src/ingestion/pipelines/analytics/joined-ingestion-pipeline.tsindex d501729c0b48..5e5cbc30ea3b 100644--- a/nodejs/src/ingestion/pipelines/analytics/joined-ingestion-pipeline.ts+++ b/nodejs/src/ingestion/pipelines/analytics/joined-ingestion-pipeline.ts@@ -190,6 +190,7 @@ export function createJoinedIngestionPipeline<createApplyEventRestrictionsStep(eventIngestionRestrictionManager, {overflowMode,preservePartitionLocality,+ pipelineWritesPersons: true,}))// Rate-limit non-cookieless events to overflow before parsing the body.diff --git a/nodejs/src/ingestion/pipelines/errortracking/error-tracking-consumer.ts b/nodejs/src/ingestion/pipelines/errortracking/error-tracking-consumer.tsindex c9a694d84da4..6a9774286bf6 100644--- a/nodejs/src/ingestion/pipelines/errortracking/error-tracking-consumer.ts+++ b/nodejs/src/ingestion/pipelines/errortracking/error-tracking-consumer.ts@@ -51,9 +51,10 @@ export interface ErrorTrackingConsumerOptions {statefulOverflowRedisTTLSeconds: numberstatefulOverflowLocalCacheTTLSeconds: number/**- * When true, overflow redirects keep the original partition key. When- * false (default), the overflow producer emits with a null key. Applies- * to both restriction-driven force-overflow and rate-limit-to-overflow.+ * When true (the default), overflow redirects keep the original partition+ * key. When false, the overflow producer emits with a null key so Kafka+ * spreads the load across the overflow topic's partitions. Applies to both+ * restriction-driven force-overflow and rate-limit-to-overflow.*/preservePartitionLocality: booleanpipeline: stringdiff --git a/nodejs/src/ingestion/pipelines/errortracking/error-tracking-pipeline.ts b/nodejs/src/ingestion/pipelines/errortracking/error-tracking-pipeline.tsindex 0183330b9e37..10c7fcdf3c83 100644--- a/nodejs/src/ingestion/pipelines/errortracking/error-tracking-pipeline.ts+++ b/nodejs/src/ingestion/pipelines/errortracking/error-tracking-pipeline.ts@@ -152,6 +152,8 @@ export function createErrorTrackingPipeline(config: ErrorTrackingPipelineConfig)createApplyEventRestrictionsStep(eventIngestionRestrictionManager, {overflowMode,preservePartitionLocality,+ // createFetchPersonChunkStep below only reads persons.+ pipelineWritesPersons: false,}))// Rate-limit non-cookieless events to overflow before parsing the body.diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/ml-mirror-pipeline.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/ml-mirror-pipeline.tsindex d24d9bd0a064..dc4022027c7e 100644--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/ml-mirror-pipeline.ts+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/ml-mirror-pipeline.ts@@ -110,6 +110,8 @@ export function createMlMirrorReplayPipeline(createApplyEventRestrictionsStep(eventIngestionRestrictionManager, {overflowMode,preservePartitionLocality: true,+ // Mirrors replay, which never reads or writes persons.+ pipelineWritesPersons: false,})).pipe(createValidateSessionReplayHeadersStep())diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/session-replay-pipeline.ts b/nodejs/src/ingestion/pipelines/sessionreplay/session-replay-pipeline.tsindex 5aa642d95470..55b08ba19da3 100644--- a/nodejs/src/ingestion/pipelines/sessionreplay/session-replay-pipeline.ts+++ b/nodejs/src/ingestion/pipelines/sessionreplay/session-replay-pipeline.ts@@ -138,6 +138,9 @@ export function createSessionReplayPipeline(config: SessionReplayPipelineConfig)createApplyEventRestrictionsStep(eventIngestionRestrictionManager, {overflowMode,preservePartitionLocality: true, // Sessions must stay on the same partition+ // Replay never reads or writes persons. The line above pins+ // locality either way, so this only records the fact.+ pipelineWritesPersons: false,}))// Validate the headers capture guarantees (DLQ if missing) and narrow the type