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): add fairness key (#70807)

Rodrigo Iloro01531ad5a1c4
diff --git a/products/experiments/backend/presentation/views.py b/products/experiments/backend/presentation/views.py
index c86630afac5e..1a24d6d754d6 100644
--- a/products/experiments/backend/presentation/views.py
+++ b/products/experiments/backend/presentation/views.py
@@ -1047,7 +1047,10 @@ class EnterpriseExperimentsViewSet(
asyncio.run(
temporal.start_workflow(
"experiment-metrics-recalculation-workflow",
- MetricsRecalcInputs(recalculation_id=recalculation_id),
+ MetricsRecalcInputs(
+ recalculation_id=recalculation_id,
+ fairness_key=str(experiment.team.organization_id),
+ ),
id=f"experiment-metrics-recalculation-{recalculation_id}",
task_queue=settings.EXPERIMENTS_RECALCULATION_TASK_QUEUE,
)
diff --git a/products/experiments/backend/temporal/models.py b/products/experiments/backend/temporal/models.py
index 6bf11b0c2983..f05cd9e48fe9 100644
--- a/products/experiments/backend/temporal/models.py
+++ b/products/experiments/backend/temporal/models.py
@@ -39,6 +39,9 @@ class ExperimentMetricsRecalculationWorkflowInputs:
"""Input to the batch metrics recalculation workflow."""
recalculation_id: str # UUID as string
+ # Task-queue fairness key for the calc activities (the org id, matching the app_per_org limiter scope).
+ # None falls back to default dispatch order.
+ fairness_key: str | None = None
@dataclasses.dataclass
diff --git a/products/experiments/backend/temporal/recalculation_workflow.py b/products/experiments/backend/temporal/recalculation_workflow.py
index 21ab942119b4..92cfdbdf7deb 100644
--- a/products/experiments/backend/temporal/recalculation_workflow.py
+++ b/products/experiments/backend/temporal/recalculation_workflow.py
@@ -2,7 +2,7 @@ import asyncio
from datetime import timedelta
import temporalio.workflow
-from temporalio.common import RetryPolicy
+from temporalio.common import Priority, RetryPolicy
from temporalio.exceptions import ApplicationError
from posthog.temporal.common.base import PostHogWorkflow
@@ -122,6 +122,9 @@ class ExperimentMetricsRecalculationWorkflow(PostHogWorkflow):
],
start_to_close_timeout=timedelta(seconds=METRIC_CALC_ACTIVITY_TIMEOUT_SECONDS),
retry_policy=calc_retry_policy,
+ # Round-robin dispatch across orgs under backlog; a no-op on namespaces without
+ # fairness support.
+ priority=Priority(fairness_key=inputs.fairness_key),
)
for metric in metrics
],