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(insights): skip redundant insight caching state writes on dashboard reads (#69408)

Paul D'Ambrac38ed3c56eb6
diff --git a/posthog/caching/insight_cache.py b/posthog/caching/insight_cache.py
index 5d3645b69ac0..e30143e3c59e 100644
--- a/posthog/caching/insight_cache.py
+++ b/posthog/caching/insight_cache.py
@@ -144,8 +144,13 @@ def update_cached_state(
# :TRICKY: We update _all_ states with same cache_key to avoid needless re-calculations and
# handle race conditions around cache_key changing.
- return InsightCachingState.objects.filter(team_id=team_id, cache_key=cache_key).update(
- last_refresh=timestamp, refresh_attempt=0
+ # We exclude rows already at these exact values so a warm dashboard read - where every tile is
+ # a cache hit and reports its already-stored last_refresh - doesn't issue one redundant
+ # UPDATE per tile. The end state is identical; only no-op writes are skipped.
+ return (
+ InsightCachingState.objects.filter(team_id=team_id, cache_key=cache_key)
+ .exclude(last_refresh=timestamp, refresh_attempt=0)
+ .update(last_refresh=timestamp, refresh_attempt=0)
)
diff --git a/posthog/caching/test/test_insight_cache.py b/posthog/caching/test/test_insight_cache.py
index 3ac262718085..fe296292ff37 100644
--- a/posthog/caching/test/test_insight_cache.py
+++ b/posthog/caching/test/test_insight_cache.py
@@ -9,7 +9,7 @@ from django.utils.timezone import now
import orjson as json
-from posthog.caching.insight_cache import update_cache
+from posthog.caching.insight_cache import update_cache, update_cached_state
from posthog.caching.insight_caching_state import upsert
from posthog.caching.test.test_insight_caching_state import create_insight, filter_dict
from posthog.models import Team, User
@@ -122,3 +122,23 @@ def test_update_cache_when_recently_refreshed(team: Team, user: User):
updated_caching_state = InsightCachingState.objects.get(team=team)
assert updated_caching_state.last_refresh == caching_state.last_refresh
+
+
+@freeze_time("2020-01-04T13:01:01Z")
+def test_update_cached_state_skips_redundant_write(team: Team, user: User):
+ # A warm dashboard read calls update_cached_state once per tile, each reporting the
+ # already-stored last_refresh. A row already at the target values must not be re-written,
+ # otherwise every tile issues a redundant per-request UPDATE.
+ caching_state = create_insight_caching_state(team, user, last_refresh=timedelta(days=14), refresh_attempt=3)
+ fresh = now()
+
+ # A genuine change writes the row and resets refresh_attempt.
+ assert update_cached_state(team.pk, caching_state.cache_key, fresh, result=None) == 1
+
+ # Repeating with the same timestamp is a no-op - the redundant write is skipped.
+ assert update_cached_state(team.pk, caching_state.cache_key, fresh, result=None) == 0
+
+ updated_caching_state = InsightCachingState.objects.get(pk=caching_state.pk)
+ assert updated_caching_state.last_refresh == fresh
+ assert updated_caching_state.refresh_attempt == 0