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.

perf(messaging): shrink reconcile overrides lookback to 6h (#72919)

Gustavo H. Strassburgerbb04e926d800
diff --git a/posthog/settings/schedules.py b/posthog/settings/schedules.py
index 53ce6cb86ae1..e60cb12fa1ed 100644
--- a/posthog/settings/schedules.py
+++ b/posthog/settings/schedules.py
@@ -108,8 +108,9 @@ REALTIME_COHORT_CALCULATION_P99_P100_INTERVAL_MINUTES: int = get_from_env(
# How often precalculated_events is reconciled against person_distinct_id_overrides to
# repair rows made stale by person merges. Runs at the realtime calculation cadence so a
-# merge is reconciled by the next calculation run; each run is incremental over overrides
-# from the last RECONCILE_PRECALCULATED_DATA_OVERRIDES_LOOKBACK_HOURS (48 by default), which must stay
+# merge is reconciled by the next calculation run. Each run re-diffs every override from the
+# last RECONCILE_PRECALCULATED_DATA_OVERRIDES_LOOKBACK_HOURS (6 by default) — there is no
+# watermark, so a wider window just reprocesses more already-handled merges. The window must stay
# well above this interval and well below the overrides squash cadence
# (SQUASH_PERSON_OVERRIDES_SCHEDULE, weekly by default) — the squash deletes override rows
# after folding them into events, and reconciliation needs to see them before then.
diff --git a/posthog/temporal/messaging/reconcile_precalculated_data_workflow.py b/posthog/temporal/messaging/reconcile_precalculated_data_workflow.py
index 5d98ff548dca..6d7c6a7559ca 100644
--- a/posthog/temporal/messaging/reconcile_precalculated_data_workflow.py
+++ b/posthog/temporal/messaging/reconcile_precalculated_data_workflow.py
@@ -55,6 +55,15 @@ def _positive_int_env(name: str, default: int, logger: structlog.BoundLogger) ->
return value
+# Default overrides lookback window, env-overridable via
+# RECONCILE_PRECALCULATED_DATA_OVERRIDES_LOOKBACK_HOURS. 6h is a conservative interim value sized
+# for steady-state run duration, not a hard bound: nothing caps how long a run takes, so a run
+# longer than the window can still strand a merge until the next full_scan (the planned per-run
+# watermark is the real guarantee). Lower it once per-run duration is reliably small. See the
+# block comment below for how the window relates to the schedule and squash cadence.
+DEFAULT_OVERRIDES_LOOKBACK_HOURS = 6
+
+
# precalculated_events stores the person_id that was resolved for a distinct_id when the row
# was written. When a later identify/merge re-points the distinct_id to another person, the
# row silently goes stale: cohort_membership keeps a person the distinct_id no longer belongs
@@ -69,10 +78,11 @@ def _positive_int_env(name: str, default: int, logger: structlog.BoundLogger) ->
#
# The override row written at merge time is the invalidation signal: each scheduled run
# picks up distinct_ids whose override landed within the lookback window
-# (RECONCILE_PRECALCULATED_DATA_OVERRIDES_LOOKBACK_HOURS) and repairs just their rows, so the schedule
-# can run at the realtime calculation cadence and a merge is reconciled by the next
-# calculation run instead of hours later. A `full_scan` input ignores the window — use it
-# for first-deploy remediation or after the workflow was down longer than the lookback.
+# (RECONCILE_PRECALCULATED_DATA_OVERRIDES_LOOKBACK_HOURS, default DEFAULT_OVERRIDES_LOOKBACK_HOURS)
+# and repairs just their rows, so the schedule can run at the realtime calculation cadence and a
+# merge is reconciled by the next calculation run instead of hours later. A `full_scan` input
+# ignores the window — use it for first-deploy remediation or after the workflow was down longer
+# than the lookback.
#
# Timing constraints: the lookback must comfortably exceed the schedule interval (so no
# merge falls between runs), and both must stay well inside the person-overrides squash
@@ -259,7 +269,9 @@ class ReconciliationRunConfig:
async def get_reconciliation_run_config_activity() -> ReconciliationRunConfig:
"""Compute the run-wide lookback boundary and concurrency once, before any team runs."""
logger = LOGGER.bind()
- overrides_lookback_hours = _positive_int_env("RECONCILE_PRECALCULATED_DATA_OVERRIDES_LOOKBACK_HOURS", 48, logger)
+ overrides_lookback_hours = _positive_int_env(
+ "RECONCILE_PRECALCULATED_DATA_OVERRIDES_LOOKBACK_HOURS", DEFAULT_OVERRIDES_LOOKBACK_HOURS, logger
+ )
team_concurrency = _positive_int_env("RECONCILE_PRECALCULATED_DATA_TEAM_CONCURRENCY", 5, logger)
return ReconciliationRunConfig(
since=dt.datetime.now(dt.UTC) - dt.timedelta(hours=overrides_lookback_hours),
@@ -331,7 +343,9 @@ async def reconcile_team_precalculated_person_properties_activity(
distinct_id_batch_size = _positive_int_env("RECONCILE_PRECALCULATED_DATA_DISTINCT_ID_BATCH_SIZE", 1000, logger)
kafka_flush_batch_size = _positive_int_env("RECONCILE_PRECALCULATED_DATA_KAFKA_FLUSH_BATCH_SIZE", 1000, logger)
- overrides_lookback_hours = _positive_int_env("RECONCILE_PRECALCULATED_DATA_OVERRIDES_LOOKBACK_HOURS", 48, logger)
+ overrides_lookback_hours = _positive_int_env(
+ "RECONCILE_PRECALCULATED_DATA_OVERRIDES_LOOKBACK_HOURS", DEFAULT_OVERRIDES_LOOKBACK_HOURS, logger
+ )
if inputs.full_scan:
overrides_query = OVERRIDES_QUERY
@@ -501,7 +515,9 @@ async def reconcile_team_precalculated_events_activity(inputs: ReconcileTeamInpu
distinct_id_batch_size = _positive_int_env("RECONCILE_PRECALCULATED_DATA_DISTINCT_ID_BATCH_SIZE", 1000, logger)
kafka_flush_batch_size = _positive_int_env("RECONCILE_PRECALCULATED_DATA_KAFKA_FLUSH_BATCH_SIZE", 1000, logger)
- overrides_lookback_hours = _positive_int_env("RECONCILE_PRECALCULATED_DATA_OVERRIDES_LOOKBACK_HOURS", 48, logger)
+ overrides_lookback_hours = _positive_int_env(
+ "RECONCILE_PRECALCULATED_DATA_OVERRIDES_LOOKBACK_HOURS", DEFAULT_OVERRIDES_LOOKBACK_HOURS, logger
+ )
if inputs.full_scan:
overrides_query = OVERRIDES_QUERY
diff --git a/posthog/temporal/messaging/test/test_reconcile_precalculated_data_workflow.py b/posthog/temporal/messaging/test/test_reconcile_precalculated_data_workflow.py
index b63fa3180694..64e13bfc0ae1 100644
--- a/posthog/temporal/messaging/test/test_reconcile_precalculated_data_workflow.py
+++ b/posthog/temporal/messaging/test/test_reconcile_precalculated_data_workflow.py
@@ -13,6 +13,7 @@ from temporalio.worker import UnsandboxedWorkflowRunner, Worker
from posthog.clickhouse.client import sync_execute
from posthog.models.team import Team
from posthog.temporal.messaging.reconcile_precalculated_data_workflow import (
+ DEFAULT_OVERRIDES_LOOKBACK_HOURS,
ReconcilePersonPropertiesResult,
ReconcilePrecalculatedDataWorkflow,
ReconcilePrecalculatedDataWorkflowInputs,
@@ -72,7 +73,11 @@ class TestGetReconciliationRunConfigActivity:
after = dt.datetime.now(dt.UTC)
assert result.team_concurrency == 5
- assert before - dt.timedelta(hours=48) <= result.since <= after - dt.timedelta(hours=48)
+ assert (
+ before - dt.timedelta(hours=DEFAULT_OVERRIDES_LOOKBACK_HOURS)
+ <= result.since
+ <= after - dt.timedelta(hours=DEFAULT_OVERRIDES_LOOKBACK_HOURS)
+ )
def _insert_precalculated_event(