: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
feat(flags): lower the flag_evaluations last_called_at sync buffer to 360s (#121651)
Phil Haack4f9b333949c8
diff --git a/posthog/settings/feature_flags.py b/posthog/settings/feature_flags.pyindex 035e3c10c5dd..d9ec43477523 100644--- a/posthog/settings/feature_flags.py+++ b/posthog/settings/feature_flags.py@@ -81,12 +81,16 @@ FEATURE_FLAG_LAST_CALLED_AT_SYNC_REPLICATION_BUFFER_SECONDS: int = max(0,get_from_env("FEATURE_FLAG_LAST_CALLED_AT_SYNC_REPLICATION_BUFFER_SECONDS", 60, type_cast=int),)-# Replaces the buffer above when the source is flag_evaluations. That table stamps inserted_at with the Kafka-# message time, before ClickHouse writes the row, so this buffer must also cover the ClickHouse consumer lag.-# A row that ClickHouse writes later than this falls behind the checkpoint and is never read.+# Replaces the buffer above when the source is flag_evaluations. Its MV stamps inserted_at on the ingestion node,+# before the row reaches a shard and before the other replicas of that shard fetch it. The query reads each shard+# from a random replica. ClickHouse skips a replica that lags more than max_replica_delay_for_distributed_queries+# (300 seconds by default). The sync query turns off the stale-replica fallback, so the read fails when every+# replica of a shard lags past that limit. A replica that lags up to 300 seconds can still answer, so this buffer+# must stay above that limit. A row that becomes visible later than this falls behind the checkpoint and is never+# read.FEATURE_FLAG_LAST_CALLED_AT_SYNC_FLAG_EVALUATIONS_BUFFER_SECONDS: int = max(0,- get_from_env("FEATURE_FLAG_LAST_CALLED_AT_SYNC_FLAG_EVALUATIONS_BUFFER_SECONDS", 900, type_cast=int),+ get_from_env("FEATURE_FLAG_LAST_CALLED_AT_SYNC_FLAG_EVALUATIONS_BUFFER_SECONDS", 360, type_cast=int),)# Per-chunk ClickHouse execution cap. sync_execute sets no max_execution_time of its own, so# without this a hung query is bounded only by the server profile, and a run can outlive thediff --git a/posthog/tasks/tasks.py b/posthog/tasks/tasks.pyindex 3344cc714095..c989d5aae2d1 100644--- a/posthog/tasks/tasks.py+++ b/posthog/tasks/tasks.py@@ -1487,7 +1487,14 @@ def sync_feature_flag_last_called(self: PushGatewayTask) -> None:"current_sync_timestamp": chunk_end_ts,"limit": settings.FEATURE_FLAG_LAST_CALLED_AT_SYNC_CLICKHOUSE_LIMIT,},- settings={"max_execution_time": settings.FEATURE_FLAG_LAST_CALLED_AT_SYNC_QUERY_TIMEOUT_SECONDS},+ settings={+ "max_execution_time": settings.FEATURE_FLAG_LAST_CALLED_AT_SYNC_QUERY_TIMEOUT_SECONDS,+ # When every replica of a shard lags past max_replica_delay_for_distributed_queries,+ # the default fallback reads a stale replica and returns a result with rows missing.+ # The checkpoint would then move past those rows. With the fallback off, ClickHouse+ # fails the chunk, so the checkpoint stops before it.+ "fallback_to_stale_replicas_for_distributed_queries": 0,+ },)except Exception as e:# Transient errors are what autoretry_for is for. ClickHouseAtCapacity indiff --git a/products/experiments/backend/health/checks/new_flag_no_traffic.py b/products/experiments/backend/health/checks/new_flag_no_traffic.pyindex cb84a80441bb..b565c2a4d4da 100644--- a/products/experiments/backend/health/checks/new_flag_no_traffic.py+++ b/products/experiments/backend/health/checks/new_flag_no_traffic.py@@ -23,7 +23,7 @@ from products.experiments.backend.health.skips import ()# `last_called_at` moves only when `sync_feature_flag_last_called` runs, every 30 minutes (`posthog/tasks/scheduled.py`).-# A run reads the calls up to a buffer before its start (`FEATURE_FLAG_LAST_CALLED_AT_SYNC_*_BUFFER_SECONDS`, 1 or 15+# A run reads the calls up to a buffer before its start (`FEATURE_FLAG_LAST_CALLED_AT_SYNC_*_BUFFER_SECONDS`, 1 or 6# minutes by default), so a call waits up to one interval plus that buffer. Update this when the schedule changes.LAST_CALLED_AT_LAG = "an hour"diff --git a/products/feature_flags/backend/test/test_feature_flag_sync.py b/products/feature_flags/backend/test/test_feature_flag_sync.pyindex 79120a507c66..028009eeeea2 100644--- a/products/feature_flags/backend/test/test_feature_flag_sync.py+++ b/products/feature_flags/backend/test/test_feature_flag_sync.py@@ -113,6 +113,22 @@ class TestSyncFeatureFlagLastCalled(BaseTest):params = call_args[0][1]assert params["last_sync_timestamp"] == checkpoint_time+ @time_machine.travel("2024-06-15 12:00:00", tick=False)+ @patch("posthog.clickhouse.client.sync_execute")+ @patch("posthog.tasks.tasks.get_client")+ def test_chunk_queries_refuse_stale_replicas(+ self, mock_get_client: MagicMock, mock_sync_execute: MagicMock+ ) -> None:+ mock_get_client.return_value = mock_redis_client()+ mock_sync_execute.return_value = []++ with self.settings(FEATURE_FLAG_LAST_CALLED_AT_SYNC_SOURCE="flag_evaluations"):+ sync_feature_flag_last_called()++ assert mock_sync_execute.call_args_list+ for call in mock_sync_execute.call_args_list:+ assert call.kwargs["settings"]["fallback_to_stale_replicas_for_distributed_queries"] == 0+@time_machine.travel("2024-06-15 12:00:00", tick=False)@patch("posthog.clickhouse.client.sync_execute")@patch("posthog.tasks.tasks.get_client")