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(growth): shorten SDK Doctor team-events cache TTL to reduce out-of-date alerting (#57556)

Steven Shultsa2e95fcec455
diff --git a/products/growth/backend/constants.py b/products/growth/backend/constants.py
index d12773ef10c4..03035e9585ca 100644
--- a/products/growth/backend/constants.py
+++ b/products/growth/backend/constants.py
@@ -1,6 +1,16 @@
from typing import Literal, TypedDict
-SDK_CACHE_EXPIRY = 60 * 60 * 24 * 7 # 7 days
+SDK_CACHE_EXPIRY = 60 * 60 * 24 * 7 # 7 days — used by the GitHub `latestVersion` cache (hourly Dagster job)
+
+# Team-events snapshot TTL — sized to self-heal one missed daily cron run.
+# The Temporal `sdk_outdated` cron starts at 08:00 UTC; per-team writes happen mid-batch, so the
+# previous day's write for any given team can be timestamped meaningfully later. A 26h TTL gives
+# ~2h headroom relative to cron *start*, and less for teams late in the batch queue. That's
+# deliberate: when a team's TTL expires before its next batch slot, the next user request hits
+# the cache-miss fallback in posthog/api/sdk_doctor.py:get_team_data, which runs one fresh
+# single-team HogQL query and repopulates Redis. Steady-state load is unchanged; the trade-off
+# prevents days-long stale snapshots when a team's batch is skipped or times out.
+TEAM_SDK_CACHE_EXPIRY = 60 * 60 * 26 # 26 hours
# Canonical list of the SDK identifiers SDK Doctor tracks. Lives here (rather than in
diff --git a/products/growth/backend/team_sdk_versions.py b/products/growth/backend/team_sdk_versions.py
index 1f620f63e15f..bde4bad499fb 100644
--- a/products/growth/backend/team_sdk_versions.py
+++ b/products/growth/backend/team_sdk_versions.py
@@ -15,7 +15,7 @@ from posthog.clickhouse.query_tagging import Feature, Product, tags_context
from posthog.exceptions_capture import capture_exception
from posthog.models import Team
-from products.growth.backend.constants import SDK_CACHE_EXPIRY, SdkVersionEntry, team_sdk_versions_key
+from products.growth.backend.constants import TEAM_SDK_CACHE_EXPIRY, SdkVersionEntry, team_sdk_versions_key
from products.growth.dags.github_sdk_versions import SDK_TYPES
default_logger: BoundLogger = structlog.get_logger(__name__)
@@ -103,7 +103,7 @@ def get_and_cache_team_sdk_versions(
if sdk_versions is not None:
payload = json.dumps(sdk_versions)
cache_key = team_sdk_versions_key(team_id)
- redis_client.setex(cache_key, SDK_CACHE_EXPIRY, payload)
+ redis_client.setex(cache_key, TEAM_SDK_CACHE_EXPIRY, payload)
logger.info(f"[SDK Doctor] Team {team_id} SDK versions cached successfully")
return sdk_versions
diff --git a/products/growth/backend/temporal/health_checks/sdk_outdated.py b/products/growth/backend/temporal/health_checks/sdk_outdated.py
index 5feadf6a58bc..0c1186e34e26 100644
--- a/products/growth/backend/temporal/health_checks/sdk_outdated.py
+++ b/products/growth/backend/temporal/health_checks/sdk_outdated.py
@@ -12,8 +12,8 @@ from posthog.temporal.health_checks.models import HealthCheckResult
from posthog.temporal.health_checks.query import execute_clickhouse_health_team_query
from products.growth.backend.constants import (
- SDK_CACHE_EXPIRY,
SDK_TYPES,
+ TEAM_SDK_CACHE_EXPIRY,
SdkVersionEntry,
github_sdk_versions_key,
team_sdk_versions_key,
@@ -74,7 +74,7 @@ def _cache_team_sdk_data(team_sdk_data: dict[int, dict[str, list[SdkVersionEntry
pipe = redis_client.pipeline()
for team_id, sdk_data in team_sdk_data.items():
cache_key = team_sdk_versions_key(team_id)
- pipe.setex(cache_key, SDK_CACHE_EXPIRY, json.dumps(sdk_data))
+ pipe.setex(cache_key, TEAM_SDK_CACHE_EXPIRY, json.dumps(sdk_data))
pipe.execute()
diff --git a/products/growth/backend/temporal/health_checks/tests/test_sdk_outdated.py b/products/growth/backend/temporal/health_checks/tests/test_sdk_outdated.py
index 61b41f27cc03..c4df1d23c272 100644
--- a/products/growth/backend/temporal/health_checks/tests/test_sdk_outdated.py
+++ b/products/growth/backend/temporal/health_checks/tests/test_sdk_outdated.py
@@ -6,8 +6,8 @@ from django.test import TestCase
from posthog.models.health_issue import HealthIssue
-from products.growth.backend.constants import SDK_TYPES
-from products.growth.backend.temporal.health_checks.sdk_outdated import SdkOutdatedCheck
+from products.growth.backend.constants import SDK_TYPES, TEAM_SDK_CACHE_EXPIRY
+from products.growth.backend.temporal.health_checks.sdk_outdated import SdkOutdatedCheck, _cache_team_sdk_data
def _make_github_data(latest_version: str, release_dates: dict | None = None) -> dict:
@@ -176,3 +176,16 @@ class TestSdkOutdatedCheck(TestCase):
results = self.check.detect([1])
assert results == {}
+
+ @patch("products.growth.backend.temporal.health_checks.sdk_outdated.get_client")
+ def test_cache_team_sdk_data_uses_team_sdk_cache_expiry(self, mock_get_client: MagicMock):
+ mock_redis = MagicMock()
+ mock_pipe = MagicMock()
+ mock_redis.pipeline.return_value = mock_pipe
+ mock_get_client.return_value = mock_redis
+
+ _cache_team_sdk_data({1: {"web": [{"lib_version": "1.0.0", "max_timestamp": "x", "count": 1}]}})
+
+ mock_pipe.setex.assert_called_once()
+ _key, ttl, _payload = mock_pipe.setex.call_args[0]
+ assert ttl == TEAM_SDK_CACHE_EXPIRY
diff --git a/products/growth/backend/tests/test_team_sdk_versions.py b/products/growth/backend/tests/test_team_sdk_versions.py
new file mode 100644
index 000000000000..03b659634fd3
--- /dev/null
+++ b/products/growth/backend/tests/test_team_sdk_versions.py
@@ -0,0 +1,19 @@
+from unittest.mock import MagicMock, patch
+
+from django.test import SimpleTestCase
+
+from products.growth.backend.constants import TEAM_SDK_CACHE_EXPIRY
+from products.growth.backend.team_sdk_versions import get_and_cache_team_sdk_versions
+
+
+class TestGetAndCacheTeamSdkVersions(SimpleTestCase):
+ @patch("products.growth.backend.team_sdk_versions.get_sdk_versions_for_team")
+ def test_uses_team_sdk_cache_expiry(self, mock_get_sdk_versions: MagicMock):
+ mock_get_sdk_versions.return_value = {"web": [{"lib_version": "1.0.0", "max_timestamp": "x", "count": 1}]}
+ mock_redis = MagicMock()
+
+ get_and_cache_team_sdk_versions(team_id=1, redis_client=mock_redis)
+
+ mock_redis.setex.assert_called_once()
+ _key, ttl, _payload = mock_redis.setex.call_args[0]
+ assert ttl == TEAM_SDK_CACHE_EXPIRY