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.

fix(experiments): updates the workflow retry settings to better handle "query too busy" errors (#65691)

Rodrigo Ilorod96afd4db8e7
diff --git a/products/experiments/backend/temporal/ARCHITECTURE.md b/products/experiments/backend/temporal/ARCHITECTURE.md
index d34e37e2b58b..f1f645900d8f 100644
--- a/products/experiments/backend/temporal/ARCHITECTURE.md
+++ b/products/experiments/backend/temporal/ARCHITECTURE.md
@@ -118,8 +118,8 @@ The split is intentional: Grafana tells you about the worker process, PostHog te
## Performance notes
-- **Concurrency cap of 10.** The workflow fans out per-metric calc activities under `asyncio.Semaphore(MAX_CONCURRENT_METRICS=10)`. Each activity runs one blocking ClickHouse query; without the cap, a 50-metric experiment would saturate the worker's connection pool and starve other work on the queue.
-- **5-minute per-attempt budget.** `start_to_close_timeout=timedelta(minutes=5)` on the calc activity, with `RetryPolicy(maximum_attempts=2)`. We deliberately don't set a heartbeat timeout — the activity has no progress hooks inside the ClickHouse query, so the heartbeat couldn't fire mid-query anyway; `start_to_close_timeout` is the real per-attempt ceiling.
+- **Concurrency cap of 4.** The workflow fans out per-metric calc activities under `asyncio.Semaphore(MAX_CONCURRENT_METRICS=4)`. Each activity runs one blocking ClickHouse query; without the cap, a 50-metric experiment would saturate the worker's connection pool and starve other work on the queue. The cap is 4 (not higher) because these offline queries share the org's `app:query:per-org` ClickHouse budget (default 20, halved/quartered under cluster load) with the org's live queries; staying at 4 keeps a single run under the budget even when it drops to 5, avoiding `ClickHouseAtCapacity` throttling.
+- **5-minute per-attempt budget.** `start_to_close_timeout=timedelta(minutes=5)` on the calc activity, with `RetryPolicy(maximum_attempts=5, initial_interval=5s, backoff_coefficient=2.0, maximum_interval=60s)`. The exponential backoff (5s → 10s → 20s → 40s, jittered by Temporal) absorbs transient `ClickHouseAtCapacity`/`ConcurrencyLimitExceeded` throttling so a metric that hits backpressure retries in a later, quieter load window instead of failing the run. We deliberately don't set a heartbeat timeout — the activity has no progress hooks inside the ClickHouse query, so the heartbeat couldn't fire mid-query anyway; `start_to_close_timeout` is the real per-attempt ceiling.
- **No saved-metric snapshot.** Each calc activity that processes a saved/shared metric re-queries the M2M through-model to resolve the metric definition. For a 5–10 metric experiment this is fine; for larger experiments the per-call query starts to add up. Tracked as a deferred optimization (snapshot resolved metric dicts on the recalc row at discovery time).
## What this doc doesn't cover
diff --git a/products/experiments/backend/temporal/recalculation_workflow.py b/products/experiments/backend/temporal/recalculation_workflow.py
index f02036691971..d09f7f3ec7d0 100644
--- a/products/experiments/backend/temporal/recalculation_workflow.py
+++ b/products/experiments/backend/temporal/recalculation_workflow.py
@@ -19,7 +19,10 @@ with temporalio.workflow.unsafe.imports_passed_through():
)
from products.experiments.backend.temporal.recalculation_metrics import increment_workflow_finished
-MAX_CONCURRENT_METRICS = 10
+# Offline recalc shares the org's ClickHouse query budget (app:query:per-org, default 20,
+# halved/quartered under cluster load) with the org's live queries. Cap at 4 so a single run
+# stays under the budget even when it drops to 5, avoiding ClickHouseAtCapacity throttling.
+MAX_CONCURRENT_METRICS = 4
@temporalio.workflow.defn(name="experiment-metrics-recalculation-workflow")
@@ -111,10 +114,16 @@ class ExperimentMetricsRecalculationWorkflow(PostHogWorkflow):
# No heartbeat: the activity's only long-running step is one blocking ClickHouse query
# with no progress hooks, so start_to_close_timeout is the real per-attempt ceiling.
start_to_close_timeout=timedelta(minutes=5),
+ # Throttle errors (ClickHouseAtCapacity / ConcurrencyLimitExceeded) are transient
+ # backpressure: spread retries with exponential backoff so each attempt lands in a
+ # different load window. Temporal jitters each interval automatically, so concurrent
+ # metrics don't re-stampede in lockstep. Permanent failures don't reach here
+ # (StatisticError/ZeroDivisionError are recorded and returned, never raised).
retry_policy=RetryPolicy(
- maximum_attempts=2,
+ maximum_attempts=5,
initial_interval=timedelta(seconds=5),
- maximum_interval=timedelta(seconds=30),
+ backoff_coefficient=2.0,
+ maximum_interval=timedelta(seconds=60),
),
)